#include <assert.h> #include <algorithm> #include <cstdio> #include <set> #include <utility> using namespace std; typedef pair<int, int> PII; const int MAXT = 1 << 20; const int MAXQ = 100'000; const int MAXN = 300'000 + MAXQ; struct T { long long sum; int count; int left, right; T() {} T(long long sum) : sum(sum), left(-1), right(-1) {} T(long long sum, bool count, int idx) : sum(sum), count(count), left(idx), right(idx) {} int idx() const { assert(left == right); return left; } }; bool operator<(const T& l, const T& r) { if (l.sum != r.sum) return l.sum < r.sum; return l.idx() < r.idx(); } enum Op { QUERY, ADD, REMOVE }; Op op_from_code(int code) { if (code == 1) return QUERY; else if (code == 2) return ADD; else if (code == 3) return REMOVE; else assert(false); return QUERY; } struct E { Op op; long long a1; long long a2; }; int n, q; int cap; E e[MAXQ]; T t[MAXT]; long long get_sum(int i) { if (t[i].left == t[i].right && t[i].count == 0) return 0; return t[i].sum; } void build(int i) { assert(0 < i && i < cap); t[i].left = t[2 * i].left; t[i].right = t[2 * i + 1].right; t[i].sum = get_sum(2 * i) + get_sum(2 * i + 1); t[i].count = t[2 * i].count + t[2 * i + 1].count; } void update(int i) { if (i == 0) return; if (i < cap) build(i); update(i / 2); } long long get_count(int i, int left, int right) { if (i > cap + n) { return 0; } if (left <= t[i].left && t[i].right <= right) { return t[i].count; } if (t[i].right < left || right < t[i].left) { return 0; } return get_count(2 * i, left, right) + get_count(2 * i + 1, left, right); } long long get_count(int left, int right) { return get_count(1, left, right); } pair<int, long long> get_jump(int i, long long target, int left, int right) { if (i > cap + n) { return pair(left, 0); } if (t[i].right < left || right < t[i].left) { return pair(left, 0); } if (left <= t[i].left && t[i].right <= right && get_sum(i) <= target) { return pair(t[i].left, get_sum(i)); } if (t[i].left == t[i].right) { return pair(t[i].left, get_sum(i)); } auto right_res = get_jump(2 * i + 1, target, left, right); if (right_res.second >= target) { return right_res; }; auto left_res = get_jump(2 * i, target - right_res.second, left, right); if (left_res.second == 0) { return right_res; } left_res.second += right_res.second; return left_res; } pair<int, long long> get_jump(long long target, int left, int right) { return get_jump(1, target, left, right); } T& a(int i) { return t[i + cap]; } set<T> enabled, disabled; set<PII>::const_iterator merge(set<PII>& eaten, set<PII>::const_iterator it) { auto next = it; next++; if (next != eaten.end()) { assert(it->first < it->second); assert(next->first < next->second); assert(it->second <= next->first); if (it->second == next->first) { auto p = pair(it->first, next->second); eaten.erase(it); eaten.erase(next); auto res = eaten.insert(p); assert(res.second); it = res.first; } } return it; } void eat(set<PII>& eaten, PII p) { auto res = eaten.insert(p); assert(res.second); auto it = res.first; it = merge(eaten, it); if (it != eaten.begin()) { it--; merge(eaten, it); } } int query(long long s, long long k) { int res = 0; set<PII> eaten; eaten.insert(pair(-1, 0)); while (s < k) { auto next = enabled.lower_bound(T(s)); assert(next == enabled.end() || next->sum >= s); int right = n; if (next != enabled.end()) { right = next->idx(); } long long need = k - s; if (next != enabled.end()) { if (s > next->sum) { eat(eaten, pair(next->idx(), next->idx() + 1)); s += next->sum; res++; next++; continue; } need = min(need, next->sum - s + 1); } assert(need > 0); long long jump = 0; int left = right; auto bound = eaten.lower_bound(pair(right, -1)); assert(bound != eaten.begin()); bound--; while (jump < need && left > 0) { assert(bound->second < right && bound->second <= left); auto jump_to = get_jump(need - jump, bound->second, right - 1); left = jump_to.first; jump += jump_to.second; res += get_count(left, right - 1); auto p = pair(left, right); assert(left < right); right = bound->first; left = right; bound--; eat(eaten, p); } if (jump < need) { return -1; } s += jump; } return res; } void add(long long x) { assert(x > 0); auto it = disabled.lower_bound(T(x)); assert(it != disabled.end()); assert(a(it->idx()).sum == x); a(it->idx()).count = 1; update(cap + it->idx()); enabled.insert(a(it->idx())); disabled.erase(it); } void remove(long long x) { assert(x > 0); auto it = enabled.lower_bound(T(x)); assert(it != enabled.end()); assert(a(it->idx()).sum == x); a(it->idx()).count = 0; update(cap + it->idx()); disabled.insert(a(it->idx())); enabled.erase(it); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &t[i].sum); t[i].count = 1; } scanf("%d", &q); for (int i = 0; i < q; i++) { int op_code; long long a1, a2 = -1; scanf("%d %lld", &op_code, &a1); Op op = op_from_code(op_code); if (op == QUERY) { scanf("%lld", &a2); } e[i].op = op; e[i].a1 = a1; e[i].a2 = a2; } for (int i = 0; i < q; i++) { if (e[i].op == ADD) { t[n].sum = e[i].a1; t[n].count = 0; n++; } } sort(t, t + n); for (int i = 0; i < n; i++) { a(i).left = a(i).right = i; if (a(i).count) { enabled.insert(a(i)); } else { disabled.insert(a(i)); } } cap = 1; while (cap < n) cap *= 2; for (int i = n - 1; i >= 0; i--) t[i + cap] = t[i]; for (int i = cap + n; i < 2 * cap; i++) { t[i].left = t[i].right = MAXN; t[i].count = t[i].sum = 0; } for (int i = cap - 1; i > 0; i--) build(i); for (int i = 0; i < q; i++) { if (e[i].op == QUERY) { printf("%d\n", query(e[i].a1, e[i].a2)); } else if (e[i].op == ADD) { add(e[i].a1); } else if (e[i].op == REMOVE) { remove(e[i].a1); } else { assert(false); } } }
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 | #include <assert.h> #include <algorithm> #include <cstdio> #include <set> #include <utility> using namespace std; typedef pair<int, int> PII; const int MAXT = 1 << 20; const int MAXQ = 100'000; const int MAXN = 300'000 + MAXQ; struct T { long long sum; int count; int left, right; T() {} T(long long sum) : sum(sum), left(-1), right(-1) {} T(long long sum, bool count, int idx) : sum(sum), count(count), left(idx), right(idx) {} int idx() const { assert(left == right); return left; } }; bool operator<(const T& l, const T& r) { if (l.sum != r.sum) return l.sum < r.sum; return l.idx() < r.idx(); } enum Op { QUERY, ADD, REMOVE }; Op op_from_code(int code) { if (code == 1) return QUERY; else if (code == 2) return ADD; else if (code == 3) return REMOVE; else assert(false); return QUERY; } struct E { Op op; long long a1; long long a2; }; int n, q; int cap; E e[MAXQ]; T t[MAXT]; long long get_sum(int i) { if (t[i].left == t[i].right && t[i].count == 0) return 0; return t[i].sum; } void build(int i) { assert(0 < i && i < cap); t[i].left = t[2 * i].left; t[i].right = t[2 * i + 1].right; t[i].sum = get_sum(2 * i) + get_sum(2 * i + 1); t[i].count = t[2 * i].count + t[2 * i + 1].count; } void update(int i) { if (i == 0) return; if (i < cap) build(i); update(i / 2); } long long get_count(int i, int left, int right) { if (i > cap + n) { return 0; } if (left <= t[i].left && t[i].right <= right) { return t[i].count; } if (t[i].right < left || right < t[i].left) { return 0; } return get_count(2 * i, left, right) + get_count(2 * i + 1, left, right); } long long get_count(int left, int right) { return get_count(1, left, right); } pair<int, long long> get_jump(int i, long long target, int left, int right) { if (i > cap + n) { return pair(left, 0); } if (t[i].right < left || right < t[i].left) { return pair(left, 0); } if (left <= t[i].left && t[i].right <= right && get_sum(i) <= target) { return pair(t[i].left, get_sum(i)); } if (t[i].left == t[i].right) { return pair(t[i].left, get_sum(i)); } auto right_res = get_jump(2 * i + 1, target, left, right); if (right_res.second >= target) { return right_res; }; auto left_res = get_jump(2 * i, target - right_res.second, left, right); if (left_res.second == 0) { return right_res; } left_res.second += right_res.second; return left_res; } pair<int, long long> get_jump(long long target, int left, int right) { return get_jump(1, target, left, right); } T& a(int i) { return t[i + cap]; } set<T> enabled, disabled; set<PII>::const_iterator merge(set<PII>& eaten, set<PII>::const_iterator it) { auto next = it; next++; if (next != eaten.end()) { assert(it->first < it->second); assert(next->first < next->second); assert(it->second <= next->first); if (it->second == next->first) { auto p = pair(it->first, next->second); eaten.erase(it); eaten.erase(next); auto res = eaten.insert(p); assert(res.second); it = res.first; } } return it; } void eat(set<PII>& eaten, PII p) { auto res = eaten.insert(p); assert(res.second); auto it = res.first; it = merge(eaten, it); if (it != eaten.begin()) { it--; merge(eaten, it); } } int query(long long s, long long k) { int res = 0; set<PII> eaten; eaten.insert(pair(-1, 0)); while (s < k) { auto next = enabled.lower_bound(T(s)); assert(next == enabled.end() || next->sum >= s); int right = n; if (next != enabled.end()) { right = next->idx(); } long long need = k - s; if (next != enabled.end()) { if (s > next->sum) { eat(eaten, pair(next->idx(), next->idx() + 1)); s += next->sum; res++; next++; continue; } need = min(need, next->sum - s + 1); } assert(need > 0); long long jump = 0; int left = right; auto bound = eaten.lower_bound(pair(right, -1)); assert(bound != eaten.begin()); bound--; while (jump < need && left > 0) { assert(bound->second < right && bound->second <= left); auto jump_to = get_jump(need - jump, bound->second, right - 1); left = jump_to.first; jump += jump_to.second; res += get_count(left, right - 1); auto p = pair(left, right); assert(left < right); right = bound->first; left = right; bound--; eat(eaten, p); } if (jump < need) { return -1; } s += jump; } return res; } void add(long long x) { assert(x > 0); auto it = disabled.lower_bound(T(x)); assert(it != disabled.end()); assert(a(it->idx()).sum == x); a(it->idx()).count = 1; update(cap + it->idx()); enabled.insert(a(it->idx())); disabled.erase(it); } void remove(long long x) { assert(x > 0); auto it = enabled.lower_bound(T(x)); assert(it != enabled.end()); assert(a(it->idx()).sum == x); a(it->idx()).count = 0; update(cap + it->idx()); disabled.insert(a(it->idx())); enabled.erase(it); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &t[i].sum); t[i].count = 1; } scanf("%d", &q); for (int i = 0; i < q; i++) { int op_code; long long a1, a2 = -1; scanf("%d %lld", &op_code, &a1); Op op = op_from_code(op_code); if (op == QUERY) { scanf("%lld", &a2); } e[i].op = op; e[i].a1 = a1; e[i].a2 = a2; } for (int i = 0; i < q; i++) { if (e[i].op == ADD) { t[n].sum = e[i].a1; t[n].count = 0; n++; } } sort(t, t + n); for (int i = 0; i < n; i++) { a(i).left = a(i).right = i; if (a(i).count) { enabled.insert(a(i)); } else { disabled.insert(a(i)); } } cap = 1; while (cap < n) cap *= 2; for (int i = n - 1; i >= 0; i--) t[i + cap] = t[i]; for (int i = cap + n; i < 2 * cap; i++) { t[i].left = t[i].right = MAXN; t[i].count = t[i].sum = 0; } for (int i = cap - 1; i > 0; i--) build(i); for (int i = 0; i < q; i++) { if (e[i].op == QUERY) { printf("%d\n", query(e[i].a1, e[i].a2)); } else if (e[i].op == ADD) { add(e[i].a1); } else if (e[i].op == REMOVE) { remove(e[i].a1); } else { assert(false); } } } |