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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include <bits/stdc++.h>

using namespace std;


template<typename val_t>
struct AVLTree {
  
  struct Node {
    Node *p, *l, *r;
    val_t v;
    int h;
    int sz;
    
    val_t mv; // max value
    val_t sum; // sum of values
    int q; // query id
    int iq; // min query id
    
    void init() {
      p = l = r = NULL;
      h = 1;
      sz = 1;
      
      mv = v;
      sum = v;
      q = 0;
      iq = 0;
    }
    
    Node() { init(); }
    Node(val_t const &_v): v(_v) { init(); }
    ~Node() { delete l; delete r; }
    
    static int heightOf(Node const * const &ch) {
      return (ch == NULL ? 0 : ch->h);
    }
    
    static int sizeOf(Node const * const &ch) {
      return (ch == NULL ? 0 : ch->sz);
    }
    
    int balanceFactor() const {
      return heightOf(r) - heightOf(l);
    }
    
    void fix() {
      h = max(heightOf(l), heightOf(r)) + 1;
      sz = sizeOf(l) + sizeOf(r) + 1;
      
      mv = v;
      sum = v;
      iq = q;
      if(l != NULL) { mv = max(mv, l->v); sum += l->sum; iq = min(iq, l->iq); }
      if(r != NULL) { mv = max(mv, r->v); sum += r->sum; iq = min(iq, r->iq); }
    }
    
    val_t usum() {
      val_t ret = v;
      
      if(l != NULL) ret += l->sum;
      if(p != NULL) {
        if(p->r == this) {
          ret += p->v;
          if(p->l != NULL) {
            ret += p->l->sum;
          }
        }
      }
      
      return ret;
    }
    
    Node *rotateRight() {
      Node *t = l;
      if(t == NULL) return this;
      swap(v, t->v);
      l = t->l;
      t->l = t->r;
      t->r = r;
      r = t;
      if(l != NULL)    l->p = this;
      if(t->r != NULL) t->r->p = t;
      t->fix();
      fix();
      return this;
    }
    
    Node *rotateLeft() {
      Node *t = r;
      if(t == NULL) return this;
      swap(v, t->v);
      r = t->r;
      t->r = t->l;
      t->l = l;
      l = t;
      if(r != NULL)    r->p = this;
      if(t->l != NULL) t->l->p = t;
      t->fix();
      fix();
      return this;
    }
    
    void balance() {
      fix();
      if(balanceFactor() == 2) {
        if(r->balanceFactor() < 0) r->rotateRight();
        rotateLeft();
      }
      if(balanceFactor() == -2) {
        if(l->balanceFactor() > 0) l->rotateLeft();
        rotateRight();
      }
    }
    
    void insert(val_t const &w, Node *&ch) {
      if(ch == NULL) {ch = new Node(w); ch->p = this; }
      else            ch->insert(w);
      balance();
    }
    
    void insert(val_t const &w) {
      if(w < v) insert(w, l);
      else      insert(w, r);
    }
    
    Node *first() {
      if(l != NULL) return l->first();
      return this;
    }
    
    Node *next() {
      if(r != NULL) return r->first();
      
      Node *t = this;
      while(t != NULL) {
        if(t->p != NULL and t->p->l == t and t->p->r != NULL) return t->p->r->first();
        t = t->p;
      }
      
      return this;
    }
    
    Node *erase(AVLTree &tr) {
      // cerr << "ethis = " << this << "\n";
      if(l == NULL or r == NULL) {
        Node *t = NULL;
        if(l != NULL) {t = l; l = NULL; }
        if(r != NULL) {t = r; r = NULL; }
        if(t != NULL) t->p = p;
        if(p != NULL) {
          if(this == p->l) p->l = t;
          if(this == p->r) p->r = t;
          p->balance();
        }
        if(tr.root == this) {
          tr.root = t;
        }
        return this;
      }
      
      Node *n = next();
      v = n->v;
      Node *ret = n->erase(tr);
      fix();
      if(p != NULL) p->fix();
      balance();
      if(p != NULL) p->balance();
      
      return ret;
    }
    
    Node *lowerBound(val_t const &w, Node * const &ch) {
      if(ch == NULL) return this;
      else           return ch->lowerBound(w);
    }
    
    Node *lowerBound(val_t const &w) {
      if(w > v) return lowerBound(w, r);
      else {
        Node *ret = lowerBound(w, l);
        return (ret == NULL or ret->v < w) ? this : ret;
      }
    }
    
    int countGreater() const {
      int ans = sizeOf(r);
      for(Node const *t = p, *u = this; t != NULL; u = t, t = t->p) {
        if(t->l == u) ans += 1 + sizeOf(t->r);
      }
      return ans;
    }
    
    void debug(int d = 0) const {
      for(int i = 0; i < d; ++i) cerr << "----";
      cerr << "Node " << v << " h=" << h << " sz=" << sz << "   t=" << this << " p = " << p << " l=" << l << " r=" << r << '\n';
      cerr << 'l';
      if(l) l->debug(d+1);
      else  cerr << '\n';
      cerr << 'r';
      if(r) r->debug(d+1);
      else  cerr << '\n';
    }
  };
  
  Node *root;
  
  int lq; // last query
  
  AVLTree(): root(NULL), lq(0) { }
  ~AVLTree() { delete root; }
  
  void insert(val_t const &w) {
    if(root == NULL) root = new Node(w);
    else             root->insert(w);
  }
  
  Node *lowerBound(val_t const &w) const {
    if(root == NULL) return NULL;
    Node *ret = root->lowerBound(w);
    return ret->v < w ? NULL : ret;
  }
  
  int countGreater(val_t const &w) const {
    Node *t = lowerBound(w);
    if(t == NULL) return 0;
    return (t->v != w) + t->countGreater();
  }
  
  void erase(Node *n) {
    bool last = (root->sz == 1);
    Node *t = n->erase(*this);
    // cerr << "erasing " << t << '\n';
    if(t) {
      t->l = NULL;
      t->r = NULL;
    }
    delete t;
    if(last) root = NULL;
  }
  
  void debug() const {
    if(root) root->debug();
  }
  
};


typedef AVLTree<int64_t>::Node TNode;


TNode *greaterThan(AVLTree<int64_t> &tr, TNode *n, int64_t s) {
  if(n->iq >= tr.lq) return NULL;
  
  if(n->v > s) {
    TNode *t = n->l;
    if(t == NULL or t->iq >= tr.lq) return n;
    t = greaterThan(tr, t, s);
    if(t != NULL and t->v > s) return t; else return n;
  } else {
    TNode *t = n->r;
    if(t == NULL) return NULL;
    return greaterThan(tr, t, s);
  }
}


TNode *greaterThan(AVLTree<int64_t> &tr, int64_t s) {
  TNode *n = tr.root;
  if(n == NULL) return NULL;
  
  return greaterThan(tr, n, s);
}


TNode *lowerThan(AVLTree<int64_t> &tr, TNode *n, int64_t s) {
  if(n->iq >= tr.lq) return NULL;
  
  if(n->v < s) {
    TNode *t = n->r;
    if(t == NULL or t->iq >= tr.lq) return n;
    t = lowerThan(tr, t, s);
    if(t != NULL and t->v < s) return t; else return n;
  } else {
    TNode *t = n->l;
    if(t == NULL) return NULL;
    return lowerThan(tr, t, s);
  }
}


TNode *lowerThan(AVLTree<int64_t> &tr, int64_t s) {
  TNode *n = tr.root;
  if(n == NULL) return NULL;
  
  return lowerThan(tr, n, s);
}


TNode *last(AVLTree<int64_t> &tr, TNode *n) {
  if(n->iq >= tr.lq) return NULL;
  
  if(n->r != NULL and n->r->iq < tr.lq) return last(tr, n->r);
  
  return n;
}


TNode *prevv(AVLTree<int64_t> &tr, TNode *n) {
  if(n->l != NULL and n->l->iq < tr.lq) return last(tr, n->l);
  
  while(n != NULL) {
    if(n->p != NULL and n->p->r == n) {
      if(n->p->q < tr.lq) return n->p;
      return prevv(tr, n->p);
    }
    n = n->p;
  }
  
  return NULL;
}


int64_t eat(AVLTree<int64_t> &tr, TNode *n) {
  int64_t ret = n->v;
  n->q = tr.lq;
  
  while(n != NULL) {
    n->iq = n->q;
    if(n->l) n->iq = min(n->iq, n->l->iq);
    if(n->r) n->iq = min(n->iq, n->r->iq);
    n = n->p;
  }
  
  return ret;
}


int query2(AVLTree<int64_t> &tr, int64_t s, int64_t k) {
  if(s >= k) return 0;
  
  // cerr << "s = " << s << " k = " << k << '\n';
  
  int64_t target = k;
  int count = 0;
  
  {
    TNode *g = greaterThan(tr, s - 1);
    if(g != NULL and g->v + 1 < target) {
      target = g->v + 1;
    }
  }
  
  // cerr << "s = " << s << "  target = " << target << '\n';
  
  TNode *m = lowerThan(tr, s);
  if(m == NULL) return -1;
  //cerr << "m = " << m << '\n';
  if(s + m->usum() < target) return -1;
  
  //cerr << "m = " << m << '\n';
  
  while(s < target) {
    if(m == NULL) return -1;
    s += eat(tr, m);
    // cerr << "IAH s = " << s << '\n';
    m = prevv(tr, m);
    count++;
  }
  
  if(target != k) {
    int c2 = query2(tr, s, k);
    if(c2 == -1) return -1;
    return c2 + count;
  }
  
  return count;
}

int query(AVLTree<int64_t> &tr, int64_t s, int64_t k) {
  tr.lq++;
  return query2(tr, s, k);
}


int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int n;
  cin >> n;
  
  AVLTree<int64_t> tr;
  
  for(int i = 0; i < n; i++) {
    int64_t w;
    cin >> w;
    tr.insert(w);
  }
  
  int q;
  cin >> q;
  
  while(q --> 0) {
    int op;
    cin >> op;
    
    if(op == 1) {
      int64_t s, k;
      cin >> s >> k;
      
      //tr.debug();
      // cerr << "sk " << s << ' ' << k << '\n';
      
      cout << query(tr, s, k) << '\n';
    } else if(op == 2) {
      int64_t w;
      cin >> w;
      tr.insert(w);
    } else /* if(op == 3) */ {
      int64_t w;
      cin >> w;
      tr.erase(tr.lowerBound(w));
    }
  }
  
  return 0;
}