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 #include #define x first #define y second using namespace std; using L = long long; struct Query { int type; L v1, v2; }; vector> scal; vector> reduced; int n; int q; Query queries[400100]; int posOfQuery[100100]; L treeVals[400100]; L sums[400100]; L number[400100]; int depth = 1; int lsb(int x) {return x & (-x);} void fenAdd(L * fen, L val, int pos) { while(pos <= depth) { fen[pos] += val; pos += lsb(pos); } } L fenRead(L * fen, int pos) { L ans = 0; while(pos) { ans += fen[pos]; pos -= lsb(pos); } return ans; } L fenInterval(L * fen, int a, int b) { return fenRead(fen, b) - fenRead(fen, a - 1); } struct TR { L maxi; }; TR tree[1<<20]; //int countOfLeaves[1<<19]; void update(int p) { while(p > 1) { p /= 2; tree[p].maxi = max(tree[2 * p].maxi, tree[2 * p + 1].maxi); } } L searchForGreaterThan(L val) { if(tree[1].maxi <= val) return -1; int p = 1; while(p < depth) { if(tree[2 * p].maxi > val) { p *= 2; } else p = p * 2 + 1; } return p; } vector> binsearches; //vector szprotki_startowe; int findAns(L greaterThan) { int k = 0; L f = 0; for(int u = depth; u > 0; u >>= 1) { //cout << k << ' ' << sums[k ^ u] << ' ' << f << ' ' << greaterThan << ' ' << u << '\n'; if(f + sums[k ^ u] <= greaterThan) { k ^= u; f += sums[k]; } } return k; } multimap elemsToDel; int main() { ios_base::sync_with_stdio(0); cin.tie(0); //cout << (long long)4e18 << '\n'; cin >> n; scal.reserve(400100); binsearches.reserve(400100); for(int i = 0; i < n; ++i) { L in; cin >> in; scal.push_back({in, -1}); } cin >> q; for(int i = 0; i < q; ++i) { cin >> queries[i].type >> queries[i].v1; if(queries[i].type == 1) cin >> queries[i].v2; else if(queries[i].type != 1) { scal.push_back({queries[i].v1, i}); } } scal.push_back({4e18, -1}); sort(scal.begin(), scal.end()); while(depth <= scal.size() + 6) depth *= 2; int nowPos = 0; //L lastVal = -1; for(auto pp : scal) { /*if(pp.x != lastVal) { ++nowPos; lastVal = pp.x; treeVals[nowPos] = lastVal; }*/ ++nowPos; treeVals[nowPos] = pp.x; if(pp.y == -1) { elemsToDel.insert({pp.x, nowPos}); tree[nowPos + depth].maxi = pp.x; fenAdd(sums, pp.x, nowPos); fenAdd(number, 1, nowPos); } else { posOfQuery[pp.y] = nowPos; } //cout << pp.x << ' ' << pp.y << '\n'; } for(int i = depth - 1; i >= 1; --i) { tree[i].maxi = max(tree[2 * i].maxi, tree[2 * i + 1].maxi); } /*for(int i = 1; i <= 20; ++i) { cout << "fen: " << sums[i] << '\n'; }*/ for(int i = 0; i < q; ++i) { if(queries[i].type == 1) { //cout << "DOIN1\n"; int ans = 0; int lastBlackPoint = 1; int greaterPos; L szczupakNow = queries[i].v1; if(szczupakNow >= queries[i].v2) { cout << "0\n"; continue; } bool workDone = 0; while(true) { //cout << "UNO\n"; greaterPos = searchForGreaterThan(szczupakNow) - depth; //cout << greaterPos << ' ' << treeVals[greaterPos] << ' ' << szczupakNow << '\n'; binsearches.push_back({lastBlackPoint, greaterPos - 1}); if(treeVals[greaterPos] + 1 >= queries[i].v2) { ///Last biggest szczupakor while(true) { //cout << "DOS\n"; if(binsearches.size() == 0) { cout << "-1\n"; break; } L intVal = fenInterval(sums, binsearches[binsearches.size() - 1].x, binsearches[binsearches.size() - 1].y); //cout << "CLOSIN " << binsearches[binsearches.size() - 1].x << ' ' << binsearches[binsearches.size() - 1].y << ' ' << intVal << ' ' << szczupakNow << ' ' << ans << ' ' << treeVals[greaterPos] << '\n'; if (intVal + szczupakNow < queries[i].v2) { szczupakNow += intVal; ans += fenInterval(number, binsearches[binsearches.size() - 1].x, binsearches[binsearches.size() - 1].y); binsearches.pop_back(); } else { //cout << szczupakNow << ' ' << fenRead(sums, binsearches[binsearches.size() - 1].y) << ' ' << queries[i].v2; int gotTheSzczupak = findAns(szczupakNow + fenRead(sums, binsearches[binsearches.size() - 1].y) - queries[i].v2) + 1; //cout << "GOLEM " << gotTheSzczupak << '\n'; ans += fenInterval(number, gotTheSzczupak, binsearches[binsearches.size() - 1].y); cout << ans << '\n'; binsearches[binsearches.size() - 1].y = gotTheSzczupak - 1; if(binsearches[binsearches.size() - 1].x > binsearches[binsearches.size() - 1].y) { binsearches.pop_back(); } break; } } break; } else { ///Get to szczupakor NOW! while(true) { if(binsearches.size() == 0) { cout << "-1\n"; workDone = 1; break; } L intVal = fenInterval(sums, binsearches[binsearches.size() - 1].x, binsearches[binsearches.size() - 1].y); //cout << "serchin " << binsearches[binsearches.size() - 1].x << ' ' << binsearches[binsearches.size() - 1].y << ' ' << intVal << ' ' << szczupakNow << ' ' << ans << ' ' << treeVals[greaterPos] << '\n'; if (intVal + szczupakNow < treeVals[greaterPos] + 1) { szczupakNow += intVal; ans += fenInterval(number, binsearches[binsearches.size() - 1].x, binsearches[binsearches.size() - 1].y); binsearches.pop_back(); } else { int gotTheSzczupak = findAns(szczupakNow + fenRead(sums, binsearches[binsearches.size() - 1].y) - (treeVals[greaterPos] + 1)) + 1; //cout << "GOTTHESZCZUPAK " << binsearches[binsearches.size() - 1].x << ' ' << binsearches[binsearches.size() - 1].y << ' ' << gotTheSzczupak << '\n'; ans += fenInterval(number, gotTheSzczupak, binsearches[binsearches.size() - 1].y); szczupakNow += fenInterval(sums, gotTheSzczupak, binsearches[binsearches.size() - 1].y); binsearches[binsearches.size() - 1].y = gotTheSzczupak - 1; if(binsearches[binsearches.size() - 1].x > binsearches[binsearches.size() - 1].y) binsearches.pop_back(); if(szczupakNow >= queries[i].v2) { cout << ans << '\n'; workDone = 1; } break; } } if(workDone) break; } lastBlackPoint = greaterPos; } while(!binsearches.empty()) binsearches.pop_back(); } else if (queries[i].type == 2) { //cout << "DOIN2\n"; elemsToDel.insert({queries[i].v1, posOfQuery[i]}); tree[posOfQuery[i] + depth].maxi = treeVals[posOfQuery[i]]; update(posOfQuery[i] + depth); fenAdd(sums, queries[i].v1, posOfQuery[i]); fenAdd(number, 1, posOfQuery[i]); } else { auto posOfDeletion = elemsToDel.find(queries[i].v1); //cout << "DOIN3 " << posOfDeletion << "\n"; tree[posOfDeletion->y + depth].maxi = 0; update(posOfDeletion->y + depth); fenAdd(sums, -queries[i].v1, posOfDeletion->y); fenAdd(number, -1, posOfDeletion->y); elemsToDel.erase(posOfDeletion); } } return 0; }