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
#include <bits/stdc++.h>
#define PB push_back
#define ST first
#define ND second
#define _ ios_base::sync_with_stdio(0); cin.tie(0);
//mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

using namespace std;

using ll = long long;
using pi = pair<int,int>;
using vi = vector<int>;

struct Query {
	int typ;
	ll a,b;
};

const int nax = 300*1000+10,qax = 100*1000+10,B=400,INF=1e9;
int n,q;
int nr,cntU;
ll val[nax+qax];
ll delta[nax+qax];
int pre[nax+qax],delta2[nax+qax];
pair<ll,int> all[nax+qax];
pair<ll,int> usun[qax];
Query qr[qax];
multiset<pair<ll,int> >S;

int bck(int x) {
	return x/B;
}

void update(int x,ll y) {
	int b = bck(x);
	for(int i=x; i<=nr&&bck(i)==b; i++) {
		val[i]+=y;
	}
	for(int i=b+1; i<=bck(nr); i++) delta[i]+=y;
}

void update2(int x,int y) {
	int b = bck(x);
	for(int i=x; i<=nr&&bck(i)==b; i++) {
		pre[i]+=y;
	}
	for(int i=b+1; i<=bck(nr); i++) delta2[i]+=y;
}

ll get_val(int x) {
	return val[x]+delta[bck(x)];
}

int get_pre(int x) {
	return pre[x]+delta2[x];
}

bool check(int a,int b,ll x) {
	return get_val(b)-get_val(a-1)>=x;
}

int main() {_
	cin>>n;
	for(int i=0; i<n; i++) {
		ll x;
		cin>>x;
		all[i] = {x,0};
	}
	nr = n;
	cin>>q;
	for(int i=1; i<=q; i++) {
		cin>>qr[i].typ;
		if(qr[i].typ==1) {
			cin>>qr[i].a>>qr[i].b;
		} else if(qr[i].typ==2) {
			cin>>qr[i].a;
			all[nr++] = {qr[i].a,i};
		} else {
			cin>>qr[i].a;
			usun[cntU++] = {qr[i].a,i};
		}
	}
	sort(all,all+nr);
	sort(usun,usun+cntU);
	int wsk = 0;
	for(int i=0; i<nr; i++) {
		val[i+1] = val[i];
		pre[i+1] = pre[i];
		if(all[i].ND==0) {
			S.insert({all[i].ST,i+1});
			val[i+1]+=all[i].ST;
			pre[i+1]++;
		} else {
			qr[all[i].ND].b = i+1;
		}
		if(wsk<cntU&&usun[wsk].ST==all[i].ST) {
			qr[usun[wsk].ND].b = i+1;
			wsk++;
		}
	}
	for(int i=1; i<=q; i++) {
		if(qr[i].typ==2) {
			update(qr[i].b,qr[i].a);
			update2(qr[i].b,1);
			S.insert({qr[i].a,qr[i].b});
		} else if(qr[i].typ==3) {
			update(qr[i].b,-qr[i].a);
			update2(qr[i].b,-1);
			S.erase({qr[i].a,qr[i].b});
		} else {
			if(qr[i].a>=qr[i].b) {
				cout<<"0\n";
				continue;
			}
			ll w = qr[i].a;
			int ans = 0,pop=1;
			bool ok=1;
			vector<pi>seg;
			while(w<qr[i].b) {
				auto x = S.lower_bound({w,0});
				if(x==S.begin()) {
					ok=0;
					break;
				}
				x--;
				if(pop-1<(*x).ND) {
					w+=(*x).ST;
					seg.PB({pop,(*x).ND-1});
					pop = (*x).ND+1;
					ans++;
				} else {
					x++;
					ll brak = qr[i].b-1;
					if(x!=S.end()) {
						brak = min(brak,(*x).ST);
					}
					brak +=-w+1;
					while(!seg.empty()&&(get_val(seg.back().ND)-get_val(seg.back().ST-1))<brak) {
						brak-=(get_val(seg.back().ND)-get_val(seg.back().ST-1));
						w+=(get_val(seg.back().ND)-get_val(seg.back().ST-1));
						ans+=get_pre(seg.back().ND)-get_pre(seg.back().ST-1);
						seg.pop_back();
					}
					if(!seg.empty()) {
						int l=seg.back().ST,r=seg.back().ND,mid;
						while(l<=r) {
							mid=(l+r)/2;
							if(check(mid,seg.back().ND,brak)) l=mid+1;
							else r=mid-1;
						}
						l--;
						w+=get_val(seg.back().ND)-get_val(l-1);
						brak-=get_val(seg.back().ND)-get_val(l-1);
						ans+=get_pre(seg.back().ND)-get_pre(l-1);
						//cout<<seg.back().ND<<" "<<l<<"\n";
						seg.back().ND = l-1;
						if(seg.back().ND<seg.back().ST) seg.pop_back();
					}
					if(brak>0) {
						ok=0;
						break;
					}
				}
			}
			if(!ok) cout<<"-1\n";
			else cout<<ans<<"\n";	
				
		}
	}
	
	
	
}