1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <bits/stdc++.h>


using namespace std;

struct val_t {
    int min, minCount;
    long long minTotalValue;
};

struct Tree {
    typedef int mod_t;

    int p = 1;
    vector <val_t> V;
    vector <mod_t> M;

    vector <int> init;
    vector <long long> value;

    Tree(int n, vector <int> &init, vector <long long> &value): init(init), value(value) {
        while (p < n) p *= 2;

        int m = 2 * p - 1;
        V.resize(m);
        M.resize(m, getZeroMod());

        for (int i = p - 1; i < m; i++) {
            V[i] = getValInit(i - p + 1);
        }

        for (int i = p - 2; i >= 0; i--) {
            V[i] = sum(V[2 * i + 1], V[2 * i + 2]);
        }
    }

    void apply(val_t &v, const mod_t &m) {
        v.min += m;
    }

    void buff(mod_t &a, const mod_t &b) {
        a += b;
    }

    val_t sum(const val_t &a, const val_t &b) {
        if (a.min < b.min) {
            return a;
        } else if (a.min > b.min) {
            return b;
        } else {
            return {
                a.min, a.minCount + b.minCount, a.minTotalValue + b.minTotalValue
            };
        }
    }

    val_t getValInit(int i) {
        if (i < init.size()) {
            return {init[i], 1, value[i]};
        } else {
            return {1, 0, 0};
        }
    }

    mod_t getZeroMod() { return 0; }

    void modify(int x, int y, const mod_t &m) { _modify(0, 0, p-1, x, y, m); }
    val_t query(int x, int y) { return _query(0, 0, p-1, x, y); }

    void _push(int u) {
        apply(V[u], M[u]);

        if (u < p-1) {
            buff(M[2 * u + 1], M[u]);
            buff(M[2 * u + 2], M[u]);
        }

        M[u] = getZeroMod();
    }

    void _modify(int u, int a, int b, int x, int y, const mod_t &m) {
        _push(u);

        if (x <= a && b <= y) {
            buff(M[u], m);
            _push(u);
            return ;
        }

        int mid = (a + b) / 2;

        if (x <= mid) _modify(2 * u + 1, a, mid, x, y, m);
        else _push(2 * u + 1);

        if (mid < y) _modify(2 * u + 2, mid + 1, b, x, y, m);
        else _push(2 * u + 2);

        V[u] = sum(V[2 * u + 1], V[2 * u + 2]);
    }

    val_t _query(int u, int a, int b, int x, int y) {
        _push(u);

        if (x <= a && b <= y) {
            return V[u];
        }

        int mid = (a + b) / 2;

        if (y <= mid) {
            return _query(2 * u + 1, a, mid, x, y);
        } else if (mid < x) {
            return _query(2 * u + 2, mid + 1, b, x, y);
        } else {
            return sum(
                _query(2 * u + 1, a, mid, x, y),
                _query(2 * u + 2, mid + 1, b, x, y)
            );
        }
    }
};

struct event {
    int type;

    int id;
    long long s, k;
};

vector <int> solve(int n, int nInitial, vector <long long> &w, vector <event> &events) {
    vector <pair<long long, int>> weightAndId(n);
    for (int i = 0; i < n; i++) {
        weightAndId[i] = {w[i], i};
    }

    set <pair<long long,int>> alive(weightAndId.begin(), weightAndId.begin() + nInitial);

    sort(weightAndId.begin(), weightAndId.end());

    vector <int> init(n);
    for (int i = 0; i < n; i++) {
        init[i] = weightAndId[i].second >= nInitial;
    }

    vector <int> pos(n);
    for (int i = 0; i < n; i++) {
        pos[weightAndId[i].second] = i;
    }

    vector <long long> wInOrder(n);
    for (int i = 0; i < n; i++) {
        wInOrder[i] = weightAndId[i].first;
    }

    Tree tree(n, init, wInOrder);

    vector <int> ans;

    for (auto &e : events) {
        if (e.type == 1) {
            long long curr = e.s, target = e.k;

            int ansHere;

            if (curr >= target) {
                ansHere = 0;
            } else if (alive.empty() || alive.begin()->first >= curr) {
                ansHere = -1;
            } else {
                ansHere = 0;

                vector <pair<int,int>> toRevert;

                while (curr < target) {
                    auto it = alive.lower_bound({curr, -1});
                    int i = pos[prev(it)->second];

                    long long need = target - curr;

                    if (it != alive.end()) {
                        need = min(need, it->first - curr);
                    }

                    int l = -1, r = i + 1;
                    while (r - l > 1) {
                        int mid = (l + r) / 2;

                        auto res = tree.query(mid, i);

                        if (res.min == 0 && res.minTotalValue >= need) {
                            l = mid;
                        } else {
                            r = mid;
                        }
                    }

                    if (l == -1) {
                        ansHere = -1;
                        break;
                    }

                    auto res = tree.query(l, i);

                    ansHere += res.minCount;
                    curr += res.minTotalValue;

                    tree.modify(l, i, 1);
                    toRevert.push_back({l, i});
                }

                for (auto &r : toRevert) {
                    tree.modify(r.first, r.second, -1);
                }
            }

            ans.push_back(ansHere);
        } else {
            int p = pos[e.id];

            if (e.type == 2) {
                tree.modify(p, p, -1);
                alive.insert({w[e.id], e.id});
            } else {
                tree.modify(p, p, 1);
                alive.erase({w[e.id], e.id});
            }
        }
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;

    int nInitial = n;

    vector <long long> w(n);
    for (long long &wi : w) {
        cin >> wi;
    }

    map <long long,vector<int>> withValue;
    for (int i = 0; i < n; i++) {
        withValue[w[i]].push_back(i);
    }

    int q;
    cin >> q;

    vector <event> events(q);
    for (auto &ei : events) {
        cin >> ei.type;

        if (ei.type == 1) {
            cin >> ei.s >> ei.k;
        } else {
            long long wi;
            cin >> wi;

            if (ei.type == 2) {
                w.push_back(wi);
                ei.id = n++;

                withValue[wi].push_back(ei.id);
            } else {
                ei.id = withValue[wi].back();
                withValue[wi].pop_back();
            }
        }
    }

    auto ans = solve(n, nInitial, w, events);

    for (int x : ans) {
        cout << x << '\n';
    }

    return 0;
}