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
#include <bits/stdc++.h>
#define ll long long
#define mk std::make_pair
#define st first
#define nd second
#define debug if(0)

struct Node{
    ll sum_open, sum;
    int cnt, cnt_open, lazy, on;
    Node(){sum = sum_open = cnt = cnt_open = on = 0; lazy = -1; }
};

const int base = 1<<19;
Node tree[2*base+3];

void push_down(int low, int high, int v){
    if (tree[v].lazy != -1){
        if (tree[v].lazy == 0)
            tree[v].cnt_open = tree[v].sum_open = 0;
        else{
            tree[v].sum_open = tree[v].sum; tree[v].cnt_open = tree[v].cnt;
        }
        tree[v].on = tree[v].lazy;
        if (low != high){
            tree[v*2].lazy = tree[v].lazy;
            tree[v*2+1].lazy = tree[v].lazy;
        }
    }
    tree[v].lazy = -1;
}

void add(int p, int low, int high, int v, ll x){
    if (low > high)
        return;
    push_down(low, high, v);
    if (p < low || p > high)
        return;
    if (low == high){
        tree[v].sum += x; tree[v].sum_open = tree[v].sum; tree[v].on = 1;
        if (x > 0)
            tree[v].cnt = tree[v].cnt_open = 1;
        else
            tree[v].cnt = tree[v].cnt_open = 0;
        return;
    }
    int mid = (low+high)/2;
    if (p <= mid)
        add(p, low, mid, v*2, x);
    else
        add(p, mid+1, high, v*2+1, x);
    tree[v].cnt_open = tree[v*2].cnt_open + tree[v*2+1].cnt_open;
    tree[v].sum_open = tree[v*2].sum_open + tree[v*2+1].sum_open;
    tree[v].sum = tree[v*2].sum + tree[v*2+1].sum;
    tree[v].cnt = tree[v*2].cnt + tree[v*2+1].cnt;
}

void set_bit(int L, int R, int low, int high, int v, int bit){
    if (L > R || low > high)
        return;
    push_down(low, high, v);
    if (L > high || R < low)
        return;
    if (low >= L && high <= R){
        tree[v].lazy = bit;
        push_down(low, high, v);
        return;
    }
    int mid = (low+high)/2;
    set_bit(L, R, low, mid, v*2, bit);
    set_bit(L, R, mid+1, high, v*2+1, bit);
    tree[v].sum_open = tree[v*2].sum_open + tree[v*2+1].sum_open;
    tree[v].cnt_open = tree[v*2].cnt_open + tree[v*2+1].cnt_open;
}

ll sum(int L, int R, int low, int high, int v){
    if (L > R || low > high)
        return 0;
    push_down(low, high, v);
    if (L > high || R < low)
        return 0;
    if (low >= L && high <= R){
        return tree[v].sum_open;
    }
    int mid = (low+high)/2;
    return sum(L, R, low, mid, v*2) + sum(L, R, mid+1, high, v*2+1);
}

int cnt(int L, int R, int low, int high, int v){
    if (L > R || low > high)
        return 0;
    push_down(low, high, v);
    if (L > high || R < low)
        return 0;
    if (low >= L && high <= R){
        return tree[v].cnt_open;
    }
    int mid = (low+high)/2;
    return cnt(L, R, low, mid, v*2) + cnt(L, R, mid+1, high, v*2+1);
}

struct Event{
    int type; // 1 to pytanie o mase poczatkowa x, koncowa y
              // 2 to dodawanie szproty o masie x w node y
              // 3 to usuniecie szproty o masie x w node y
    ll x, y;
    Event(){}
    Event(int T, ll X, ll Y) : type(T), x(X), y(Y) {}
};

std::map<ll, int> next_free;
std::map<ll, int> last_deleted;
std::map<std::pair<ll, int>, int> fish_node;
std::vector<Event> events;
std::set<std::pair<ll, int> > fishes;
int n, q;
int N = 0;
std::vector<int> masses;

void input(){
    std::cin >> n;
    int k; ll x, y;
    for (int i = 1; i <= n; i++){
        N++;
        std::cin >> x;
        events.emplace_back(2, x, next_free[x]);
        fishes.insert(mk(x, next_free[x]));
        next_free[x] = next_free[x] + 1;
        masses.push_back(x);
    }
    std::cin >> q;
    for (int i = 1; i <= q; i++){
        std::cin >> k >> x;
        if (k == 1){
            std::cin >> y;
            events.emplace_back(k, x, y);
        }
        else if (k == 2){
            N++;
            events.emplace_back(2, x, next_free[x]);
            fishes.insert(mk(x, next_free[x]));
            next_free[x] = next_free[x] + 1;
            masses.push_back(x);
        }
        else{
            events.emplace_back(3, x, last_deleted[x]);
            last_deleted[x] = last_deleted[x] + 1;
        }
    }
    next_free.clear(); last_deleted.clear();
    int nb = 0;
    for (auto fish: fishes)
        fish_node[fish] = nb++;
    for (auto &e: events)
        if (e.type > 1)
            e.y = fish_node[mk(e.x, e.y)];
    fishes.clear(); fish_node.clear();
    std::sort(masses.begin(), masses.end());
}

int get_next_i_cant(ll S){
    // zwraca indeks pierwszego ktorego nie moze zjesc lub -1 jesli takiego nie ma
    // o masie S rybka ofc
    // czyli pierwszy >= S
    auto it = fishes.lower_bound(mk(S, 0));
    if (it == fishes.end())
        return -1;
    return (it->nd);
}

int get_next_i_can(ll S){
    // jest zalozenie ze jest taki jea
    // czyli ostatni < S
    auto it = fishes.lower_bound(mk(S, 0));
    it--;
    return (it->nd);
}

int find_begin_to_eat(int last, ll S){
    // najpozniejszy taki j <= last ze suma [j, last] >= S
    int b = 1;
    int e = last+1;
    int mid;
    while (b < e){
        mid = (b+e)/2;
        if (sum(last-mid+1, last, 0, N-1, 1) >= S)
            e = mid;
        else
            b = mid+1;
    }
    if (sum(last-b+1, last, 0, N-1, 1) >= S)
        return last-b+1;
    return -1;
}

int answer(ll S, ll M){
    // zaczynam z masa S, mam zakonczyc z M
    // <<< ---------!! ALGORYTM !! ---------- >>> (klepa)
    int res = 0;
    while (S < M){
        // ustawiam bit 1 na calym przedziale
        // next to pierwszy ktorego nie moge zjesc, albo -1 jesli moge wszystkie
        int next = get_next_i_cant(S);
        if (next == -1 || masses[next] >= M){
            // musze zjesc jak najmniej od gory zeby osiagnac M
            if (next == -1)
                next = N;
            int i = find_begin_to_eat(next-1, M-S);
            if (i == -1)
                return -1; // no nie daje rady
            S += sum(i, next-1, 0, N-1, 1);
            res += cnt(i, next-1, 0, N-1, 1);
            set_bit(i, next-1, 0, N-1, 1, 0);
        }
        else{
            // chce zjesc jak najmniej od gory zeby moc zjesc next
            if (sum(0, next-1, 0, N-1, 1) < masses[next] - S + 1)
                return -1; // nie moze osiagnac next, a skoro next < M to nie moze osiagnac M
            // chce znalezc tez najpozniejszy indeks ze moge zjesc od i do next-1
            int i = find_begin_to_eat(next-1, masses[next] - S + 1);
            if (i == -1){
                return -1; // no nie jestem w stanie zjesc niczego dalej juz
            }
            // chce je zjesc (od first do next-1)
            // zalozylem ze istnieje taki wiec luz
            S += sum(i, next-1, 0, N-1, 1);
            res += cnt(i, next-1, 0, N-1, 1);
            set_bit(i, next-1, 0, N-1, 1, 0);
            if (S >= M){
                // no przez przypadek osiagnalem wynik
                break;
            }
            // jem nexta, albo i wiekszego!
            int next_to_eat = get_next_i_can(S);
            S += masses[next_to_eat];
            res++;
            set_bit(next_to_eat, next_to_eat, 0, N-1, 1, 0);
        }
    }
    return res;
}

void solve(){
    // po prostu lece przez eventy
    for (auto e: events){
        set_bit(0, N-1, 0, N-1, 1, 1);
        if (e.type == 2){
            // dodanie ryby
            add(e.y, 0, N-1, 1, e.x);
            fishes.insert(mk(e.x, e.y));
        }
        else if (e.type == 3){
            // usuniecie ryby
            add(e.y, 0, N-1, 1, -e.x);
            fishes.erase(fishes.find(mk(e.x, e.y)));
        }
        else{
            // zapytanie
            std::cout << answer(e.x, e.y) << "\n";
        }
    }
}

int main(){
    std::ios_base::sync_with_stdio(0); std::cin.tie(NULL);
    input();
    solve();
}