#include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) using Ind = pair<ll, int>; struct SegTree { vector<Ind> inds; vector<ll> tSum, tCnt, tCurSum, tCurCnt; vector<char> tFlag; // 0 = nothing, 1 = enable, 2 = disable int len; SegTree(const vector<Ind>& indices) { inds = indices; for (len = 1; len < sz(inds); len *= 2); tSum.resize(len*2, 0); tCnt.resize(len*2, 0); tCurSum.resize(len*2, 0); tCurCnt.resize(len*2, 0); tFlag.resize(len*2, 0); int k = 1; while (sz(inds) < len) { inds.pb(Ind(2e18, k++)); } } int getPos(Ind k) { return int(lower_bound(all(inds), k) - inds.begin()); } void enableNode(int i, bool on) { tFlag[i] = on; if (on) { tCurSum[i] = tSum[i]; tCurCnt[i] = tCnt[i]; } else { tFlag[i] = 2; tCurSum[i] = tCurCnt[i] = 0; } } void push(int i) { if (tFlag[i] == 1) { enableNode(i*2, 1); enableNode(i*2+1, 1); } else if (tFlag[i] == 2) { enableNode(i*2, 0); enableNode(i*2+1, 0); } tFlag[i] = 0; } void update(int i) { tSum[i] = tSum[i*2] + tSum[i*2+1]; tCnt[i] = tCnt[i*2] + tCnt[i*2+1]; tCurSum[i] = tCurSum[i*2] + tCurSum[i*2+1]; tCurCnt[i] = tCurCnt[i*2] + tCurCnt[i*2+1]; } void doAdd(int vb, ll val, ll val2, int i, int b, int e) { if (i >= len) { assert(vb == i-len); tCnt[i] += val; tSum[i] += val2; if (tFlag[i] != 2) { tCurCnt[i] = tCnt[i]; tCurSum[i] = tSum[i]; } return; } int m = (b+e) / 2; push(i); if (vb < m) { doAdd(vb, val, val2, i*2, b, m); } else { doAdd(vb, val, val2, i*2+1, m, e); } update(i); } void add(Ind ind, ll val) { int i = getPos(ind); assert(i < sz(inds) && inds[i] == ind); doAdd(i, val, val*ind.x, 1, 0, len); } pair<ll, ll> doEnable(int vb, int ve, bool val, int i, int b, int e) { if (b >= ve || vb >= e) return {0, 0}; if (b >= vb && e <= ve) { auto ret = mp(tCurCnt[i], tCurSum[i]); enableNode(i, val); return ret; } int m = (b+e) / 2; push(i); auto v1 = doEnable(vb, ve, val, i*2, b, m); auto v2 = doEnable(vb, ve, val, i*2+1, m, e); update(i); return {v1.x+v2.x, v1.y+v2.y}; } pair<ll, ll> enable(Ind vb, Ind ve, bool on) { return doEnable(getPos(vb), getPos(ve), on, 1, 0, len); } int doFindNonZero(int vb, int ve, int i, int b, int e) { if (b >= ve || vb >= e || tCurCnt[i] == 0) return -1; if (b >= vb && e <= ve) { while (i < len) { push(i); i = i*2 + (tCurCnt[i*2] == 0); } return i-len; } int m = (b+e) / 2; push(i); int k = doFindNonZero(vb, ve, i*2, b, m); if (k != -1) return k; return doFindNonZero(vb, ve, i*2+1, m, e); } Ind findNonZero(Ind vb, Ind ve) { int i = doFindNonZero(getPos(vb), getPos(ve), 1, 0, len); return i != -1 ? inds[i] : Ind(-1, -1); } pair<int, ll> doFindSum(int vb, int ve, ll sum, int i, int b, int e) { if (b >= ve || vb >= e) return {-1, sum}; if (b >= vb && e <= ve) { if (tCurSum[i] < sum) { return {-1, sum-tCurSum[i]}; } while (i < len) { push(i); if (tCurSum[i*2+1] >= sum) { i = i*2+1; } else { sum -= tCurSum[i*2+1]; i = i*2; } } return {i-len, -1}; } int m = (b+e) / 2; push(i); auto k = doFindSum(vb, ve, sum, i*2+1, m, e); if (k.x != -1) return k; return doFindSum(vb, ve, k.y, i*2, b, m); } Ind findSum(Ind vb, Ind ve, ll sum) { auto k = doFindSum(getPos(vb), getPos(ve), sum, 1, 0, len); return k.x != -1 ? inds[k.x] : Ind(-1, -1); } }; struct Event { int t; // 1 = query, 2 = add, 3 = del Ind w1, w2; DBP(t, w1, w2); }; int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(18); int n; cin >> n; vector<Event> events(n); vector<Ind> weights; map<ll, int> counts; each(e, events) { e.t = 2; cin >> e.w1.x; e.w1.y = ++counts[e.w1.x]; weights.pb(e.w1); } int q; cin >> q; events.resize(n+q); rep(i, 0, q) { auto& e = events[n+i]; cin >> e.t; if (e.t == 1) { cin >> e.w1.x >> e.w2.x; e.w1.y = e.w2.y = -1; weights.pb(e.w1); weights.pb(e.w2); } else if (e.t == 2) { cin >> e.w1.x; e.w1.y = ++counts[e.w1.x]; weights.pb(e.w1); } else if (e.t == 3) { cin >> e.w1.x; e.w1.y = counts[e.w1.x]--; weights.pb(e.w1); } } sort(all(weights)); weights.erase(unique(all(weights)), weights.end()); SegTree tree(weights); each(e, events) { if (e.t == 1) { Ind curWeight = e.w1; ll ans = 0; while (curWeight < e.w2) { Ind nxt = tree.findNonZero(curWeight, {2e18, 1e9}); if (nxt.x != -1) { nxt = min(Ind(nxt.x+1, -1), e.w2); } else { nxt = e.w2; } ll needed = nxt.x - curWeight.x; Ind from = tree.findSum({0, 0}, curWeight, needed); if (from.x == -1) { ans = -1; break; } auto chg = tree.enable(from, curWeight, 0); ans += chg.x; curWeight.x += chg.y; } tree.enableNode(1, 1); cout << ans << '\n'; } else if (e.t == 2) { tree.add(e.w1, 1); } else if (e.t == 3) { tree.add(e.w1, -1); } } 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 | #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) using Ind = pair<ll, int>; struct SegTree { vector<Ind> inds; vector<ll> tSum, tCnt, tCurSum, tCurCnt; vector<char> tFlag; // 0 = nothing, 1 = enable, 2 = disable int len; SegTree(const vector<Ind>& indices) { inds = indices; for (len = 1; len < sz(inds); len *= 2); tSum.resize(len*2, 0); tCnt.resize(len*2, 0); tCurSum.resize(len*2, 0); tCurCnt.resize(len*2, 0); tFlag.resize(len*2, 0); int k = 1; while (sz(inds) < len) { inds.pb(Ind(2e18, k++)); } } int getPos(Ind k) { return int(lower_bound(all(inds), k) - inds.begin()); } void enableNode(int i, bool on) { tFlag[i] = on; if (on) { tCurSum[i] = tSum[i]; tCurCnt[i] = tCnt[i]; } else { tFlag[i] = 2; tCurSum[i] = tCurCnt[i] = 0; } } void push(int i) { if (tFlag[i] == 1) { enableNode(i*2, 1); enableNode(i*2+1, 1); } else if (tFlag[i] == 2) { enableNode(i*2, 0); enableNode(i*2+1, 0); } tFlag[i] = 0; } void update(int i) { tSum[i] = tSum[i*2] + tSum[i*2+1]; tCnt[i] = tCnt[i*2] + tCnt[i*2+1]; tCurSum[i] = tCurSum[i*2] + tCurSum[i*2+1]; tCurCnt[i] = tCurCnt[i*2] + tCurCnt[i*2+1]; } void doAdd(int vb, ll val, ll val2, int i, int b, int e) { if (i >= len) { assert(vb == i-len); tCnt[i] += val; tSum[i] += val2; if (tFlag[i] != 2) { tCurCnt[i] = tCnt[i]; tCurSum[i] = tSum[i]; } return; } int m = (b+e) / 2; push(i); if (vb < m) { doAdd(vb, val, val2, i*2, b, m); } else { doAdd(vb, val, val2, i*2+1, m, e); } update(i); } void add(Ind ind, ll val) { int i = getPos(ind); assert(i < sz(inds) && inds[i] == ind); doAdd(i, val, val*ind.x, 1, 0, len); } pair<ll, ll> doEnable(int vb, int ve, bool val, int i, int b, int e) { if (b >= ve || vb >= e) return {0, 0}; if (b >= vb && e <= ve) { auto ret = mp(tCurCnt[i], tCurSum[i]); enableNode(i, val); return ret; } int m = (b+e) / 2; push(i); auto v1 = doEnable(vb, ve, val, i*2, b, m); auto v2 = doEnable(vb, ve, val, i*2+1, m, e); update(i); return {v1.x+v2.x, v1.y+v2.y}; } pair<ll, ll> enable(Ind vb, Ind ve, bool on) { return doEnable(getPos(vb), getPos(ve), on, 1, 0, len); } int doFindNonZero(int vb, int ve, int i, int b, int e) { if (b >= ve || vb >= e || tCurCnt[i] == 0) return -1; if (b >= vb && e <= ve) { while (i < len) { push(i); i = i*2 + (tCurCnt[i*2] == 0); } return i-len; } int m = (b+e) / 2; push(i); int k = doFindNonZero(vb, ve, i*2, b, m); if (k != -1) return k; return doFindNonZero(vb, ve, i*2+1, m, e); } Ind findNonZero(Ind vb, Ind ve) { int i = doFindNonZero(getPos(vb), getPos(ve), 1, 0, len); return i != -1 ? inds[i] : Ind(-1, -1); } pair<int, ll> doFindSum(int vb, int ve, ll sum, int i, int b, int e) { if (b >= ve || vb >= e) return {-1, sum}; if (b >= vb && e <= ve) { if (tCurSum[i] < sum) { return {-1, sum-tCurSum[i]}; } while (i < len) { push(i); if (tCurSum[i*2+1] >= sum) { i = i*2+1; } else { sum -= tCurSum[i*2+1]; i = i*2; } } return {i-len, -1}; } int m = (b+e) / 2; push(i); auto k = doFindSum(vb, ve, sum, i*2+1, m, e); if (k.x != -1) return k; return doFindSum(vb, ve, k.y, i*2, b, m); } Ind findSum(Ind vb, Ind ve, ll sum) { auto k = doFindSum(getPos(vb), getPos(ve), sum, 1, 0, len); return k.x != -1 ? inds[k.x] : Ind(-1, -1); } }; struct Event { int t; // 1 = query, 2 = add, 3 = del Ind w1, w2; DBP(t, w1, w2); }; int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(18); int n; cin >> n; vector<Event> events(n); vector<Ind> weights; map<ll, int> counts; each(e, events) { e.t = 2; cin >> e.w1.x; e.w1.y = ++counts[e.w1.x]; weights.pb(e.w1); } int q; cin >> q; events.resize(n+q); rep(i, 0, q) { auto& e = events[n+i]; cin >> e.t; if (e.t == 1) { cin >> e.w1.x >> e.w2.x; e.w1.y = e.w2.y = -1; weights.pb(e.w1); weights.pb(e.w2); } else if (e.t == 2) { cin >> e.w1.x; e.w1.y = ++counts[e.w1.x]; weights.pb(e.w1); } else if (e.t == 3) { cin >> e.w1.x; e.w1.y = counts[e.w1.x]--; weights.pb(e.w1); } } sort(all(weights)); weights.erase(unique(all(weights)), weights.end()); SegTree tree(weights); each(e, events) { if (e.t == 1) { Ind curWeight = e.w1; ll ans = 0; while (curWeight < e.w2) { Ind nxt = tree.findNonZero(curWeight, {2e18, 1e9}); if (nxt.x != -1) { nxt = min(Ind(nxt.x+1, -1), e.w2); } else { nxt = e.w2; } ll needed = nxt.x - curWeight.x; Ind from = tree.findSum({0, 0}, curWeight, needed); if (from.x == -1) { ans = -1; break; } auto chg = tree.enable(from, curWeight, 0); ans += chg.x; curWeight.x += chg.y; } tree.enableNode(1, 1); cout << ans << '\n'; } else if (e.t == 2) { tree.add(e.w1, 1); } else if (e.t == 3) { tree.add(e.w1, -1); } } return 0; } |