#include <bits/stdc++.h> using namespace std; using ll = long long int; struct query { int id; ll a; ll b; query(int id, ll a, ll b) : id(id), a(a), b(b) {} }; class segtree { int pot; ll *tsum; int *ibeg; int *iend; bool *flag; struct change{ int arr; int index; ll value; change(int arr, int index, ll value) : arr(arr), index(index), value(value) {}; }; vector<change> history; ll _sum(int a, int b, int v) { if (flag[v]) return 0; if (a <= ibeg[v] && iend[v] <= b) return tsum[v]; if (a > iend[v] || b < ibeg[v]) return 0; return _sum(a, b, v << 1) + _sum(a, b, (v << 1) + 1); } void _clear(int a, int b, int v) { if (a <= ibeg[v] && iend[v] <= b) { history.push_back(change(0, v, tsum[v])); history.push_back(change(1, v, flag[v])); flag[v] = true; tsum[v] = 0; return; } if (a > iend[v] || b < ibeg[v]) return; _clear(a, b, v << 1); _clear(a, b, (v << 1) + 1); history.push_back(change(0, v, tsum[v])); tsum[v] = tsum[v << 1] + tsum[(v << 1) + 1]; } public: segtree(int n) { pot = 1; while (pot <= n) pot <<= 1; tsum = new ll[2*pot]; flag = new bool[2*pot]; ibeg = new int[2*pot]; iend = new int[2*pot]; for (int i = 1; i < 2 * pot; i++) { tsum[i] = 0; flag[i] = false; } for (int i = pot; i < 2 * pot; i++) { ibeg[i] = i - pot + 1; iend[i] = i - pot + 1; } for (int i = pot - 1; i >= 1; i--) { ibeg[i] = ibeg[i << 1]; iend[i] = iend[(i << 1) + 1]; } } void add(int pos, ll v) { pos += pot - 1; while (pos >= 1) { tsum[pos] += v; pos >>= 1; } } ll sum(int a, int b) { return _sum(a, b, 1); } ll find_suffix(int endpos, ll minsum) { minsum += sum(endpos + 1, pot); bool cleared = false; int v = 1; while (v < pot) { if (flag[v]) cleared = true; if ((cleared ? 0 : tsum[(v << 1) + 1]) < minsum) { minsum -= tsum[(v << 1) + 1]; v = (v << 1); } else { v = (v << 1) + 1; } } return v - pot + 1; } void clear(int a, int b) { _clear(a, b, 1); } void rollback() { while (!history.empty()) { change c = history.back(); if (c.arr == 0) tsum[c.index] = c.value; else flag[c.index] = c.value; history.pop_back(); } } }; int main() { int n; scanf("%d", &n); vector<query> queries; for (int i = 0; i < n; i++) { ll w; scanf("%lld", &w); queries.push_back(query(2, w, 0)); } int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int t; ll a, b = 0; scanf("%d", &t); if (t == 1) scanf("%lld%lld", &a, &b); else scanf("%lld", &a); queries.push_back(query(t, a, b)); } vector<pair<ll, int>> sizes; for (int i = 0; i < queries.size(); i++) { query q = queries[i]; if (q.id == 2) sizes.push_back(make_pair(q.a, i)); } sort(sizes.begin(), sizes.end()); map<ll, int> poz; //mapujemy wielkosci na pozycje w drzewie przedziałowym for (int i = 0; i < sizes.size(); i++) { if (poz.find(sizes[i].first) == poz.end()) { poz.insert(make_pair(sizes[i].first, i)); } } segtree tree1(sizes.size()); segtree tree2(sizes.size()); set<ll> szpr; //zbiór szprotek map<ll, int> cnts; //krotności szprotek for (query q : queries) { if (q.id == 2) { szpr.insert(q.a); cnts[q.a]++; tree1.add(poz[q.a] + cnts[q.a], 1); tree2.add(poz[q.a] + cnts[q.a], q.a); } else if (q.id == 3) { if (cnts[q.a] == 1) szpr.erase(q.a); tree1.add(poz[q.a] + cnts[q.a], -1); tree2.add(poz[q.a] + cnts[q.a], -q.a); cnts[q.a]--; } else { ll act = q.a; ll targ = q.b; ll out = 0; while (act < targ) { ll closest = targ; auto it = szpr.lower_bound(act); if (it != szpr.end()) closest = min(closest, *it + 1); ll req = closest - act; auto it2 = poz.lower_bound(act); if (it2 == poz.begin()) break; it2--; ll treeindexr = it2->second + cnts[it2->first]; if (tree2.sum(1, treeindexr) < req) break; ll treeindexl = tree2.find_suffix(treeindexr, req); out += tree1.sum(treeindexl, treeindexr); act += tree2.sum(treeindexl, treeindexr); tree1.clear(treeindexl, treeindexr); tree2.clear(treeindexl, treeindexr); } tree1.rollback(); tree2.rollback(); if (act >= targ) printf("%lld\n", out); else printf("-1\n"); } } }
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 | #include <bits/stdc++.h> using namespace std; using ll = long long int; struct query { int id; ll a; ll b; query(int id, ll a, ll b) : id(id), a(a), b(b) {} }; class segtree { int pot; ll *tsum; int *ibeg; int *iend; bool *flag; struct change{ int arr; int index; ll value; change(int arr, int index, ll value) : arr(arr), index(index), value(value) {}; }; vector<change> history; ll _sum(int a, int b, int v) { if (flag[v]) return 0; if (a <= ibeg[v] && iend[v] <= b) return tsum[v]; if (a > iend[v] || b < ibeg[v]) return 0; return _sum(a, b, v << 1) + _sum(a, b, (v << 1) + 1); } void _clear(int a, int b, int v) { if (a <= ibeg[v] && iend[v] <= b) { history.push_back(change(0, v, tsum[v])); history.push_back(change(1, v, flag[v])); flag[v] = true; tsum[v] = 0; return; } if (a > iend[v] || b < ibeg[v]) return; _clear(a, b, v << 1); _clear(a, b, (v << 1) + 1); history.push_back(change(0, v, tsum[v])); tsum[v] = tsum[v << 1] + tsum[(v << 1) + 1]; } public: segtree(int n) { pot = 1; while (pot <= n) pot <<= 1; tsum = new ll[2*pot]; flag = new bool[2*pot]; ibeg = new int[2*pot]; iend = new int[2*pot]; for (int i = 1; i < 2 * pot; i++) { tsum[i] = 0; flag[i] = false; } for (int i = pot; i < 2 * pot; i++) { ibeg[i] = i - pot + 1; iend[i] = i - pot + 1; } for (int i = pot - 1; i >= 1; i--) { ibeg[i] = ibeg[i << 1]; iend[i] = iend[(i << 1) + 1]; } } void add(int pos, ll v) { pos += pot - 1; while (pos >= 1) { tsum[pos] += v; pos >>= 1; } } ll sum(int a, int b) { return _sum(a, b, 1); } ll find_suffix(int endpos, ll minsum) { minsum += sum(endpos + 1, pot); bool cleared = false; int v = 1; while (v < pot) { if (flag[v]) cleared = true; if ((cleared ? 0 : tsum[(v << 1) + 1]) < minsum) { minsum -= tsum[(v << 1) + 1]; v = (v << 1); } else { v = (v << 1) + 1; } } return v - pot + 1; } void clear(int a, int b) { _clear(a, b, 1); } void rollback() { while (!history.empty()) { change c = history.back(); if (c.arr == 0) tsum[c.index] = c.value; else flag[c.index] = c.value; history.pop_back(); } } }; int main() { int n; scanf("%d", &n); vector<query> queries; for (int i = 0; i < n; i++) { ll w; scanf("%lld", &w); queries.push_back(query(2, w, 0)); } int q; scanf("%d", &q); for (int i = 0; i < q; i++) { int t; ll a, b = 0; scanf("%d", &t); if (t == 1) scanf("%lld%lld", &a, &b); else scanf("%lld", &a); queries.push_back(query(t, a, b)); } vector<pair<ll, int>> sizes; for (int i = 0; i < queries.size(); i++) { query q = queries[i]; if (q.id == 2) sizes.push_back(make_pair(q.a, i)); } sort(sizes.begin(), sizes.end()); map<ll, int> poz; //mapujemy wielkosci na pozycje w drzewie przedziałowym for (int i = 0; i < sizes.size(); i++) { if (poz.find(sizes[i].first) == poz.end()) { poz.insert(make_pair(sizes[i].first, i)); } } segtree tree1(sizes.size()); segtree tree2(sizes.size()); set<ll> szpr; //zbiór szprotek map<ll, int> cnts; //krotności szprotek for (query q : queries) { if (q.id == 2) { szpr.insert(q.a); cnts[q.a]++; tree1.add(poz[q.a] + cnts[q.a], 1); tree2.add(poz[q.a] + cnts[q.a], q.a); } else if (q.id == 3) { if (cnts[q.a] == 1) szpr.erase(q.a); tree1.add(poz[q.a] + cnts[q.a], -1); tree2.add(poz[q.a] + cnts[q.a], -q.a); cnts[q.a]--; } else { ll act = q.a; ll targ = q.b; ll out = 0; while (act < targ) { ll closest = targ; auto it = szpr.lower_bound(act); if (it != szpr.end()) closest = min(closest, *it + 1); ll req = closest - act; auto it2 = poz.lower_bound(act); if (it2 == poz.begin()) break; it2--; ll treeindexr = it2->second + cnts[it2->first]; if (tree2.sum(1, treeindexr) < req) break; ll treeindexl = tree2.find_suffix(treeindexr, req); out += tree1.sum(treeindexl, treeindexr); act += tree2.sum(treeindexl, treeindexr); tree1.clear(treeindexl, treeindexr); tree2.clear(treeindexl, treeindexr); } tree1.rollback(); tree2.rollback(); if (act >= targ) printf("%lld\n", out); else printf("-1\n"); } } } |