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
#include<bits/stdc++.h>

using namespace std;

#define PB push_back
#define MP make_pair
#define F first
#define S second
#define ALL(x) (x).begin(),(x).end()
#define SIZE(x) (int)(x).size()
#define CLEAR(tab) memset(tab, 0, sizeof(tab))
#define REP(x, n) for(int x = 0; x < (n); x++)
#define FOR(x, b, e) for(int x = (b); x <= (e); x++)
#define FORD(x, b, e) for(int x = (b); x >= (e); x--)
#define VAR(v, n) __typeof(n) v = (n)
#define FOREACH(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define DEBUG(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)

typedef long long int LL;
typedef pair<int,int> PII;
typedef vector<int> VI;

const int MXN = 300010;
const int C = 4 * 262144;
const int INF = 1000000001;

int zap;
int n, q;
LL w[MXN];
pair<LL, LL> query[MXN];

map<LL, int> M;
int skal = 1;
map<int, LL> M1;

LL tree[2 * C + 10];
int cnt[2 * C + 10];
pair<LL, int> tree2[2 * C + 10]; 
PII cnt2[2 * C + 10];
int mark[2 * C + 10];

multiset<LL> szprotki;

void init() {
	FOR(i, 1, n) {
		tree[M[w[i]] + C] += w[i];
		cnt[M[w[i]] + C]++;
	}
	FORD(i, C - 1, 1) {
		tree[i] = tree[2 * i] + tree[2 * i + 1];
		cnt[i] = cnt[2 * i] + cnt[2 * i + 1];
	}
	FOR(i, 1, n)
		szprotki.insert(w[i]);
}

void dodaj(int w, LL val) {
	w += C;
	while(w) {
		tree[w] += val;
		cnt[w] += (val > 0) ? 1 : (-1);
		w /= 2;
	}
}

//suma wartosci szprotek na przedziale [p, k], ile szprotek na [p, k]
//w tree2
pair<LL, int> zlicz(int w, int L, int R, int l, int r) {
	if(l <= L && R <= r) {
		if(tree2[w].S == zap)
			return MP(tree2[w].F, cnt2[w].F);
		else
			return MP(tree[w], cnt[w]);
	}

	int S = (L + R) / 2;
	pair<LL, int> res = MP(0, 0);
	
	if(mark[w] == zap)
		return MP(0, 0);
	if(l <= S) {
		pair<LL, int> p1 = zlicz(2 * w, L, S, l, r);
		res.F += p1.F;
		res.S += p1.S;
	}
	if(r > S) {
		pair<LL, int> p1 = zlicz(2 * w + 1, S + 1, R, l, r);
		res.F += p1.F;
		res.S += p1.S;
	}
	return res;
}

void update(int w) {	
	tree2[w] = MP(0, zap);
	cnt2[w] = MP(0, zap);
	if(mark[w] == zap)
		return;
	
	if(tree2[2 * w].S == zap) {
		tree2[w].F += tree2[2 * w].F;
		cnt2[w].F += cnt2[2 * w].F;
	}
	else {
		tree2[w].F += tree[2 * w];
		cnt2[w].F += cnt[2 * w];
	}

	if(tree2[2 * w + 1].S == zap) {
		tree2[w].F += tree2[2 * w + 1].F;
		cnt2[w].F += cnt2[2 * w + 1].F;
	}
	else {
		tree2[w].F += tree[2 * w + 1];
		cnt2[w].F += cnt[2 * w + 1];
	}

}

void wyrzuc(int w, LL val, int val2) {
	w += C;
	if(tree2[w].S == zap) {
		tree2[w].F -= val;
		cnt2[w].F -= val2;
	}
	else {
		tree2[w] = MP(tree[w] - val, zap);
		cnt2[w] = MP(cnt[w] - val2, zap);
	}
	
	if(mark[w]) {
		tree2[w] = MP(0, zap);
		cnt2[w] = MP(0, zap);
	}

	w /= 2;

	while(w) {
		update(w);
		w /= 2;
	}
}

//zjada wszystkie szprotki na przedziale [p, k]
void zeruj(int w, int L, int R, int l, int r) {
	if(l <= L && R <= r) {
		mark[w] = zap;
		tree2[w] = MP(0, zap);
		cnt2[w] = MP(0, zap);
		return;
	}
	int S = (L + R) / 2;
	if(l <= S)
		zeruj(2 * w, L, S, l, r);
	if(r > S)
		zeruj(2 * w + 1, S + 1, R, l, r);

	update(w);
}

//zwroc najwieksze takie p, ze suma(p, w) >= val
int przedzial(int w, LL val) {
	int L = 1, R = w;
	int res = -1;
	
	while(L <= R) {
		int S = (L + R) / 2;
		pair<LL, int> p1 = zlicz(1, 0, C - 1, S, w);
		if(p1.F >= val) {
			res = max(res, S);
			L = S + 1;
		}
		else
			R = S - 1;
	}
	
	return res;
}

LL value(int w) {
	if(tree2[w].S == zap)
		return tree2[w].F;
	return tree[w];
}

int przedzial2(int w, LL val) {
	int v = w + C;
	pair<LL, int> p1 = zlicz(1, 0, C - 1, 1, w);
	if(p1.F < val)
		return -1;

	int ost = -1;
	while(v) {
		if(mark[v] == zap)
			ost = v;
		v /= 2;
	}

	if(ost == -1)
		v = w + C;
	else
		v = ost;

	LL tmp = value(v);
	while(1) {
		if(tmp >= val)
			break;
		if(v % 2 == 1)
			tmp += value(v - 1);
		v /= 2;
	}
	if(v >= C)
		return v - C;

	tmp -= value(2 * v);
	v *= 2;
	
	while(v < C) {
		if(value(2 * v + 1) + tmp >= val)
			v = 2 * v + 1;
		else {
			tmp += value(2 * v + 1);
			v *= 2;
		}
	}

	return v - C;
}

int solve(LL s, LL k) {
	int res = 0;
	while(s < k) {
		auto it = szprotki.lower_bound(s);
		LL next = 0;
		if(it == szprotki.end() )
			next = k;
		else
			next = *it;
		int ind = M[next];
		if(next >= k)
			next = k;
		else
			next++;

	//	if(przedzial(ind - 1, next - s) != przedzial2(ind - 1, next - s) )
	//	cout<<przedzial(ind - 1, next - s)<<" "<<przedzial2(ind - 1, next - s)<<endl;
		int p = przedzial2(ind - 1, next - s);
	
		if(p == -1)
			return -1;
	
	//	PII p1 = zlicz(1, 0, C - 1, p, ind - 1);
		if(p + 1 <= ind - 1) {
			pair<LL, int> p1 = zlicz(1, 0, C - 1, p + 1, ind - 1);
			res += p1.S;
			zeruj(1, 0, C - 1, p + 1, ind - 1);
			s += p1.F;
		}

		if(s < k) {
			LL x = M1[p];
			LL ile = (next - s) / x;
			if(ile * x < next - s)
				ile++;	
			res += ile;
			s += ile * x;
			wyrzuc(p, ile * x, ile);
		}
	}
	
	return res;
}

void test() {
	scanf("%d", &n);
	FOR(i, 1, n) {
		scanf("%lld", &w[i]);
		 
		M[w[i]] = 1;
	}

	int q;
	scanf("%d", &q);
	FOR(i, 1, q) {
		int typ;
		scanf("%d", &typ);
		if(typ == 1) {
			LL s, k;
			scanf("%lld %lld", &s, &k);
			M[s] = M[k] = 1;
			query[i] = MP(s, k);
		}
		else if(typ == 2) {
			LL x;
			scanf("%lld", &x);
			M[x] = 1;
			query[i] = MP(-2, x);
		}
		else if(typ == 3) {
			LL x;
			scanf("%lld", &x);
			M[x] = 1;
			query[i] = MP(-3, x);
		}
		zap++;
	}

	FOREACH(it, M)
		it -> S = skal++;
	FOREACH(it, M)
		M1[it -> S] = it -> F;
	init();

	zap = 1;
	while(zap <= q) {
		if(query[zap].F == -2) {
			dodaj(M[query[zap].S], query[zap].S);
			szprotki.insert(query[zap].S);
		}
		else if(query[zap].F == -3) {
			dodaj(M[query[zap].S], -query[zap].S);
			szprotki.erase(szprotki.find(query[zap].S) );
		}
		else
			printf("%d\n", solve(query[zap].F, query[zap].S) );
		zap++;
	}
}

int main() {
	int te = 1;
//	scanf("%d", &te);
	while(te--)
		test();
	return 0;
}