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
#undef _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
 
ostream& operator<<(ostream &out, string str) {
	for(char c : str) out << c;
	return out;
}
 
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
	return out << "(" << p.first << ", " << p.second << ")";
}
 
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
	out << "{";
	for(auto it = a.begin(); it != a.end(); it = next(it))
		out << (it != a.begin() ? ", " : "") << *it;
	return out << "}";
}
 
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
	cerr << a << ", ";
	dump(x...);
}
 
#ifdef DEBUG
#  define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#  define debug(...) false
#endif
 
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
 
template<class T> int size(T && a) { return a.size(); }
 
using LL = long long;
using PII = pair<int, int>;

struct Node {
	bool active = true;
	int lazy = -1, size = 1;
	LL _sum = 0, sum = 0;
	LL _mn = 1e18, mn = 1e18;
	int _cnt = 0, cnt = 0;
};

ostream& operator<<(ostream &ost, Node &n) {
	return ost << "act: " << n.active 
		<< " sum: " << n.sum << " " << n._sum
		<< " mn: " << n.mn << " " << n._mn
		<< " cnt: " << n.cnt << " " << n._cnt
		<< " lazy: " << n.lazy;
}

struct Tree {
	vector<Node> tree;
	int size = 1;

	Tree(vector<pair<LL, int>> w) {
		int n = w.size();
		while(size == 1 || size < n) size *= 2;
		tree.resize(size * 2);

		FOR(i, size, size + (int)w.size() - 1) {
			tree[i]._sum = w[i - size].ST;
			tree[i]._mn = w[i - size].ST;
		}
		FOR(i, size, size * 2 - 1) {
			tree[i].active = false;
			tree[i]._cnt = 1;
		}
		for(int i = size - 1; i >= 1; i--) {
			tree[i].size = tree[i * 2].size * 2;
		}
	}

	void merge(int v) {
		tree[v].mn = tree[v]._mn = 1e18;
		tree[v].sum = tree[v]._sum = 0;
		tree[v].cnt = tree[v]._cnt = 0;
		for(int i : {v * 2, v * 2 + 1}) {
			if(!tree[i].active) continue;
			tree[v].sum += tree[i].sum;
			tree[v]._sum += tree[i]._sum;
			tree[v].mn = min(tree[v].mn, tree[i].mn);
			tree[v]._mn = min(tree[v]._mn, tree[i]._mn);
			tree[v].cnt += tree[i].cnt;
			tree[v]._cnt += tree[i]._cnt;
		}
	}

	void edit(int v, bool val) {
		tree[v].sum = val ? tree[v]._sum : 0;
		tree[v].mn = val ? tree[v]._mn : 1e18;
		tree[v].cnt = val ? tree[v]._cnt : 0;
	}

	void add_val(int v, int val) {
		if(val != -1) {
			if(val && !tree[v].active) return;
			edit(v, val);
			tree[v].lazy = val;
		}
	}

	void propagate(int v) {
		REP(i, 2)
			add_val(v * 2 + i, tree[v].lazy);
		tree[v].lazy = -1;
	}

	void add(int l, int r, int val, int v = 1) {
		if(l == 0 && r == tree[v].size - 1) {
			add_val(v, val);
			return;
		}
		propagate(v);
		int m = tree[v].size / 2;
		if(r < m)
			add(l, r, val, v * 2);
		else if(m <= l)
			add(l - m, r - m, val, v * 2 + 1);
		else
			add(l, m - 1, val, v * 2), add(0, r - m, val, v * 2 + 1);

		merge(v);
	}

	LL query(int l, int r, int v = 1) {
		if(l == 0 && r == tree[v].size - 1)
			return tree[v].sum;
		propagate(v);
		int m = tree[v].size / 2;
		if(r < m)
			return query(l, r, v * 2);
		else if(m <= l)
			return query(l - m, r - m, v * 2 + 1);
		else
			return query(l, m - 1, v * 2) + query(0, r - m, v * 2 + 1);
	}

	void prepare() { add(0, size - 1, true); }

	void flag(int pos, bool act, int v = 1) {
		if(tree[v].size == 1) {
			tree[v].active = act;
			edit(v, act);
			return;
		}
		propagate(v);
		int m = tree[v].size / 2;
		if(pos < m)
			flag(pos, act, v * 2);
		else
			flag(pos - m, act, v * 2 + 1);

		merge(v);
	}

	// znajduje największe x, że w[x] < s
	int find_val(LL val, int v = 1) { // opt : można sklepać iteracyjnie
		if(tree[v].mn >= val) return -1;
		if(tree[v].size == 1) return v - size;
		propagate(v);
		for(int i : {v * 2 + 1, v * 2}) {
			int res = find_val(val, i);
			if(res != -1) return res;
		}
		return -1;
	}

	// znajduje najmniejsze x, że val <= pref[x]
	int find_sum(LL val, int v = 1) {
		if(tree[v].size == 1) return v - size;
		propagate(v);
		for(int i : {v * 2, v * 2 + 1}) {
			if(val <= tree[i].sum) 
				return find_sum(val, i);
			else
				val -= tree[i].sum;
		}
		return -1;
	}

	// znajduje następnego x po pos, że jest aktywne
	int find_next_active(int pos) {
		pos += size;
		while(true) {
			if(pos % 2 == 0) {
				if(tree[pos + 1]._cnt && tree[pos + 1].active) {
					pos++;
					break;
				}
			}
			pos /= 2;
			if(pos == 1) return -1;
		}

		while(tree[pos].size != 1) {
			if(tree[pos * 2]._cnt && tree[pos * 2].active) pos *= 2;
			else pos = pos * 2 + 1;
		}
		return pos - size;
	}

	int get_ans() {
		return tree[1]._cnt - tree[1].cnt;
	}

	void print() {
		FOR(i, 1, size - 1) propagate(i);
		FOR(i, 1, 2 * size - 1)
			debug(i, tree[i]);
	}
};

#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

mt19937_64 rng(2137);
struct chash {
	const uint64_t C = LL(2e18 * M_PI) + 69;
	const int RANDOM = rng();
	size_t operator()(uint64_t x) const {
		return __builtin_bswap64((x^RANDOM) * C);
	}
};
template<class L, class R> 
using hash_map = gp_hash_table<L, R, chash>;

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;

	hash_map<LL, int> m1, m2;
	vector<pair<LL, int>> w(n), all(n);
	REP(i, n) {
		LL W; cin >> W;
		w[i] = {W, m1[W]++};
		all[i] = w[i];
	}

	int q;
	cin >> q;
	vector<LL> t(q), s(q), k(q);
	vector<pair<LL, int>> d(q);

	REP(i, q) {
		cin >> t[i];
		if(t[i] == 1) cin >> s[i] >> k[i];
		else cin >> d[i].ST;
		if(t[i] == 2) {
			d[i].ND = m1[d[i].ST]++;
			all.emplace_back(d[i]);
		}
		if(t[i] == 3) {
			d[i].ND = m2[d[i].ST]++;
		}
	}

	sort(all.begin(), all.end());

	auto scale = [&](pair<LL, int> p) {
		auto it = lower_bound(all.begin(), all.end(), p);
		return distance(all.begin(), it);
	};

	Tree tree(all);
	for(auto &p : w) {
		int pos = scale(p);
		tree.flag(pos, true);
	}

	REP(i, q) {
		if(t[i] == 1) {
			tree.prepare();

			bool ok = true;
			while(s[i] < k[i]) {
				auto check = [&](LL t) { if(t < 0) ok = false; return !ok; };
				int pos = tree.find_val(s[i]);
				if(check(pos)) break;

				int nxt = tree.find_next_active(pos);
				LL threshold = nxt == -1 ? k[i] : min(k[i], all[nxt].ST + 1);
				LL val = s[i] + tree.query(0, pos) - threshold + 1;
				if(check(val - 1)) break;

				int x = tree.find_sum(val);
				s[i] += tree.query(x, pos);
				tree.add(x, pos, false);
			}

			if(ok) cout << tree.get_ans() << "\n";
			else cout << "-1\n";
		}
		else if(t[i] == 2) {
			int pos = scale(d[i]);
			tree.flag(pos, true);
		}
		else if(t[i] == 3) {
			int pos = scale(d[i]);
			tree.flag(pos, false);
		}
	}
}