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
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <map>

using namespace std;
typedef long long I;
#define MAX 524288

//#define MAX 32

//#define MAX 8

#define MAXQ 131072

#define MAXI 3100000000000000000LL

#define ATACK 1
#define ADD 2
#define REM 3
struct K {
    I c,s,k;
    int pos = -1;
};

K kom[MAXQ];


#define DD(x)
#define DDD(x)
#define DDD2(x) 

#define EE(x) 

struct D {
    D() : sum(0), req(0), small(MAXI), big(0), count(0) {}
    D(I w) : sum(w), req(w+1), small(w), big(w), count(1) {}
    I sum, req, small, big, count;
    int id = -1;
    void print(){
     //   std::cout <<"r:" << req << " s:" << sum  /*<< "_:" << small*/ << " b:" << big << " c:" << count << " | ";
    //    std::cout << " s:" << sum << " c:" << count << " | ";
      //  std::cout << " s:" << sum << " b:" << big << "|" ;
        std::cout << " s:" << sum << "|" ;
    }
};

D d[MAX*2];

void update(I i) {
    d[i].sum = d[2*i].sum + d[2*i+1].sum;
    d[i].count = d[2*i].count + d[2*i+1].count;
    d[i].small = std::min(d[2*i].small, d[2*i+1].small);
    d[i].big = std::max(d[2*i].big, d[2*i+1].big);
    d[i].req = std::max(d[2*i].req, d[2*i+1].req - d[2*i].sum);
}

int find(I w, int posw = 1) {
    DD(cout << "f:" << posw << "\n");
    if(MAX <= posw) return posw;
    if(w>=d[2*posw+1].small) return find(w, 2*posw+1);
    return find(w, 2*posw);
}

int findL(I w, int posw = 1) {
    DD(cout << "f:" << posw << "\n");
    if(MAX <= posw) return posw;
    if(w>d[2*posw].big) return findL(w, 2*posw+1);
    return findL(w, 2*posw);
}

void remove_pos(int pos) {
    d[pos] = D();
    while(pos) {
        pos/=2;
        update(pos);
    }
}
void remove_w(I w) {
    int pos = find(w);
    d[pos] = D();
    while(pos) {
        pos/=2;
        update(pos);
    }
}


void add(int pos, I w) {
    d[pos] = D(w);
    while(pos) {
        pos/=2;
        update(pos);
    }
}

int sumR(int posw, I &s, I k, int pos = 1, int a= MAX, int b = 2*MAX) {
    if (b <= posw || d[pos].sum < 1 || k <= s) return 0;
    if (posw <= a && d[pos].req <= s && s+d[pos].sum <= k) {
        s+=d[pos].sum;
        return d[pos].count;
    }
    if(MAX <= pos) {
        if (d[pos].req <= s) {
            s+=d[pos].sum;
            return d[pos].count;
        }
        return 0;
    }
    int res = 0;
    res += sumR(posw, s, k, 2*pos,   a, (a+b)/2);
    res += sumR(posw, s, k, 2*pos+1, (a+b)/2, b);
    return res;
}



I sumL(int posw, int &dist, int pos = 1, int a= MAX, int b = 2*MAX) {
    if (pos >= 2*MAX) return 0;
    if (posw < a || d[pos].count < 1 || dist == 0) return 0;
    if (b <= posw && d[pos].count <= dist) {
        dist-=d[pos].count;
        return d[pos].sum;
    }
    
    I res = 0;
    res += sumL(posw, dist, pos*2+1, (a+b)/2, b);
    res += sumL(posw, dist, 2*pos,   a, (a+b)/2);
    return res;
}


int sum(int aa, int bb, int &npos,I &s, I k, int pos = 1, int a = MAX, int b=2*MAX) {
//    std::cout << "!sum:" << aa -MAX << "," << bb -MAX << " - " << a -MAX  << "," << b-MAX << " pos:" << pos - MAX<<  "\n";
    if (pos >= 2*MAX) return 0;
    if (d[pos].count < 1 || s >=k) return 0;
    if (aa <= a && b <= bb && s+d[pos].sum <= k) {
        s+=d[pos].sum;
        npos = a;
        return d[pos].count;
    }
    if(aa <= a && b <= bb && MAX <= pos && d[pos].sum != MAXI) {
        s+=d[pos].sum;
        npos = a;
        return d[pos].count;
    }
    int ab2 = (a+b)/2;
    int res = 0;
    if (ab2<=bb)
        res += sum(std::max(ab2,aa), bb, npos, s, k, 2*pos+1, ab2, b);
    if (aa <= ab2)
        res += sum(aa, std::min(ab2, bb), npos, s, k, 2*pos,  a ,ab2);
    
    return res;
}

int findSum(std::vector<std::pair<int,int>> &przed, I &s, I k, int res = 0) {
    if (przed.empty()) return res;
    auto p = przed.back();
    przed.pop_back();
    int pos = p.second;
    int count = sum(p.first, p.second, pos, s, k);
    if (s>=k) {
        przed.emplace_back(p.first, pos);
        return count+res;
    }
    return findSum(przed, s, k, res+count);
}

int findLess(I w) {
    int pos = findL(w);
    EE(std::cout << "!!!findL:" << pos -MAX<< "\n");
    while(d[pos].sum >= w) pos--;
    return pos;
}

int calc2(std::vector<std::pair<int,int>> &przed, I &s, I k, int res = 0) {
    if (s>=k) return res;
    int pos = findL(s);
    EE(std::cout << "!pos:" << pos-MAX << " s:" << s << "\n");
    if(pos >= 2*MAX || k-s <= d[pos].sum - s +1) {
        EE(std::cout << "A:\n");
        I ss = 0;
        int count = findSum(przed, ss, k-s);
        EE(std::cout << " ss: " << s  << " count:" << count << "\n");

        if(ss < k-s) return -1;
        return res+count;
    } else {
        EE(std::cout << "B:\n");
        I ss = 0;
        int count = findSum(przed, ss, d[pos].sum - s +1);
        if(ss < d[pos].sum - s +1) return -1;
        s += ss;
        if (s>=k) return count+res;
        EE(std::cout << " s: " << s  << " count:" << count  << " pos:" << pos << "\n");
        int npos = findLess(s);
        EE(std::cout << " przed: " << pos+1 -MAX << "," << std::min(2*MAX,npos+1) -MAX  << "\n");
        przed.emplace_back(pos, std::min(2*MAX,npos+1));
        return calc2(przed, s, k, count+res);
    }
}


int calc(I s, I k) {
    int npos = findLess(s);
    EE(std::cout << "npos:" << npos -MAX<<  " s:" << s <<"\n");
    std::vector<std::pair<int,int>> przed;
    przed.emplace_back(MAX, std::min(2*MAX, npos+1));

    int res = calc2(przed, s, k);
    if(res >= MAX+1) return -1;
    return res;
}

void print()
{
    int v = 2;
    for(int i=1;i<MAX*2;i++) {
        d[i].print();
        if(i == v-1) {
            std::cout << "\n";
            v*=2;
        }
    }
}

int main()
{
    I n,w,nn,q;
    scanf("%lld", &n);
    for(nn=0;nn<n;nn++) {
        scanf("%lld", &w);
        d[MAX+nn] = D(w);
    }
    scanf("%lld", &q);
    for(I j=0;j<q;j++) {
        I c,s,k;
        scanf("%lld %lld", &c, &s);
        if(c == ATACK) scanf("%lld", &k);
        kom[j] = {c,s,k};
    }

    //ADD
    for(I j=0;j<q;j++) 
        if(kom[j].c == ADD) {
            d[MAX+nn] = D(kom[j].s);
            d[MAX+nn].id = j;
            nn++;
        }
    d[MAX+nn++] = D(MAXI);

    sort(&d[MAX], &d[2*MAX], [](const D& a, const D &b) {return a.sum < b.sum;} );

    DDD2(std::cout << "sorted\n";
    print());

    //Update pos
    for(int i=MAX;i<2*MAX;i++) {
        if(d[i].id>=0) kom[d[i].id].pos = i;
    }

    for(I i=MAX-1; i > 0; i--) update(i);

    //REMOVE
    for(I j=0;j<q;j++) if(kom[j].c == ADD) remove_pos(kom[j].pos);


/*    std::cout << "Removed\n";

    
    print();
    int ff = 3;
    std::cout << "find" << ff << "->" << find(ff) - MAX << "\n";
    ff=8;
    std::cout << "findL" << ff << "->" << findL(ff) - MAX << "\n";
    int d =1;
    std::cout << "sumL:" << sumL(MAX+4, d) << "\n";
    std::cout << "sumR:" << sumR(MAX+2, s,k) << "\n";
    std::cout << "s: " << s << "\n";
 
   int pos;
    I s=9,k=16;
    std::cout << "sum:" << sum(6+MAX, 7+MAX, pos, s, k) << "\n";
    std::cout << "s: " << s << "\n";
*/
//    return 1 ;
    

    for(I j=0;j<q;j++) {
        switch(kom[j].c) {
            case ADD: 
                add(kom[j].pos, kom[j].s);
                DDD2(std::cout << "added pos:" << kom[j].pos - MAX  << " w: " << kom[j].s << "\n";
                print());
                break;
            case REM: 
                remove_w(kom[j].s); 
                DDD2(std::cout << "removed:" << kom[j].s << "\n";
                print());
                break;
            case ATACK: 
                EE(print());
  //              printf("WYNIK %lld: %d\n", j, calc(kom[j].s, kom[j].k)); 
                printf("%d\n", calc(kom[j].s, kom[j].k)); 
            break;
        }
    }
}