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
#include <iostream>
#include <set>
#include <map>
#include <limits>
#include <vector>

#include <iomanip>               // for std::setw
#include <ios>                   // for std::noskipws, streamsize
#include <istream>               // for std::istream
#include <ostream>               // for std::ostream
#include <sstream>               // for std::ostringstream
#include <cstddef>               // for NULL
#include <stdexcept>             // for std::domain_error
#include <string>                // for std::string implicit constructor
#include <cstdlib>               // for std::abs
#include <limits>                // for std::numeric_limits

#include<type_traits>

using namespace std;

// #define DEBUG

#ifdef DEBUG
#define dbg cerr
// #define dbg2 cerr
#define dbg2 0 && cerr
#else
#define dbg 0 && cerr
#define dbg2 0 && cerr
#endif

using fish = unsigned long long;


std::multiset<fish> w;
std::vector<fish> fishes;

struct Node;
Node* root = nullptr;

std::set<Node*> nodes_to_recover;

struct Node {
    void recover() {
        suma = suma_original;
        nr_of_fish = nr_of_fish_original;
        marked = false;
    }

    fish suma, suma_original;
    int nr_of_fish, nr_of_fish_original;
    bool marked;

    int left, right;
    Node *p_left, *p_right;

    int middle() {
        return (this->left + this->right)/2;
    }

    void build(fish values[], int left, int right) {
        dbg2 << "Build node " << left << ", " << right << endl;
        if (left > right) {
            return;
        }

        this->left = left;
        this->right = right;

        if (left == right) {
            this->suma = values[left];
            nr_of_fish = 1;
        } else {
            this->p_left = new Node();
            this->p_right = new Node();

            this->p_left->build(values, left, middle());
            this->p_right->build(values, middle()+1, right);

            nr_of_fish = p_left->nr_of_fish + p_right->nr_of_fish;
            this->suma = this->p_left->suma +  this->p_right->suma;
        }
        suma_original = suma;
        nr_of_fish_original = nr_of_fish;
        this->marked = false;
    }

    fish sum(int left, int right) {
        dbg2 << "[Node " << this->left << ", " << this->right << "]: Sum of (" << left <<  ", " << right << "): ";
        if (left > right) {
            dbg2 << "0\n";
            return 0;
        }

        if (this->left == left && this->right == right) {
            dbg2 << "this node: " << this->suma << "\n";
            return this->suma;
        }

        push();

        dbg2 << "\n";

        return this->p_left->sum(left, min(right, middle())) + 
               this->p_right->sum(max(left, middle()+1), right);
    }

    void push() {
        if (marked) {
            this->p_left->suma = 0;
            this->p_right->suma = 0;
            this->suma = 0;
            this->nr_of_fish = 0;

            this->p_left->marked = true;
            this->p_right->marked = true;
            marked = false;

            nodes_to_recover.insert(this);
        }
    }

    void zero(int l, int r) {
        dbg2 << "Zeroing (" << l <<  ", " << r << "): ";
        if (l > r) {
            dbg2 << "none.\n";
            return;
        }

        if (this->left == l && this->right == r) {
            dbg2 << "this node: " << this->suma << "\n";
            this->marked = true;
            this->suma = 0;
            this->nr_of_fish = 0;
            nodes_to_recover.insert(this);
        } else {
            dbg2 << "\n";
            push();

            this->p_left->zero(l, min(r, middle()));
            this->p_right->zero(max(l, middle()+1), r);

            nodes_to_recover.insert(this);
            this->suma = this->p_left->suma +  this->p_right->suma;
            this->nr_of_fish = p_left->nr_of_fish + p_right->nr_of_fish;
        }
    }

    int nr_of_fishes(int l, int r) {
        if (l > r) {
            return 0;
        }

        if (this->left == l && this->right == r) {
            return this->nr_of_fish;
        }

        push();
        return this->p_left->nr_of_fishes(l, min(r, middle())) + 
               this->p_right->nr_of_fishes(max(l, middle()+1), r);
    }

    void delet() {
        if (left == right) {
            return;
        } else {
            this->p_left->delet();
            this->p_right->delet();

            delete p_left;
            delete p_right;
        }
    }

    void print() {
        if (left == right) {
            dbg2 << "Leaf: " << left << ": " << this->suma << endl;
        } else {
            this->p_left->print();
            this->p_right->print();
            dbg2 << "(" << left << ", " << right << "): " << this->suma << endl;
        }
    }
};

int min_index_with_sum(fish suma, int end_index) {
    int a = 0, b = end_index;

    //dbg << "Binary search: (" << a << ", " << b << ")" << endl;

    while (a < b) {
        int m = (a+b)/2;
        //dbg << "(" << a << ", " << b << ") -> " << root->sum(m, end_index) << endl;

        if (a + 1 == b) {
            if (root->sum(b, end_index) >= suma) {
                return b;
            }
            return a;
        }

        if (root->sum(m, end_index) >= suma) {
            a = m;
        } else {
            b = m;
        }
    }

    return a;
}




int ask_query(fish from, fish to) {
    if (from >= to) {
        //dbg << "Already good!" << endl;
        return 0;
    }
    if (fishes.size() == 0) {
        //dbg << "No fishes to eat :(" << endl;
        return -1;
    }

    fish weight_now = from;
    int result = 0;
    while(weight_now < to) {
        //dbg << "WEIGHT NOW: " << weight_now << "\n";

        if (fishes[0] >= weight_now) {
            //dbg << "Too small to try :(" << endl;
            return -1;
        }

        {
            auto greater_or_equal_to_weight = lower_bound(fishes.begin(), fishes.end(), weight_now);
            auto less_than_weight = greater_or_equal_to_weight;
            less_than_weight--;

            int index_of_less_than_weight = less_than_weight - fishes.begin();

            bool ok = root->nr_of_fishes(index_of_less_than_weight, index_of_less_than_weight) == 1;

            if (ok && weight_now + *less_than_weight >= to) {
                //dbg << "by eating " << *less_than_weight << " (index " << index_of_less_than_weight << ") we can grow enough!" << endl;
                result++;
                return result;
            }
        }

        auto greater_or_equal_to_weight = lower_bound(fishes.begin(), fishes.end(), weight_now);
        
        if (greater_or_equal_to_weight == fishes.begin()) {
            //dbg << "Cannot eat! Smallest " << *fishes.begin() << endl;
            return -1;
        }

        if (greater_or_equal_to_weight == fishes.end()) {
            auto weight_left = to - weight_now;
            //dbg << "Only smaller! Weight left: " << weight_left << endl;

            auto index_done = min_index_with_sum(weight_left, fishes.size()-1);

            //dbg << "Eating [" << index_done << ", " << fishes.size()-1 << "]: " << root->sum(index_done, fishes.size()-1) << endl;

            weight_now += root->sum(index_done, fishes.size()-1);
            result += root->nr_of_fishes(index_done, fishes.size()-1);

            if (weight_now >= to) {
                return result;
            } else {
                return -1;
            }
        }

        auto weight_to_eat = *greater_or_equal_to_weight - weight_now + 1;
        int index_of_weight_to_eat = greater_or_equal_to_weight - fishes.begin();

        auto index_done = min_index_with_sum(weight_to_eat, index_of_weight_to_eat-1);

        //dbg << "To eat fish " << *greater_or_equal_to_weight << " (index " << index_of_weight_to_eat << ")" <<
        //       " you need to eat " << weight_to_eat << " [weight]. Can do that by eating [" << index_done << ", " <<
        //       index_of_weight_to_eat-1 << "]: fishes " << root->nr_of_fishes(index_done, index_of_weight_to_eat-1) << endl;
        

        //dbg << "Increased weight by " << root->sum(index_done, index_of_weight_to_eat-1);

        weight_now += root->sum(index_done, index_of_weight_to_eat-1);
        result += root->nr_of_fishes(index_done, index_of_weight_to_eat-1);
        root->zero(index_done, index_of_weight_to_eat-1);

        //dbg << " to actual: " << weight_now << endl;;

        if (weight_now >= to) {
            return result;
        }

        if (weight_now <= *greater_or_equal_to_weight) {
            //dbg << "\nAte all I could but without success :(\n";
            return -1;
        }



        {
            auto greater_or_equal_to_weight = lower_bound(fishes.begin(), fishes.end(), weight_now);
            auto less_than_weight = greater_or_equal_to_weight;
            less_than_weight--;

            int index_of_less_than_weight = less_than_weight - fishes.begin();

            bool ok = root->nr_of_fishes(index_of_less_than_weight, index_of_less_than_weight) == 1;

            if (ok) {
                //dbg << "Eating smaller one! " << *less_than_weight << "(index " << index_of_less_than_weight << ")\n";

                weight_now += *less_than_weight;
                root->zero(index_of_less_than_weight, index_of_less_than_weight);
                result++;
            }
        }
    }
    //dbg << "DONE\n";
    return result;
}

void query(fish from, fish to) {
    //dbg << endl << endl;
    //dbg << "Query " << from << " -> " << to << endl;

    auto result = ask_query(from, to);
    cout << result << endl;

    for(auto& x: nodes_to_recover) {
        x->recover();
    }
    nodes_to_recover.clear();
}

void build_all() { 
    if (root) {
        root->delet();
        delete root;
    }

    fishes.clear();
    //dbg << "Fishes length = " << w.size() << endl;
    for(const auto& x: w) {
        //dbg << x << " ";
        fishes.push_back(x);
    }
    //dbg << endl;

    root = new Node();
    if (fishes.size() > 0) {
        root->build(fishes.data(), 0, fishes.size()-1);
#ifdef DEBUG
        root->print();
#endif
    }
}


int main() {
   std::ios_base::sync_with_stdio(false);
   std::cin.tie(nullptr);

    int n;
    cin >> n;
    for(int i = 0; i < n; ++i) {
        fish now;
        cin >> now;

        w.insert(now);
    }

    bool to_build = true;

    int queries;
    cin >> queries;

    while(queries--) {
        int type;
        cin >> type;

        if (type == 1) {
            fish from, to;
            cin >> from >> to;

            if (to_build) {
                to_build = false;
                //dbg << "Building all!" << endl;
                build_all();
            }

            query(from, to);
        } else if (type == 2) {
            // add fish
            fish x; 
            cin >> x;
            w.insert(x);

            to_build = true;
        } else if (type == 3) {
            // remove fish
            fish x; 
            cin >> x;
            w.erase(x);

            to_build = true;
        }
    }
}