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
#include <cstdio>
#include <map>
#include <unordered_map>
#include <set>
#include <algorithm>
using lint = long long;

struct V {
	lint sum, c;
	
	V() : sum(0ll), c(0ll) {}
	V(lint sum, lint c) : sum(sum), c(c) {}
	V(const V &v, const V &u) : sum(v.sum+u.sum), c(v.c+u.c) {}
	
	void add(const V &v) {
		sum += v.sum;
		c += v.c;
	}
};

struct Tree {
	const lint d0 = (1ll << 60ll) - 1;
	const V defaultV = V();
	std::unordered_map<lint, V> container;
	
	void add(lint i, lint amount) {
		add(i, i*amount, amount);
	}
	
	void add(lint i, lint sum, lint amount) {
		lint di = d0 + i;
		container[di].add(V(sum, amount));
		update(di);
	}
	
	V sum(lint i) const {
		if(i == 0ll) return defaultV;
		lint di = d0 + i;
		V result = defaultV;
		while(di > 0ll) {
			if(di % 2 == 0) {
				result.add(findOrDefault(di));
				di = (di / 2ll) - 1ll;
			} else {
				di = (di / 2ll);
			}
		}
		return result;
	}
	
	const V& findOrDefault(lint di) const {
		auto v = container.find(di);
		return (v == container.end()) ? defaultV : v->second;
	}
	
	void update(lint di) {
		while(di > 0ll) {
			di /= 2ll;
			auto& left = findOrDefault(di * 2ll);
			auto& right = findOrDefault(di * 2ll + 1ll);
			container[di] = V(left, right);
		}
	}
};

lint attack(const Tree&, const std::multiset<lint>&, lint, lint);

int n, q;
int main() {
	Tree t;
	std::multiset<lint> W;
	
	scanf("%d", &n);
	for(int i = 0; i < n; ++i) {
		lint w;
		scanf("%lld", &w);
		t.add(w, 1);
		W.insert(w);
	}
	W.insert(1ll << 61ll);
	scanf("%d", &q);
	for(int i = 0; i < q; ++i) {
		int command;
		scanf("%d", &command);
		if(command == 1) {
			lint s, k;
			scanf("%lld%lld", &s, &k);
			printf("%lld\n", attack(t, W, s, k));
		} else {
			lint w;
			scanf("%lld", &w);
			if(command == 2) {
				t.add(w, 1ll);
				W.insert(w);
			} else {
				t.add(w, -1ll);
				W.erase(w);
			}
			
		}
	}
	
	return 0;
}

lint attack(const Tree &t, const std::multiset<lint> &W, lint s0, lint k) {
	lint result = 0ll;
	Tree dt;
	lint s = s0;
	while(s < k) {
		auto findNext = W.lower_bound(s);
		lint next = std::min(*findNext, k-1ll);
		
		auto ts = t.sum(s-1ll);
		auto dts = dt.sum(s-1ll);
		auto dd = V(ts, dts);
		//printf("(%lld, %lld) s=%lld next=%lld\n", s0,k,s,next);
		
		//printf("(%lld,%lld) %lld+(%lld+%lld) <= %lld\n",s0,k,s,ts.sum,dts.sum,next);
		if(s + (ts.sum + dts.sum) <= next) {
			return -1ll;
		}
		
		lint left = 0ll;
		lint right = s - 2ll;
		//printf("left=%lld right=%lld\n", left,right);
		while(left < right) {
			lint mid = (left + right + 1) / 2ll;
			auto tmid = t.sum(mid);
			auto dtmid = dt.sum(mid);
			if(s + (dd.sum - tmid.sum - dtmid.sum) > next) {
				left = mid;
			} else {
				right = mid - 1;
			}
		}
		
		lint i = left;
		auto ti = t.sum(i);
		auto dti = dt.sum(i);
		++i;

		//printf("raw %lld (%lld %lld %lld)\n", (dd.sum - ti.sum - dti.sum),dd.sum,ti.sum,dti.sum);
		lint over = s + (dd.sum - ti.sum - dti.sum) - (next+1);
		lint adjust = over / i;
		lint ds = (dd.sum - ti.sum - dti.sum) - (adjust * i);
		lint dc = (dd.c - ti.c - dti.c - adjust);
		
		//printf("s=%lld next=%lld over=%lld adjust=%lld ds=%lld dc=%lld\n",s,next,over,adjust,ds,dc);
		if(ds <= 0) return -1;
		result += dc;
		s += ds;
		dt.add(i, -ds, -dc);
	}
	
	return result;	
}