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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include <bits/stdc++.h>
#include <unistd.h>
using namespace std;
#define REP(i,n) for(int _n=(n), i=0;i<_n;++i)
#define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i)
#define TRACE(x) cerr << "TRACE(" #x ")" << endl;
#define DEBUG(x) cerr << #x << " = " << (x) << endl;
typedef long long LL; typedef unsigned long long ULL;

class Input {
 public:
  Input() { bufpos = bufend = buffer; eof = false; }
  bool Eof() { return eof; }
  char Peek() { if(bufpos == bufend) Grab(); return *bufpos; }
  unsigned char UPeek() { return static_cast<unsigned char>(Peek()); }
  void SkipWS();
  template<class T> T Get();
  void operator()() {}
  template<class Arg, class... Args> void operator()(Arg &arg, Args &... args) {
    arg = Get<Arg>();
    operator()(args...);
  }
 private:
  static const int BUFSIZE = 1<<16;
  char buffer[BUFSIZE];
  char *bufpos;
  char *bufend;
  bool eof;
  void Grab();
};

void Input::Grab() {
  if(eof) return;
  bufpos = buffer;
  bufend = buffer + read(0, buffer, BUFSIZE);
  if(bufend==bufpos) { eof=true; *bufpos=0; }
}

template<> inline char Input::Get<char>() {
  char res = Peek();
  ++bufpos;
  return res;
}

void Input::SkipWS() {
  while(isspace(UPeek())) Get<char>();
}

template<> unsigned Input::Get<unsigned>() {
  SkipWS();
  unsigned x = 0;
  while(isdigit(UPeek())) {
    x = 10u * x + (Get<char>()-'0');
  }
  return x;
}

template<> int Input::Get<int>() {
  SkipWS();
  bool neg = false;
  if(Peek()=='-') { neg=true; Get<char>(); }
  unsigned x = Get<unsigned>();
  if (neg) x = -x;
  return static_cast<int>(x);
}

template<> ULL Input::Get<ULL>() {
  SkipWS();
  ULL x = 0;
  while(isdigit(UPeek())) {
    x = 10ULL * x + (Get<char>()-'0');
  }
  return x;
}

template<> LL Input::Get<LL>() {
  SkipWS();
  bool neg = false;
  if(Peek()=='-') { neg=true; Get<char>(); }
  ULL x = Get<ULL>();
  if (neg) x = -x;
  return static_cast<LL>(x);
}

template<> string Input::Get<string>() {
  SkipWS();
  string s;
  while(!Eof() && !isspace(UPeek())) s += Get<char>();
  return s;
}

class Output {
 public:
  void Flush();
  Output():bufpos(buffer),BUFLIMIT(buffer+BUFSIZE-100) {}
  ~Output() { Flush(); }
  void Put(char c);
  void Put(unsigned x);
  void Put(int x);
  void Put(ULL x);
  void Put(LL x);
  void Put(const char*s);
  void Put(const string&s);

  void operator()() {}
  template<class Arg, class... Args> void operator()(const Arg &arg, const Args &... args) {
    Put(arg);
    operator()(args...);
  }
 private:
  static const int BUFSIZE = 1<<16;
  char buffer[BUFSIZE];
  char *bufpos;
  char *BUFLIMIT;
};

void Output::Flush() {
  char *p = buffer;
  while(p < bufpos) {
    p += write(1, p, bufpos-p);
  }
  bufpos = buffer;
}

inline void Output::Put(char c) {
  *bufpos = c;
  ++bufpos;
  if(bufpos >= BUFLIMIT) Flush();
}

void Output::Put(unsigned x) {
  char *old = bufpos;
  do {
    *bufpos = char('0' + x % 10u);
    x /= 10u;
    ++bufpos;
  } while(x);
  reverse(old, bufpos);
  if(bufpos >= BUFLIMIT) Flush();
}

void Output::Put(int x) {
  if(x<0) {
    Put('-'); Put(-static_cast<unsigned>(x));
  } else {
    Put(static_cast<unsigned>(x));
  }
}

void Output::Put(ULL x) {
  char *old = bufpos;
  do {
    *bufpos = char('0' + x % 10u);
    x /= 10u;
    ++bufpos;
  } while(x);
  reverse(old, bufpos);
  if(bufpos >= BUFLIMIT) Flush();
}

void Output::Put(LL x) {
  if(x<0) {
    Put('-'); Put(-static_cast<ULL>(x));
  } else {
    Put(static_cast<ULL>(x));
  }
}

void Output::Put(const char*s) {
  while(*s) Put(*s++);
}

void Output::Put(const string &s) {
  for(char c : s) Put(c);
}

Input IN;
Output OUT;

struct Fish {
  LL weight;
  int query; // when added, or -1
};

inline bool operator<(const Fish &a, const Fish &b) {
  return a.weight < b.weight;
}

enum QueryType {
  query_none = 0,
  query_predator = 1,
  query_add = 2,
  query_delete = 3,
};

struct Query {
  QueryType type = query_none;
  int fish_idx = -1; // for query_add
  LL weight = 0; // for query_predator and query_delete
  LL goal = 0; // for query_predator
};

constexpr int MAX_QUERIES = 100000;
constexpr int MAX_FISHES = 1<<19; // including added

vector<Fish> fishes;
vector<Query> queries;

void read_input() {
  int n; IN(n);
  fishes.reserve(n + MAX_QUERIES);
  REP(i,n) {
    Fish fish;
    IN(fish.weight);
    fish.query = -1;
    fishes.push_back(fish);
  }
  int nq; IN(nq);
  queries.reserve(nq);
  REP(q,nq) {
    Query query;
    int qt; IN(qt);
    query.type = QueryType(qt);
    switch(query.type) {
      case query_predator: {
        IN(query.weight);
        IN(query.goal);
        break;
      }
      case query_add: {
        Fish fish;
        IN(fish.weight);
        fish.query = q;
        fishes.push_back(fish);
        break;
      }
      case query_delete: {
        IN(query.weight);
        break;
      }
      default: {
        assert(false);
      }
    }
    queries.push_back(query);
  }

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

  REP(i, int(fishes.size())) {
    int q = fishes[i].query;
    if (q != -1) {
      queries[q].fish_idx = i;
    }
  }
}

struct TreeNode {
  int blocked = 0;
  int count = 0;
  int children_count = 0;
  LL weight_sum = 0;
  LL children_weight_sum = 0;

  void block() {
    ++blocked;
    update_values();
  }
  void unblock() {
    --blocked;
    update_values();
  }

  void update_values() {
    if (blocked == 0) {
      count = children_count;
      weight_sum = children_weight_sum;
    } else {
      count = 0;
      weight_sum = 0;
    }
  }
};

struct KillResults {
  LL weight = 0;
  int count = 0;
  int fish_idx1 = 0;
  int fish_idx2 = 0;
};

class Pond {
 public:
  Pond();
  void add_fish(int fish_idx);
  void block_fish(int fish_idx);
  LL prefix_weight(int fish_idx) const;
  int find_prefix(LL max_weight) const;
  int total_count() const { return nodes[1].count; }
  LL total_weight() const { return nodes[1].weight_sum; }
  void block_range(int fish_idx1, int fish_idx2, int delta_block);
  KillResults kill_some(int fish_idx_too_large, LL goal_weight);
  void undo_kill(const KillResults &kill_results);
 private:
  static constexpr int LEAF_OFFSET = MAX_FISHES+1;
  inline int leaf(int fish_idx) const { return LEAF_OFFSET+fish_idx; }
  void propagate(int p) {
    TreeNode &a = nodes[p];
    const TreeNode &b = nodes[2*p];
    const TreeNode &c = nodes[2*p+1];
    a.children_count = b.count + c.count;
    a.children_weight_sum = b.weight_sum + c.weight_sum;
    a.update_values();
  }
  vector<TreeNode> nodes;
};

Pond::Pond() : nodes(2*MAX_FISHES) {
  REP(i,int(fishes.size())) {
    nodes[leaf(i)].blocked = 1;
  }
  REP(i,int(fishes.size())) {
    const Fish &fish = fishes[i];
    TreeNode &node = nodes[leaf(i)];
    node.children_count = 1;
    node.children_weight_sum = fish.weight;
    if (fish.query == -1) node.unblock();
  }
  FORD(i,MAX_FISHES-1,1) propagate(i);
}

void Pond::add_fish(int fish_idx) {
  // only leaves can be blocked here
  int p = leaf(fish_idx);
  nodes[p].unblock();
  while (p != 1) {
    p >>= 1;
    propagate(p);
  }
}

void Pond::block_fish(int fish_idx) {
  // only leaves can be blocked here
  int p = leaf(fish_idx);
  nodes[p].block();
  while (p != 1) {
    p >>= 1;
    propagate(p);
  }
}

LL Pond::prefix_weight(int fish_idx) const {
  int p = leaf(fish_idx);
  LL weight = 0;
  while (p != 1) {
    if (p&1) weight += nodes[p-1].weight_sum;
    p >>= 1;
    if (nodes[p].blocked) weight = 0;
  }
  return weight;
}

int Pond::find_prefix(LL max_weight) const {
  // finds largest fish_idx such that prefix_weight(fish_idx) <= max_weight
  int p = 1;
  if (nodes[p].weight_sum <= max_weight) return fishes.size();
  while (p<MAX_FISHES) {
    assert(!nodes[p].blocked);
    p <<= 1;
    if (nodes[p].weight_sum <= max_weight) {
      max_weight -= nodes[p].weight_sum;
      ++p;
    }
  }
  return p - LEAF_OFFSET;
}

void Pond::block_range(int fish_idx1, int fish_idx2, int delta_block) {
  int p1 = leaf(fish_idx1);
  int p2 = leaf(fish_idx2);
  for (;;) {
    if (p1&1) {
      if (p1==p2) break;
      nodes[p1].blocked += delta_block;
      nodes[p1].update_values();
      ++p1;
    }
    if (p2&1) {
      --p2;
      nodes[p2].blocked += delta_block;
      nodes[p2].update_values();
    }
    p1 >>= 1;
    p2 >>= 1;
    propagate(p1-1);
    propagate(p2);
  }
  while (p1 != 1) {
    p1 >>= 1;
    propagate(p1);
  }
}

int first_fish_bigger_than(LL weight) {
  return upper_bound(fishes.begin(), fishes.end(), Fish{weight, -1}) - fishes.begin();
}

KillResults Pond::kill_some(int fish_idx_too_large, LL goal_weight) {
  KillResults kill_results;
  kill_results.fish_idx2 = fish_idx_too_large;
  LL available_weight = prefix_weight(kill_results.fish_idx2);
  if (available_weight < goal_weight) {
    // Unsuccessful kill.
    return kill_results;
  }
  kill_results.fish_idx1 = find_prefix(available_weight - goal_weight);
  int count_before = total_count();
  LL weight_before = total_weight();
  block_range(kill_results.fish_idx1, kill_results.fish_idx2, 1);
  kill_results.count = count_before - total_count();
  kill_results.weight = weight_before - total_weight();
  assert(kill_results.count > 0);
  assert(kill_results.weight >= goal_weight);
  return kill_results;
}

void Pond::undo_kill(const KillResults &kill_results) {
  block_range(kill_results.fish_idx1, kill_results.fish_idx2, -1);
}

int simulate_predator(LL predator_weight, LL goal_weight, Pond &pond) {
  vector<KillResults> kill_stack;
  int eaten = 0;
  while (predator_weight < goal_weight) {
    int fish_idx_too_large = first_fish_bigger_than(predator_weight-1);
    LL available_weight = pond.prefix_weight(fish_idx_too_large);
    int fish_idx_too_large_alive = pond.find_prefix(available_weight);
    assert(fish_idx_too_large_alive >= fish_idx_too_large);

    LL mini_goal = goal_weight - predator_weight;
    if (fish_idx_too_large_alive < int(fishes.size())) {
      LL smaller_goal = fishes[fish_idx_too_large_alive].weight+1 - predator_weight;
      if (smaller_goal < mini_goal) mini_goal = smaller_goal;
    }
    KillResults kill_results = pond.kill_some(fish_idx_too_large, mini_goal);
    if (kill_results.count == 0) {
      eaten = -1;
      break;
    }
    eaten += kill_results.count;
    predator_weight += kill_results.weight;
    kill_stack.push_back(kill_results);
  }

  while (!kill_stack.empty()) {
    pond.undo_kill(kill_stack.back());
    kill_stack.pop_back();
  }

  return eaten;
}

void kill_fish(LL weight, Pond &pond) {
  int fish2_idx = first_fish_bigger_than(weight);
  LL available_weight = pond.prefix_weight(fish2_idx);
  int fish1_idx = pond.find_prefix(available_weight-weight);
  pond.block_fish(fish1_idx);
}

void process_query(const Query &query, Pond &pond) {
  switch(query.type) {
    case query_predator: {
      int eaten = simulate_predator(query.weight, query.goal, pond);
      OUT(eaten, '\n');
      break;
    }
    case query_add: {
      pond.add_fish(query.fish_idx);
      break;
    }
    case query_delete: {
      kill_fish(query.weight, pond);
      break;
    }
    default: {
      assert(false);
    }
  }
}

int main() {
  read_input();
  Pond pond;
  for (const Query &query : queries) process_query(query, pond);
}