#include <cstdio> #include <cstdlib> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> using namespace std; typedef long long lol; typedef pair<int, int> PII; const int MAXN = 300000 + 9; const int XXX = 1 << 30; const lol XXXLL = 1LL << 60; struct Node { lol value; int lvl; int next; int dist; int down; lol sum; }; const int MAXLVL = 18; Node nodes[MAXN * 16 + 123]; const int HEAD = MAXLVL; const int POSHEAD = -1; const int TAIL = HEAD + 1; int firstAvailableNode = TAIL + 1; int headNode = 0; int newNode() { if (headNode == 0) return firstAvailableNode++; int res = headNode; headNode = nodes[res].next; return res; } void deleteNode(int idx) { nodes[idx].next = headNode; headNode = idx; } int rndLvl() { int x = rand(); for (int i=1; i<MAXLVL; ++i) if (x & (1 << (30 - i))) return i - 1; return MAXLVL - 1; } struct InsertRes { int pos; int idx; lol sum; }; // skip list with range tree class Magic { InsertRes insert(lol item, int idx, int pos, lol sum, int lvl) { if (nodes[nodes[idx].next].value < item) { return insert(item, nodes[idx].next, pos + nodes[idx].dist, sum + nodes[idx].sum, lvl); } if (nodes[idx].lvl > lvl) { ++nodes[idx].dist; nodes[idx].sum += item; return insert(item, nodes[idx].down, pos, sum, lvl); } if (nodes[idx].lvl == 0) { int x = newNode(); nodes[x].value = item; nodes[x].lvl = 0; nodes[x].next = nodes[idx].next; nodes[x].dist = 1; nodes[x].down = 0; nodes[x].sum = item; nodes[idx].next = x; return { pos + 1, x, sum + nodes[idx].value }; } InsertRes res = insert(item, nodes[idx].down, pos, sum, lvl); int x = newNode(); nodes[x].value = item; nodes[x].lvl = nodes[idx].lvl; nodes[x].next = nodes[idx].next; nodes[x].dist = pos + nodes[idx].dist + 1 - res.pos; nodes[x].down = res.idx; nodes[x].sum = sum + nodes[idx].sum + item - res.sum; nodes[idx].next = x; nodes[idx].dist = res.pos - pos; nodes[idx].sum = res.sum - sum; return { res.pos, x, res.sum }; } void eraseAt(int x, lol item, int idx, int pos) { if (idx == 0) return; int nidx = nodes[idx].next; int npos = pos + nodes[idx].dist; if (x > npos) return eraseAt(x, item, nidx, npos); if (x == npos) { int eidx = nidx; nodes[idx].next = nodes[eidx].next; nodes[idx].dist += nodes[eidx].dist - 1; nodes[idx].sum += nodes[eidx].sum - item; deleteNode(eidx); return eraseAt(x, item, nodes[idx].down, pos); } --nodes[idx].dist; nodes[idx].sum -= item; return eraseAt(x, item, nodes[idx].down, pos); } lol at(int x, int idx, int pos) { if (pos == x) return nodes[idx].value; if (pos + nodes[idx].dist <= x) return at(x, nodes[idx].next, pos + nodes[idx].dist); return at(x, nodes[idx].down, pos); } int lowerBound(lol item, int idx, int pos) { if (nodes[nodes[idx].next].value < item) return lowerBound(item, nodes[idx].next, pos + nodes[idx].dist); if (nodes[idx].lvl == 0) return pos + 1; return lowerBound(item, nodes[idx].down, pos); } lol getSum(int x, int y, int idx, int pos, lol res = 0) { if (x == y) return res; int npos = pos + nodes[idx].dist; if (npos <= x) return getSum(x, y, nodes[idx].next, npos, res); if (npos < y) { res += getSum(npos, y, nodes[idx].next, npos); y = npos; } if (x == pos && y == npos) return res + nodes[idx].sum; return getSum(x, y, nodes[idx].down, pos, res); } public: Magic() { nodes[TAIL].value = XXXLL; for (int i=1; i<=HEAD; ++i) { nodes[i].value = 0; nodes[i].lvl = i - 1; nodes[i].next = TAIL; nodes[i].dist = 1; nodes[i].down = i - 1; nodes[i].sum = 0; } } void insert(lol item) { int lvl = rndLvl(); insert(item, HEAD, POSHEAD, 0, lvl); } void erase(lol item) { int pos = lowerBound(item); eraseAt(pos, item, HEAD, POSHEAD); } lol at(int x) { return at(x, HEAD, POSHEAD); } lol getSum(int x, int y) { lol res = getSum(x, y, HEAD, POSHEAD); return res; } int lowerBound(lol item) { return lowerBound(item, HEAD, POSHEAD); } }; Magic magic; vector<PII> range; int solve(lol a, lol b, int res = 0) { if (b <= 0) { return res; } if (range.empty()) { return XXX; } int rx = range.back().first; int ry = range.back().second; range.pop_back(); lol nextw = magic.at(magic.lowerBound(a)); lol rsum = magic.getSum(rx, ry); if (a + rsum <= nextw) { if (rsum < b) { return solve(a + rsum, b - rsum, res + ry - rx); } while (rx + 1 < ry) { int rm = (rx + ry) / 2; lol rsum2 = magic.getSum(rm, ry); if (rsum2 < b) { a += rsum2; b -= rsum2; res += ry - rm; ry = rm; } else { rx = rm; } } return res + 1; } int orgrx = rx; int orgry = ry; while (rx + 1 < ry) { int rm = (rx + ry) / 2; lol rsum2 = magic.getSum(rm, ry); if (a + rsum2 > nextw || rsum2 >= b) { rx = rm; } else { a += rsum2; b -= rsum2; res += ry - rm; ry = rm; } } lol wrx = magic.at(rx); a += wrx; b -= wrx; ++res; if (orgrx < rx) range.push_back(make_pair(orgrx, rx)); range.push_back(make_pair(orgry, magic.lowerBound(a))); return solve(a, b, res); } int main() { srand(20191213); int n; scanf("%d", &n); for (int i=0; i<n; ++i) { lol w; scanf("%lld", &w); magic.insert(w); } range.reserve(256); int q; scanf("%d", &q); for (int qq=0; qq<q; ++qq) { int cmd; scanf("%d", &cmd); if (1 == cmd) { lol a, b; scanf("%lld%lld", &a, &b); range.clear(); range.push_back(make_pair(0, magic.lowerBound(a))); int res = solve(a, b - a); if (res >= XXX) res = -1; printf("%d\n", res); } if (2 == cmd) { lol w; scanf("%lld", &w); magic.insert(w); } if (3 == cmd) { lol w; scanf("%lld", &w); magic.erase(w); } } return 0; }
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 | #include <cstdio> #include <cstdlib> #include <vector> #include <map> #include <set> #include <queue> #include <algorithm> using namespace std; typedef long long lol; typedef pair<int, int> PII; const int MAXN = 300000 + 9; const int XXX = 1 << 30; const lol XXXLL = 1LL << 60; struct Node { lol value; int lvl; int next; int dist; int down; lol sum; }; const int MAXLVL = 18; Node nodes[MAXN * 16 + 123]; const int HEAD = MAXLVL; const int POSHEAD = -1; const int TAIL = HEAD + 1; int firstAvailableNode = TAIL + 1; int headNode = 0; int newNode() { if (headNode == 0) return firstAvailableNode++; int res = headNode; headNode = nodes[res].next; return res; } void deleteNode(int idx) { nodes[idx].next = headNode; headNode = idx; } int rndLvl() { int x = rand(); for (int i=1; i<MAXLVL; ++i) if (x & (1 << (30 - i))) return i - 1; return MAXLVL - 1; } struct InsertRes { int pos; int idx; lol sum; }; // skip list with range tree class Magic { InsertRes insert(lol item, int idx, int pos, lol sum, int lvl) { if (nodes[nodes[idx].next].value < item) { return insert(item, nodes[idx].next, pos + nodes[idx].dist, sum + nodes[idx].sum, lvl); } if (nodes[idx].lvl > lvl) { ++nodes[idx].dist; nodes[idx].sum += item; return insert(item, nodes[idx].down, pos, sum, lvl); } if (nodes[idx].lvl == 0) { int x = newNode(); nodes[x].value = item; nodes[x].lvl = 0; nodes[x].next = nodes[idx].next; nodes[x].dist = 1; nodes[x].down = 0; nodes[x].sum = item; nodes[idx].next = x; return { pos + 1, x, sum + nodes[idx].value }; } InsertRes res = insert(item, nodes[idx].down, pos, sum, lvl); int x = newNode(); nodes[x].value = item; nodes[x].lvl = nodes[idx].lvl; nodes[x].next = nodes[idx].next; nodes[x].dist = pos + nodes[idx].dist + 1 - res.pos; nodes[x].down = res.idx; nodes[x].sum = sum + nodes[idx].sum + item - res.sum; nodes[idx].next = x; nodes[idx].dist = res.pos - pos; nodes[idx].sum = res.sum - sum; return { res.pos, x, res.sum }; } void eraseAt(int x, lol item, int idx, int pos) { if (idx == 0) return; int nidx = nodes[idx].next; int npos = pos + nodes[idx].dist; if (x > npos) return eraseAt(x, item, nidx, npos); if (x == npos) { int eidx = nidx; nodes[idx].next = nodes[eidx].next; nodes[idx].dist += nodes[eidx].dist - 1; nodes[idx].sum += nodes[eidx].sum - item; deleteNode(eidx); return eraseAt(x, item, nodes[idx].down, pos); } --nodes[idx].dist; nodes[idx].sum -= item; return eraseAt(x, item, nodes[idx].down, pos); } lol at(int x, int idx, int pos) { if (pos == x) return nodes[idx].value; if (pos + nodes[idx].dist <= x) return at(x, nodes[idx].next, pos + nodes[idx].dist); return at(x, nodes[idx].down, pos); } int lowerBound(lol item, int idx, int pos) { if (nodes[nodes[idx].next].value < item) return lowerBound(item, nodes[idx].next, pos + nodes[idx].dist); if (nodes[idx].lvl == 0) return pos + 1; return lowerBound(item, nodes[idx].down, pos); } lol getSum(int x, int y, int idx, int pos, lol res = 0) { if (x == y) return res; int npos = pos + nodes[idx].dist; if (npos <= x) return getSum(x, y, nodes[idx].next, npos, res); if (npos < y) { res += getSum(npos, y, nodes[idx].next, npos); y = npos; } if (x == pos && y == npos) return res + nodes[idx].sum; return getSum(x, y, nodes[idx].down, pos, res); } public: Magic() { nodes[TAIL].value = XXXLL; for (int i=1; i<=HEAD; ++i) { nodes[i].value = 0; nodes[i].lvl = i - 1; nodes[i].next = TAIL; nodes[i].dist = 1; nodes[i].down = i - 1; nodes[i].sum = 0; } } void insert(lol item) { int lvl = rndLvl(); insert(item, HEAD, POSHEAD, 0, lvl); } void erase(lol item) { int pos = lowerBound(item); eraseAt(pos, item, HEAD, POSHEAD); } lol at(int x) { return at(x, HEAD, POSHEAD); } lol getSum(int x, int y) { lol res = getSum(x, y, HEAD, POSHEAD); return res; } int lowerBound(lol item) { return lowerBound(item, HEAD, POSHEAD); } }; Magic magic; vector<PII> range; int solve(lol a, lol b, int res = 0) { if (b <= 0) { return res; } if (range.empty()) { return XXX; } int rx = range.back().first; int ry = range.back().second; range.pop_back(); lol nextw = magic.at(magic.lowerBound(a)); lol rsum = magic.getSum(rx, ry); if (a + rsum <= nextw) { if (rsum < b) { return solve(a + rsum, b - rsum, res + ry - rx); } while (rx + 1 < ry) { int rm = (rx + ry) / 2; lol rsum2 = magic.getSum(rm, ry); if (rsum2 < b) { a += rsum2; b -= rsum2; res += ry - rm; ry = rm; } else { rx = rm; } } return res + 1; } int orgrx = rx; int orgry = ry; while (rx + 1 < ry) { int rm = (rx + ry) / 2; lol rsum2 = magic.getSum(rm, ry); if (a + rsum2 > nextw || rsum2 >= b) { rx = rm; } else { a += rsum2; b -= rsum2; res += ry - rm; ry = rm; } } lol wrx = magic.at(rx); a += wrx; b -= wrx; ++res; if (orgrx < rx) range.push_back(make_pair(orgrx, rx)); range.push_back(make_pair(orgry, magic.lowerBound(a))); return solve(a, b, res); } int main() { srand(20191213); int n; scanf("%d", &n); for (int i=0; i<n; ++i) { lol w; scanf("%lld", &w); magic.insert(w); } range.reserve(256); int q; scanf("%d", &q); for (int qq=0; qq<q; ++qq) { int cmd; scanf("%d", &cmd); if (1 == cmd) { lol a, b; scanf("%lld%lld", &a, &b); range.clear(); range.push_back(make_pair(0, magic.lowerBound(a))); int res = solve(a, b - a); if (res >= XXX) res = -1; printf("%d\n", res); } if (2 == cmd) { lol w; scanf("%lld", &w); magic.insert(w); } if (3 == cmd) { lol w; scanf("%lld", &w); magic.erase(w); } } return 0; } |