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
#include <bits/stdc++.h>
using namespace std;
using vi=vector<int>;
using ll=long long;
#define pb push_back
#define fst first
#define snd second
using vll=vector<ll>;
using Pii=pair<int,int>;
using Pll=pair<ll,ll>;
using vpii=vector<Pii>;
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#define sim template <class c
#define mor > muu & operator<<(
#define ris return *this
#define R22(r) sim> typename enable_if<1 r sizeof(dud<c>(0)),muu&>::type operator<<(c g) {
sim> struct rge {c b, e;};
sim> rge<c> range(c i, c j) {return rge<c>{i, j};}
sim> auto dud(c*r)->decltype(cerr << *r);
sim> char dud(...);
struct muu {
	#ifdef LOCAL
	stringstream a;
	~muu() {cerr << a.str() << endl;}
	R22(<) a << boolalpha << g; ris;}
	R22(==) ris << range(begin(g), end(g));}
	sim mor rge<c> u) {
		a << "[";
		for (c i = u.b; i != u.e; ++i)
			*this << ", " + 2 * (i == u.b) << *i;
		ris << "]";
	}
	sim, class m mor pair<m,c> r) {ris << "(" << r.first << ", " << r.second << ")";}
	#else
	sim mor const c &){ris;}
	#endif
	muu & operator()(){ris;}
};
#define imie(r) "[" #r ": " << (r) << "] "
#define debug (muu() << __FUNCTION__ << "#" << __LINE__ << ": " )
typedef long long ll;
typedef pair<ll, ll> Pll;
bool persistent = true;
struct Data;
ll daj_sume(Data* n);
ll daj_wielkosc(Data* n);
struct Data {
  pair<ll, int> szprotka; // {masa, id}
  ll suma;
  int wielkosc;
  Data() = default;
  Data(Pll szp) : szprotka(szp), suma(szp.first), wielkosc(1) {}
  Data(const Data&) = default;
  bool NeedsTouch() { return false; }
  void Touch(Data* left, Data* right)  { assert(NeedsTouch()); }
  // Beware: left and right might not be touched!
  void Update(Data* left, Data* right) { 
	  assert(!NeedsTouch());
	  suma = szprotka.first + daj_sume(left) + daj_sume(right);
	  wielkosc = 1 + daj_wielkosc(left) + daj_wielkosc(right);
  }
};
ll daj_sume(Data* n)
{
	if(!n) return 0;
	return n->suma;
}
ll daj_wielkosc(Data* n)
{
	if(!n) return 0;
	return n->wielkosc;
}
struct Node;
struct N {
  int v;
  N(int v_) : v(v_) {}
  N(Node* n);
  Node* operator->() const;
  operator int() const { return v; }
};

struct Node : Data {
  N l, r;
  int h;

  template <typename ...Args>
  Node(Args&& ...args)
      : Data(forward<Args>(args)...), l(0), r(0), h(0) {}

  Node(const Node& node) = default;
  Node* ptr() { return this; }
};

ll daj_sume(N n)
{
	if(!n) return 0;
	return n->suma;
}
ll daj_wielkosc(N n)
{
	if(!n) return 0;
	return n->wielkosc;
}

constexpr int kMaxNodes = (150 * 1024 * 1024 /* 450MiB */) / sizeof(Node);
int nast_node = 1 /* 0 is reserved for the null Node */;
Node node_ptr[kMaxNodes];

template <typename ...Args> N New(Args&& ...args) {
  assert(nast_node < kMaxNodes);
  node_ptr[nast_node] = Node(forward<Args>(args)...);
  return nast_node++;
}

N::N(Node* n) : v(n - node_ptr) {}                   // Converts index to ptr.
Node* N::operator->() const { return node_ptr + v; } // Converts ptr to index.

N Touch(N n) {
  if (!n or !n->NeedsTouch()) return n;
  if (persistent) {
    n->l = New(*n->l->ptr());
    n->r = New(*n->r->ptr());
  }
  n->Touch(n->l ? n->l->ptr() : nullptr, n->r ? n->r->ptr() : nullptr);
  return n;
}

N Make(N l, N v, N r) {
  if (persistent) v = New(*v->ptr());
  v->l = l;
  v->r = r;
  v->h = max(l->h, r->h) + 1;
  assert(abs(l->h - r->h) <= 2);
  v->Update(l ? l->ptr() : nullptr, r ? r->ptr() : nullptr);
  return v;
}

N Bal(N l, N v, N r) {
  assert(abs(l->h - r->h) <= 3);
  Touch(l); Touch(r);
  if (l->h > r->h + 2) {
    N ll = l->l, lr = l->r;
    if (ll->h >= lr->h) return Make(ll, l, Make(lr, v, r));
    return Touch(lr), Make(Make(ll, l, lr->l), lr, Make(lr->r, v, r));
  } else if (r->h > l->h + 2) {
    N rr = r->r, rl = r->l;
    if (rr->h >= rl->h) return Make(Make(l, v, rl), r, rr);
    return Touch(rl), Make(Make(l, v, rl->l), rl, Make(rl->r, r, rr));
  } else {
    return Make(l, v, r);
  }
}

N AddLeft(N n, N v) {
  if (!Touch(n)) return Make(0, v, 0);
  return Bal(AddLeft(n->l, v), n, n->r);
}

N AddRight(N n, N v) {
  if (!Touch(n)) return Make(0, v, 0);
  return Bal(n->l, n, AddRight(n->r, v));
}

pair<N, N> RemLeft(N n) {
  if (!Touch(n)->l) return {n->r, n};
  auto s = RemLeft(n->l);
  return {Bal(s.first, n, n->r), s.second};
}

// Joins l, r - trees, v - single vertex that will be overridden even
// in the case of a persistent tree.
N Join(N l, N v, N r) {
  if (!Touch(l)) return AddLeft(r, v);
  if (!Touch(r)) return AddRight(l, v);
  if (l->h > r->h + 2) return Bal(l->l, l, Join(l->r, v, r));
  if (r->h > l->h + 2) return Bal(Join(l, v, r->l), r, r->r);
  return Make(l, v, r);
}

N Merge(N l, N r) {
  if (!l or !r) return r + l;
  auto s = RemLeft(r);
  return Join(l, s.second, s.first);
}

ll daj_min(N n)
{
	if(!n) return 1e18;
	if(!n->l) return n->szprotka.first;
	return daj_min(n->l);
}

//~ pair<N, N> Split(N n, /* Maybe some additional arguments. */) {
  //~ if (!Touch(n)) return {0, 0};
  //~ if (/* Condition for checking if n belongs to the left tree. */) {
    //~ auto s = Split(n->r, /* Some args. */);
    //~ return {Join(n->l, n, s.first), s.second};
  //~ } else /* n belongs to the right tree. */ {
    //~ auto s = Split(n->l, /* Some args. */);
    //~ return {s.first, Join(s.second, n, n->r)};
  //~ }
//~ }
// lewe zawiera mniejsze, a prawe większe lub równe
pair<N, N> Split(N n, pair<ll, int> szp) {
  if (!Touch(n)) return {0, 0};
  if (szp > n->szprotka) {
    auto s = Split(n->r, szp);
    return {Join(n->l, n, s.first), s.second};
  } else /* n belongs to the right tree. */ {
    auto s = Split(n->l, szp);
    return {s.first, Join(s.second, n, n->r)};
  }
}
pair<N, N> Split_walk(N n, ll left) {
  if (!Touch(n)) return {0, 0};
  if (daj_sume(n->r) >= left) {
    auto s = Split_walk(n->r, left);
    return {Join(n->l, n, s.first), s.second};
  } else /* n belongs to the right tree. */ {
    auto s = Split_walk(n->l, left - n->suma - daj_sume(n->r));
    return {s.first, Join(s.second, n, n->r)};
  }
}
N wrzuc(N n, Pll szp)
{
	auto s = Split(n, szp);
	return Join(s.first, New(szp), s.second);
}
N wrzucaj(const vector<Pll>& sprs)
{
	if(sprs.empty()) return 0;
	int div = sprs.size() / 2;
	vector<Pll> left, right;
	for(int i = 0; i < div; ++i)
		left.push_back(sprs[i]);
	for(int i = div + 1; i < (int)sprs.size(); ++i)
		right.push_back(sprs[i]);
	N le = wrzucaj(left);
	N ri = wrzucaj(right);
	return Join(le, New(sprs[div]), ri);
}
map<ll, vector<int>> Map;
int main()
{
	int n;
	scanf("%d", &n);
	vector<Pll> szprotki;
	for(int i = 0; i < n; ++i)
	{
		ll w;
		scanf("%lld", &w);
		Map[w].push_back(i);
		Pll szp = {w, i};
		szprotki.push_back(szp);
		//avl = wrzuc(avl, szp);
	}
	sort(szprotki.begin(), szprotki.end());
	N avl = wrzucaj(szprotki);
	debug() << imie(nast_node) << imie(kMaxNodes);
	int q;
	scanf("%d", &q);
	for(int i = 0; i < q; ++i)
	{
		int type;
		scanf("%d", &type);
		if(type == 1)
		{
			ll s, k;
			scanf("%lld%lld", &s, &k);
			int zjadlem = 0;
			int n_node = nast_node;
			auto spl = Split(avl, {s, -1});
			auto mozna_zjesc = spl.first;
			auto za_duze = spl.second;
			while(s < k)
			{
				ll goal = min(k, daj_min(za_duze) + 1);
				auto s2 = Split_walk(mozna_zjesc, goal - s);
				if(!s2.second)
				{
					break;
				}
				s += daj_sume(s2.second);
				zjadlem += daj_wielkosc(s2.second);
				auto s3 = Split(za_duze, {s, -1});
				za_duze = s3.second;
				mozna_zjesc = Merge(s2.first, s3.first);
			}
			if(s < k)
				puts("-1");
			else
				printf("%d\n", zjadlem);
			nast_node = n_node;
		}
		else {
			ll w;
			scanf("%lld", &w);
			persistent = false;
			if(type == 2) {
				Map[w].push_back(i + n);
				Pll szp = {w, i + n};
				avl = wrzuc(avl, szp);
			} else if(type == 3) {
				Pll szp = {w, Map[w].back()};
				Map[w].pop_back();
				auto s = Split(avl, szp);
				auto rem = RemLeft(s.second);
				avl = Merge(s.first, rem.first);
			}
			persistent = true;
		}
		//debug() << daj_sume(avl) << " " << daj_wielkosc(avl);
	}
	debug() << imie(nast_node) << imie(kMaxNodes);
}