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
#include <algorithm>
#include <cstddef>
#include <iostream>
#include <map>
#include <utility>
#include <vector>

struct query {
	int type {};
	long long w {};
	long long k {};
};

struct eaten_range {
	std::size_t left {};
	std::size_t right {};
	int remaining_at_left {};
};

int main() {
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);
	int n;
	std::cin >> n;
	std::vector<long long> weights(n);
	for (auto&& w : weights) {
		std::cin >> w;
	}
	std::vector<long long> all_weights = weights;
	int q;
	std::cin >> q;
	std::vector<query> queries(q);
	for (auto&& c : queries) {
		std::cin >> c.type;
		if (c.type == 1) {
			std::cin >> c.w >> c.k;
		} else {
			std::cin >> c.w;
			if (c.type == 2)
				all_weights.push_back(c.w);
		}
	}
	std::sort(all_weights.begin(), all_weights.end());
	all_weights.erase(std::unique(all_weights.begin(), all_weights.end()), all_weights.end());
	auto tree_size = all_weights.size() * 2;
	while (tree_size & (tree_size - 1)) {
		tree_size &= tree_size - 1;
	}
	auto get_index = [&](long long w) {
		return std::lower_bound(all_weights.begin(), all_weights.end(), w) - all_weights.begin();
	};
	std::vector<std::pair<int, long long>> tree(tree_size * 2);
	std::map<long long, int> current_fish;
	for (const auto& w : weights) {
		current_fish[w]++;
		auto& [count, weight] = tree[tree_size + get_index(w)];
		count++;
		weight += w;
	}
	for (auto i = tree_size; --i;) {
		tree[i].first = tree[i << 1].first + tree[i << 1 | 1].first;
		tree[i].second = tree[i << 1].second + tree[i << 1 | 1].second;
	}
	auto get_range_sum = [&](std::size_t a, std::size_t b) {
		std::pair<int, long long> r;
		a += tree_size;
		b += tree_size;
		while (a < b) {
			if (a & 1) {
				r.first += tree[a].first;
				r.second += tree[a].second;
				a++;
			}
			if (b & 1) {
				b--;
				r.first += tree[b].first;
				r.second += tree[b].second;
			}
			a >>= 1;
			b >>= 1;
		}
		return r;
	};
	auto lower_bound_left = [&](std::size_t b, long long w) {
		if (w <= 0)
			return b;
		std::size_t a = b + tree_size - 1;
		while (tree[a].second < w) {
			if (~a & 1) {
				w -= tree[a].second;
				a--;
			}
			a >>= 1;
		}
		while (a < tree_size) {
			a <<= 1;
			if (tree[a | 1].second >= w)
				a |= 1;
			else
				w -= tree[a | 1].second;
		}
		return a - tree_size;
	};
	std::vector<eaten_range> eaten;
	for (const auto& c : queries) {
		switch (c.type) {
			case 1: {
				auto s = c.w;
				int r = 0;
				eaten = {{}};
				while (s < c.k) {
					long long target_weight = c.k;
					int fish_remaining_at_right = 0;
					long long weight_remaining_at_right = 0;
					std::size_t left_bound = 0;
					auto right_bound = tree_size;
					auto first_inedible = current_fish.lower_bound(s);
					if (first_inedible != current_fish.end()) {
						right_bound = get_index(first_inedible->first);
						target_weight = std::min(target_weight, first_inedible->first + 1);
					}
					for (auto i = eaten.size(); i--;) {
						left_bound = eaten[i].right;
						auto sum = get_range_sum(left_bound, right_bound);
						if (s + sum.second + weight_remaining_at_right >= target_weight)
							break;
						if (i + 1 < eaten.size()) {
							eaten[i].right = eaten.back().right;
							eaten.pop_back();
						} else {
							eaten[i].right = right_bound;
						}
						r += sum.first + fish_remaining_at_right;
						s += sum.second + weight_remaining_at_right;
						right_bound = eaten[i].left;
						fish_remaining_at_right = eaten[i].remaining_at_left;
						weight_remaining_at_right = all_weights[right_bound] * fish_remaining_at_right;
					}
					if (left_bound == right_bound && fish_remaining_at_right == 0) {
						r = -1;
						break;
					}
					std::size_t a = lower_bound_left(right_bound, target_weight - s - weight_remaining_at_right);
					auto fish = get_range_sum(a, right_bound);
					r += fish.first + fish_remaining_at_right;
					s += fish.second + weight_remaining_at_right;
					int remainder = (int)((s - target_weight) / all_weights[a]);
					r -= remainder;
					s -= all_weights[a] * remainder;
					remainder += remainder;
					eaten.emplace_back();
					eaten.back().left = a;
					eaten.back().right = right_bound;
					eaten.back().remaining_at_left = remainder;
				}
				std::cout << r << '\n';
				break;
			}
			case 2: {
				current_fish[c.w]++;
				for (auto i = tree_size + get_index(c.w); i; i >>= 1) {
					tree[i].first++;
					tree[i].second += c.w;
				}
				break;
			}
			case 3: {
				auto it = current_fish.find(c.w);
				it->second--;
				if (it->second == 0)
					current_fish.erase(it);
				for (auto i = tree_size + get_index(c.w); i; i >>= 1) {
					tree[i].first--;
					tree[i].second -= c.w;
				}
				break;
			}
		}
	}
	return 0;
}