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
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <cstdlib>

#include <algorithm>
#include <memory>
#include <vector>

// #define SHOWDEBUG

using llu = unsigned long long int;

struct node_stats_t {
  uint64_t sum = 0;
  uint64_t count = 0;
  int64_t max = -1;
};

struct node_t {
  uint64_t left = 0;
  uint64_t right = 0;
  uint64_t cleared_count = 0;

  node_stats_t stats = {0, 0};
};

std::vector<node_t> tree;
size_t root = 0;

const uint64_t MAX_SIZE = (uint64_t)1 << (uint64_t)41;

template <typename F>
struct scope_guard {
  F f;
  scope_guard(F&& f) : f(f) {}
  ~scope_guard() { f(); }
};

template <typename F>
scope_guard<F> make_scope_guard(F&& f) {
  return scope_guard<F>(std::move(f));
}

size_t allocate_node() {
  tree.push_back(node_t{});
  return tree.size() - 1;
}

void update_stats(size_t node) {
  if (tree[node].cleared_count > 0) {
    tree[node].stats.sum = 0;
    tree[node].stats.count = 0;
    tree[node].stats.max = -1;
  } else {
    tree[node].stats.sum =
        tree[tree[node].left].stats.sum + tree[tree[node].right].stats.sum;
    tree[node].stats.count =
        tree[tree[node].left].stats.count + tree[tree[node].right].stats.count;

    tree[node].stats.max = std::max(tree[tree[node].left].stats.max,
                                    tree[tree[node].right].stats.max);
  }

#ifdef SHOWDEBUG
  printf("Update stats for %llu: %llu %llu %lli\n", (llu)node,
         (llu)tree[node].stats.sum, (llu)tree[node].stats.count,
         (long long int)tree[node].stats.max);
#endif
}

template <typename F>
uint64_t inner_operate_direct(size_t node,
                              uint64_t position,
                              uint64_t width,
                              F f) {
  if (node == 0) {
    node = allocate_node();
  }

  if (width == 1) {
    f(tree[node]);
    // Don't bother removing, for now
    // I think that we have enough memory not to free any nodes
    return node;
  }

  if (position < width / 2) {
    tree[node].left = inner_operate_direct(tree[node].left, position, width / 2,
                                           std::move(f));
  } else {
    tree[node].right = inner_operate_direct(
        tree[node].right, position - width / 2, width / 2, std::move(f));
  }

  update_stats(node);
  return node;
}

template <typename F>
void operate_direct(uint64_t position, F f) {
  root = inner_operate_direct<F>(root, position, MAX_SIZE, std::move(f));
}

// Assumes that actual_begin <= target_begin && target_end <= actual_end
template <bool NeedUpdate, typename F>
void inner_operate_range(size_t node,
                         uint64_t actual_begin,
                         uint64_t actual_end,
                         uint64_t target_begin,
                         uint64_t target_end,
                         F& f) {
  // printf("Visiting %u\n", (unsigned int)node);
  if (node == 0) {
    return;  // Nothing to to, this is an invalid node
  }

#ifdef SHOWDEBUG
  printf("Enter [%llu, %llu)\n", (llu)actual_begin, (llu)actual_end);
#endif

  if (actual_begin == target_begin && actual_end == target_end) {
#ifdef SHOWDEBUG
    printf("  OP [%llu, %llu)\n", (llu)actual_begin, (llu)actual_end);
#endif
    f(tree[node]);
    if (NeedUpdate) {
      update_stats(node);
    }
    return;
  }

  const uint64_t midpt = (actual_begin + actual_end) / 2;
  if (target_end <= midpt) {
    inner_operate_range<NeedUpdate, F>(tree[node].left, actual_begin, midpt,
                                       target_begin, target_end, f);
  } else if (midpt <= target_begin) {
    inner_operate_range<NeedUpdate, F>(tree[node].right, midpt, actual_end,
                                       target_begin, target_end, f);
  } else {
    inner_operate_range<NeedUpdate, F>(tree[node].left, actual_begin, midpt,
                                       target_begin, midpt, f);
    inner_operate_range<NeedUpdate, F>(tree[node].right, midpt, actual_end,
                                       midpt, target_end, f);
  }

  if (NeedUpdate) {
    update_stats(node);
  }

#ifdef SHOWDEBUG
  printf("Leave [%llu, %llu)\n", (llu)actual_begin, (llu)actual_end);
#endif
}

template <bool NeedUpdate, typename F>
void operate_range(uint64_t begin, uint64_t end, F f) {
  inner_operate_range<NeedUpdate, F>(root, 0, MAX_SIZE, begin,
                                     std::min(end, MAX_SIZE), f);
}

template <typename F>
std::tuple<uint64_t, uint64_t, size_t> find_by(F f) {
  uint64_t begin = 0;
  uint64_t width = MAX_SIZE;

  size_t node = root;
  while (width > 1) {
#ifdef SHOWDEBUG
    printf("  find_by(node: %llu, begin: %llu, width: %llu)\n", (llu)node,
           (llu)begin, (llu)width);
#endif

    if (node == 0) {
      break;
    }

    if (f(begin, begin + width, node)) {
      begin += width / 2;
      node = tree[node].right;
    } else {
      node = tree[node].left;
    }
    width /= 2;
  }

#ifdef SHOWDEBUG
  printf("  find_by returns(node: %llu, begin: %llu, width: %llu)\n", (llu)node,
         (llu)begin, (llu)width);
#endif

  return std::make_tuple(begin, begin + width, node);
}

void add_sprot(uint64_t position, uint64_t times = 1) {
  operate_direct(position, [position, times](node_t& nref) {
    nref.stats.sum += position * times;
    nref.stats.count += times;
    nref.stats.max = position;
  });
}

void remove_sprot(uint64_t position, uint64_t times = 1) {
  operate_direct(position, [position, times](node_t& nref) {
    nref.stats.sum -= position * times;
    nref.stats.count -= times;

    if (nref.stats.count == 0) {
      nref.stats.max = -1;
    }
  });
}

void mark_cleared(uint64_t begin, uint64_t end) {
#ifdef SHOWDEBUG
  printf("Clear range %llu %llu\n", (llu)begin, (llu)end);
#endif
  operate_range<true>(begin, end, [](node_t& nref) {
    nref.cleared_count++;
    // printf("CLEARING\n");
  });
}

void unmark_cleared(uint64_t begin, uint64_t end) {
#ifdef SHOWDEBUG
  printf("Unclear range %llu %llu\n", (llu)begin, (llu)end);
#endif
  operate_range<true>(begin, end, [](node_t& nref) {
    nref.cleared_count--;
    // printf("UNCLEARING\n");
  });
}

node_stats_t get_stats_on_range(uint64_t begin, uint64_t end) {
  node_stats_t ret = {};
#ifdef SHOWDEBUG
  printf("lel\n");
#endif
  operate_range<false>(begin, end, [&ret](node_t& nref) {
    ret.sum += nref.stats.sum;
    ret.count += nref.stats.count;
#ifdef SHOWDEBUG
    printf("  - Add %llu %llu (%llu)\n", (llu)nref.stats.sum,
           (llu)nref.stats.count, (llu)nref.cleared_count);
#endif
  });
#ifdef SHOWDEBUG
  printf("lol\n");
#endif
  return ret;
}

std::pair<uint64_t, size_t> get_first_sprot_not_before(uint64_t position) {
  const auto [pos, end, node] =
      find_by([position](uint64_t begin, uint64_t end, size_t node) {
        // const auto midpt = (begin + end) / 2;
        // if (midpt <= position) {
        //   // Our search range excludes the left part
        //   printf("    Left is excluded\n");
        //   return true;
        // }

        // Go to right part only if there are no nodes on the left
        if (!(tree[node].left != 0 && tree[tree[node].left].stats.max != -1 &&
              tree[tree[node].left].stats.max >= position)) {
#ifdef SHOWDEBUG
          printf("    No values on the left, or too small\n");
#endif
          return true;
        } else {
#ifdef SHOWDEBUG
          printf("    Values are on the left\n");
          printf("        (%llu %llu)\n", (llu)tree[node].left,
                 (llu)tree[tree[node].left].stats.count);
#endif
          return false;
        }
      });

// It may happen that we find nothing...
#ifdef SHOWDEBUG
  printf("end: %lld, pos: %lld\n", (llu)end, (llu)pos);
#endif
  if (node != 0) {
    return std::make_pair(pos, node);
  } else {
    return std::make_pair(MAX_SIZE, 0);
  }
}

int64_t simulate_pike_attack(const uint64_t start, const uint64_t target) {
  uint64_t pike_weight = start;
  int64_t steps = 0;

  std::vector<std::pair<uint64_t, uint64_t>> undo_sprot;
  std::vector<std::pair<uint64_t, uint64_t>> undo_clear;

  auto undo_guard = make_scope_guard([&] {
    // Un-clearing order is irrelevant
    for (auto p : undo_clear) {
      unmark_cleared(p.first, p.second);
    }

    for (auto p : undo_sprot) {
      add_sprot(p.first, p.second);
    }
  });

  while (pike_weight < target) {
#ifdef SHOWDEBUG
    printf("--- --- --- --- --- --- --- --- --- --- --- --- --- --- ---\n");
    printf("Big iteration: %llu %llu\n", (llu)pike_weight, (llu)target);
#endif
    // Find how many sprots to eat before advancing to the next one
    auto [sprot_num, nid] = get_first_sprot_not_before(pike_weight);
    // if (nid == 0) {
    //   sprot_num = target - 1;
    // }

    // TODO: Handle the case in which there are no more sprots

    const auto local_target = std::min(target, sprot_num + 1);

#ifdef SHOWDEBUG
    printf("target: %llu\n", (llu)target);
    printf("local target: %llu\n", (llu)local_target);
#endif

    // Perform binary search and find how many sprots do we need to eat to
    // advance past the next sprot
    uint64_t begin = 0;
    uint64_t end = pike_weight;
    node_stats_t stats;
    while (end - begin > 1) {
      const uint64_t midpt = (begin + end) / 2;
      stats = get_stats_on_range(midpt, pike_weight);
#ifdef SHOWDEBUG
      printf("Stats for [%llu, %llu): (%llu %llu)\n", (llu)midpt,
             (llu)pike_weight, (llu)stats.count, (llu)stats.sum);
#endif

      if (stats.sum >= local_target - pike_weight) {
        begin = midpt;
      } else {
        end = midpt;
      }
    }

    auto collect_begin = begin;
    stats = get_stats_on_range(collect_begin, pike_weight);
#ifdef SHOWDEBUG
    printf("Stats for [%llu, %llu): (%llu %llu)\n", (llu)collect_begin,
           (llu)pike_weight, (llu)stats.count, (llu)stats.sum);

    printf("Collecting across range [%llu, %llu)\n", (llu)collect_begin,
           (llu)pike_weight);
    printf("Collecting %llu sprots with %llu weight\n", (llu)stats.count,
           (llu)stats.sum);
#endif

    // If we did not achieve our local goal now, that means we fail
    if (pike_weight + stats.sum < local_target) {
#ifdef SHOWDEBUG
      printf("Failed to achieve target!\n");
      printf("%llu %llu %llu\n", (llu)pike_weight, (llu)stats.sum,
             (llu)local_target);
#endif
      return -1;
    }

    // This is tricky - we have selected the smallest range to collect sprots
    // from, but actually we might collect too much due to the fact that many
    // sprots may be accumulated at the beginning of the range. We need to
    // handle the first collected sprot size separately.
    if (stats.count > 0) {
// This should get the first node with sprots from the range
#ifdef SHOWDEBUG
      printf("kaskfsadf %llu %llu\n", (llu)stats.count, (llu)stats.sum);
#endif
      const auto [sprot_worth, node] =
          get_first_sprot_not_before(collect_begin);
#ifdef SHOWDEBUG
      printf("%llu %llu\n", (llu)sprot_worth, (llu)collect_begin);
#endif
      assert(node != 0);
      // assert(sprot_worth == collect_begin);
      collect_begin = sprot_worth;

      stats.sum -= tree[node].stats.sum;

      // Another binary search...
      uint64_t begin = 0;
      uint64_t end = stats.count;
      while (end - begin > 1) {
        const uint64_t midpt = (begin + end) / 2;
        if (pike_weight + stats.sum + midpt * sprot_worth >= local_target) {
          end = midpt;
        } else {
          begin = midpt;
        }
      }

      stats.sum += tree[node].stats.sum;

      const auto to_remove = begin + 1;
      const auto preserved = tree[node].stats.count - to_remove;

#ifdef SHOWDEBUG
      printf("Cut first: collecting %llu out of %llu\n", (llu)to_remove,
             (llu)stats.count);
#endif

      // Remove sprots from this size class separately
      undo_sprot.push_back(std::make_pair(sprot_worth, (uint64_t)to_remove));
      remove_sprot(sprot_worth, to_remove);

      stats.count -= preserved;
      stats.sum -= preserved * sprot_worth;

      // Move the range forward, we cleared the first sprot our way
      collect_begin += 1;
    }

    // Clear the range, add to clear log
    if (collect_begin < pike_weight) {
      undo_clear.push_back(std::make_pair(collect_begin, pike_weight));
      mark_cleared(collect_begin, pike_weight);
    }

    // Advance the pike
    pike_weight += stats.sum;
    steps += stats.count;
  }

// We reached the final goal
#ifdef SHOWDEBUG
  printf("Succeeded (%llu steps)\n", (llu)steps);
#endif
  return steps;
}

int main() {
  // One node is added as a dummy
  tree.push_back(node_t{});

  // Read and add all sprots
  unsigned int n;
  scanf("%u", &n);

  for (unsigned int i = 0; i < n; i++) {
    unsigned int sprot_pos;
    scanf("%u", &sprot_pos);
    add_sprot((uint64_t)sprot_pos);
  }

  // Read events and act accordingly
  unsigned int q;
  scanf("%u", &q);

  for (unsigned int i = 0; i < q; i++) {
    unsigned int evt;
    scanf("%u", &evt);

    if (evt == 1) {
      llu start, target;
      scanf("%llu %llu", &start, &target);
      const int64_t response =
          simulate_pike_attack((uint64_t)start, (uint64_t)target);
      printf("%d\n", (int)response);

    } else if (evt == 2) {
      unsigned int sprot_pos;
      scanf("%u", &sprot_pos);
      add_sprot((uint64_t)sprot_pos);

    } else if (evt == 3) {
      unsigned int sprot_pos;
      scanf("%u", &sprot_pos);
      remove_sprot((uint64_t)sprot_pos);

    } else {
      assert(0 && "Invalid input");
    }
  }

  return 0;
}