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
#define make_pair mp
#define emplace_back pb
#include <bits/stdc++.h>
using namespace std;
mt19937 mt_rand(time(0));
const int N = 3e5 + 5, Q = 1e5 + 5, INF = 1e6;
const int R = 400;
int n, q, m, con[N+Q];
long long tab[N+Q], w[N+Q], pref[N+Q], tab2[N];
map<long long, int> cnt;

struct zap {
	int type;
	long long x, y;
};
zap quer[Q];

struct ans {
	int p;
	int cnt;
	long long sum;
};

set< pair<int, int> > holes;
set< pair<int, int> > holes_copy;
set<int> positions;

void count_pref() {
	holes.clear();
	holes.insert(mp(0, 0));
	for(int i=1;i<=m;i++) pref[i] = pref[i-1] + w[i];
	for(int i=1;i<=m;i++) con[i] = con[i-1] + (int)(w[i] != 0);
}

int bin_tab(int p, int k, long long val) {
	if(p >= k) return p;
	int mid = (p + k) / 2;
	if(tab[mid] >= val) return bin_tab(p, mid, val);
	return bin_tab(mid + 1, k, val);
}

long long suma(int x, int y) {
	if(x > y) return 0;
	return pref[y] - pref[x-1];
}

ans bin_pref(int p, int k, int ost, long long val) {
	if(p == k) return {p-1, con[ost] - con[p-2], suma(p-1, ost)};
	int mid = (p + k) / 2;
	if(suma(mid, ost) >= val) return bin_pref(mid + 1, k, ost, val);
        return bin_pref(p, mid, ost, val);
}

ans find(int ost, long long val) {
	if(ost == 0 || ost == m + 1) return {-1, -1, -1};
	auto it = holes_copy.upper_bound(mp(ost, INF));
	it--;

	if(it -> first == ost && it -> second != ost) return find(it -> second, val);

	if(it -> second == ost) {
		int fir = it -> first;
		if(w[fir] >= val) return {fir, 1, w[fir]};
		val -= w[fir];
		ans next = find(ost - 1, val);
		if(next.cnt == -1) return {-1, -1, -1};
		return {next.p, 1 + next.cnt, next.sum + w[ost]};
	}
	else {
		int fir = it -> first + 1;
		long long sum = suma(fir, ost);
		if(sum >= val) {
			ans next;
			if(w[ost] >= val) next = {ost, 1, w[ost]};
			else next = bin_pref(fir + 1, ost, ost, val);
			return next;
		}
		val -= sum;
		ans next = find(it -> second, val);
		if(next.cnt == -1) return {-1, -1, -1};
		return {next.p, next.cnt + con[ost] - con[fir - 1], next.sum + sum};
	}
}

void add_hole(int x, int y) {
	auto it = holes.lower_bound(mp(x, 0));
	if(it != holes.end() && it -> first == x) holes.erase(it);
	holes.insert(mp(x, y));
}

void add_hole_copy(int x, int y) {
	auto it = holes_copy.lower_bound(mp(x, 0));
	if(it != holes_copy.end() && it -> first == x) holes_copy.erase(it);
	holes_copy.insert(mp(x, y));
}

int make_query(long long k, long long s) {
	if(k >= s) return 0;
	int fir = bin_tab(1, m + 1, k);
	auto it = positions.lower_bound(fir);
	if(it == positions.begin()) return -1;
	fir = *it;
	long long daz = min(w[fir] + 1, s);
	it--;
	fir = *it;
	ans next = find(fir, daz - k);
	add_hole_copy(fir, next.p - 1);
	k += next.sum;
	if(k >= s || next.cnt == -1) return next.cnt;
	int nxt = make_query(k, s);
	if(nxt == -1) return -1;
	return next.cnt + nxt;
}

int odp(long long k, long long s) {
	holes_copy = holes;
	return make_query(k, s);
}

int main()
{
	scanf("%d", &n);
	for(int i=1;i<=n;i++) {
		scanf("%lld", &tab2[i]);
		cnt[tab2[i]]++;
	}

	m = n;
	scanf("%d", &q);
	for(int i=1;i<=q;i++) {
		int typ;
		long long a, b = 0;
		scanf("%d%lld", &typ, &a);
		if(typ == 1) scanf("%lld", &b);
		else if(typ == 2) {
		       	cnt[a]++; 
			m++;
		}
		quer[i] = {typ, a, b};	
	}

	int sum = 1;
	for(auto &x : cnt) {
		for(int i=sum;i<sum+x.second;i++) tab[i] = x.first;
		sum += x.second;
		x.second = sum - x.second;
	}

	for(int i=1;i<=n;i++) {
		w[cnt[tab2[i]]] = tab2[i];
		cnt[tab2[i]]++;
	}

	tab[m+1] = 4e18;
	w[m+1] = 4e18;
	positions.insert(m+1);

	for(int i=1;i<=m;i++) if(w[i]) positions.insert(i);

	count_pref();

	int wsk = 0;
	for(int i=1;i<=q;i++) {
		int typ = quer[i].type;
		long long a = quer[i].x;
		long long b = quer[i].y;

		if(typ == 2) {
			int id = cnt[a];
			cnt[a]++;
			w[id] = a;
			add_hole(id, id);
			positions.insert(id);
			wsk++;
		}
		else if(typ == 3) {
			cnt[a]--;
			int id = cnt[a];
			w[id] = 0;
			add_hole(id, id - 1);
			positions.erase(id);
			wsk++;
		}
		else {
			printf("%d\n", odp(a, b));
		}
		if(wsk % R == 0) count_pref();
		/*for(int i=1;i<=m;i++) cout<<w[i]<<" ";
		cout<<endl;*/
	}
	return 0;
}