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
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <stack>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
typedef short int sint;
#define FOR(x, b, e) for(int x=(b); x<=(e); ++x)
#define FORD(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define REP(x, n) for(int x=0; x<(n); ++x)
#define ALL(c) c.begin(),c.end()
#define SIZE(x) ((int)((x).size()))
#define PB push_back
#define ST first
#define ND second
#define DEBUG 0
#define debug(x) {if (DEBUG)cerr <<#x <<" = " <<x <<endl; }
#define debugv(x) {if (DEBUG) {cerr <<#x <<" = "; FOREACH(it, (x)) cerr <<*it <<", "; cout <<endl; }}
#define REMAX(a,b) (a)=max((a),(b));
#define REMIN(a,b) (a)=min((a),(b));
#define wez(n) int (n); scanf("%d",&(n));
#define wez2(n,m) int (n),(m); scanf("%d %d",&(n),&(m));

struct Node {
	ll sum, cnt;
	Node(): sum(0), cnt(0) {

	}
	Node(ll _sum, ll _cnt): sum(_sum), cnt(_cnt)  {}
	Node operator-(const Node& a) const {
		return {sum - a.sum, cnt - a.cnt};
	}
};
Node merge(const Node& a, const Node& b) {
	return {a.sum + b.sum, a.cnt + b.cnt};
}

const int N = 400101;
const int M = 1 << 20;
const int K = 100101;
int n, P, q, roz;
ll w[N];
pair<ll, int> ev[N];
map<pair<ll, int>, int> pos;
pair<int, pair<ll, ll>> zap[K];
Node Tree[M];

void change(int position, ll value) {
	if (DEBUG) printf("change: %d pos to value: %lld\n", position, value);
	int idx = P + position - 1;
	Tree[idx].sum = value;
	Tree[idx].cnt = value > 0 ? 1 : 0;
	while (idx / 2) {
		idx /= 2;
		Tree[idx] = merge(Tree[2 * idx], Tree[2 * idx + 1]);
	}
}

Node getSum(int p, int q, int Pocz, int Kon, int ind) {
	if (p == Pocz && q == Kon) {
		return Tree[ind];
	}
	int sr = (Pocz + Kon) / 2;
	if (q <= sr) {
		return getSum(p, q, Pocz, sr, 2 * ind);
	} else if (p > sr) {
		return getSum(p, q, sr + 1, Kon, 2*ind + 1);
	} else {
		return merge(getSum(p, sr, Pocz, sr, 2 * ind), getSum(sr + 1, q, sr+ 1, Kon, 2 * ind + 1));
	}
}

Node prefSum(int position) {
	return getSum(1, position, 1, P, 1);
}

Node rangeSum(int p1, int p2) {
	if (p1 > p2) {
		return {0, 0};
	}
	Node sumEnd = prefSum(p2);
	if (p1 > 1) {
		sumEnd = sumEnd - prefSum(p1 - 1);
	}
	return sumEnd;
}

bool touch(pii r1, pii r2) {
	if (r1 > r2) {
		swap(r1, r2);
	}
	return r1.ND + 1 == r2.ST;
}

pii lacz(pii r1, pii r2) {
	return {min(r1.ST, r2.ST), max(r1.ND, r2.ND)};
}

void add(vector<pii> &eaten, pii range) {
	if (DEBUG) {
		printf("adding range: [%d;%d]\n", range.ST, range.ND);
	}
	while (!eaten.empty()) {
		if (touch(eaten.back(), range)) {
			range = lacz(eaten.back(), range);
			eaten.pop_back();
		} else {
			eaten.push_back(range);
			if (DEBUG) {
				printf("EATEN: ");
				for (auto eat : eaten) {
					printf(" [%d - %d] ", eat.ST, eat.ND);
				}
				printf("\n");
			}
			return;
		}
	}
	eaten.PB(range);
	if (DEBUG) {
		printf("EATEN: ");
		for (auto eat : eaten) {
			printf(" [%d - %d] ", eat.ST, eat.ND);
		}
		printf("\n");
	}
}

void printPos() {
	if (DEBUG) {
		printf("POS:\n");
		REP(i, roz) {
			printf("pos[(%lld, %d)] = %d\n", ev[i].ST, ev[i].ND, pos[ev[i]]);
		}
	}
}

int main() {
	scanf("%d", &n);
	REP(i, n) {
		scanf("%lld", &w[i]);
		ev[i] = {w[i], i};
	}
	roz = n;
	scanf("%d", &q);
	REP(i, q) {
		scanf("%d %lld", &zap[i].ST, &zap[i].ND.ST);
		if (zap[i].ST == 1) {
			scanf("%lld", &zap[i].ND.ND);
		}
		if (zap[i].ST == 2) {
			zap[i].ND.ND = n + i;
			ev[roz] = {zap[i].ND.ST, zap[i].ND.ND};
			++roz;
		}
	}
	sort(ev, ev + roz);
	REP(i, roz) {
		pos[ev[i]] = i + 1;
	}
	printPos();
	P = 1;
	while (P < roz) {
		P <<= 1;
	}
	set<pair<ll, int>> szproty;
	REP(i, roz) {
		if (ev[i].ND >= n) {
			continue;
		}
		int position = pos[ev[i]];
		Tree[P + position - 1] = {ev[i].ST, 1};
		szproty.insert({ev[i].ST, position});
	}
	FORD(i, P, 1) {
		Tree[i] = merge(Tree[2 * i], Tree[2 *i + 1]);
	}
	REP(k, q) {
		if (zap[k].ST > 1) {
			pair<ll, int> event = {zap[k].ND.ST, (int)zap[k].ND.ND};
			int position;
			ll value = zap[k].ND.ST;
			if (zap[k].ST == 3) {
				auto it = szproty.lower_bound({value, 0});
				position = it->ND;
				szproty.erase(it);
				if (DEBUG) printf("\tRemove szprot: %lld, pos: %d\n", value, position);
				value = 0;
			} else {
				position = pos[event];
				if (DEBUG) printf("\tAdd szprot: %lld, pos: %d\n", value, position);
				szproty.insert({value, position});
			}
			change(position, value);
		} else {
			// szczuuuuuupak!
			ll s = zap[k].ND.ST;
			if (DEBUG) {
				printf("\tSzczupak %lld -> %lld\n", s, zap[k].ND.ND);
				printf("\tSzproty\n");
				for (auto szp : szproty) {
					printf("  %d - %lld\n", szp.ND, szp.ST);
				}
			}
			int ile = 0;
			vector<pii> eaten;
			while (s < zap[k].ND.ND) {
				// czy istnieje wieksza szprotka
				ll chce = zap[k].ND.ND - s;
				pair<ll, int> szuk = {s, 0}; // cant eat this much, first too big
				auto lower_b = szproty.lower_bound(szuk);
				if (lower_b != szproty.end()) {
					chce = min(chce, lower_b->ST - s + 1);
				}
				if (DEBUG) {
					printf("Current weight: %lld, want: %lld\n", s, chce);
				}
				int position = lower_bound(ev, ev + roz, szuk) - ev;
				pii myRange(1, position);
				if (eaten.size() > 0 && eaten.back().ND < position) {
					myRange.ST = eaten.back().ND + 1;
				} else if (eaten.size() > 0 && eaten.back().ND == position) {
					myRange.ND = eaten.back().ST - 1;
					myRange.ST = eaten.size() >= 2 ? eaten[SIZE(eaten) - 2].ND + 1 : 1;
				}
				// moge jesc szproty w myRange
				if (DEBUG) {
					printf("range to eat: [%d; %d]\n", myRange.ST, myRange.ND);
				}
				if (myRange.ST > myRange.ND) {
					// no range to eat
					break;
				}
				auto ileMogeInRange = rangeSum(myRange.ST, myRange.ND);
				if (ileMogeInRange.sum < chce) {
					// zjedz te wszystkie, zrob kolejny obrot petli
					add(eaten, myRange);
					ile += ileMogeInRange.cnt;
					s += ileMogeInRange.sum;
					if (DEBUG) {
						printf("eating full range, not enough\n");
					}
				} else {
					// need to do binsearch in this range
					int pp = myRange.ST + 1;
					int qq= myRange.ND + 1;
					while (pp < qq) {
						int sr = (pp + qq) / 2;
						Node sum = rangeSum(sr, myRange.ND);
						if (sum.sum >= chce) {
							pp = sr + 1;
						} else {
							qq = sr;
						}
					}
					// zjadam od: pp - 1;myRange.ND;
					Node sum = rangeSum(pp - 1, myRange.ND);
					ile += sum.cnt;
					s += sum.sum;
					if (DEBUG) {
						printf("ate [%d;%d] happy!\n", pp - 1, myRange.ND);
					}
					add(eaten, {pp - 1, myRange.ND});
				}
			}
			if (s < zap[k].ND.ND) {
				printf("-1\n");
			} else {
				printf("%d\n", ile);
			}
		}
	}
	return 0;
}