#include <algorithm> #include <map> #include <vector> #include <unordered_map> #include <set> #define LL long long #define BIGMOD 1000012177LL #define DNUM 31LL #define K3DBG(X) using namespace std; struct pair_hash { template <class T1, class T2> std::size_t operator() (const std::pair<T1, T2> &pair) const { return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second); } }; class BitTree { public: LL *mData; int mN; BitTree(int n) : mN(n) { mData = new LL[mN+1]; for (int i=0; i<=mN; i++) { mData[i] = 0; } } ~BitTree() { delete[] mData; } // Updates a node in Binary Index Tree (BITree) at given index // in BITree. The given value 'val' is added to BITree[i] and // all of its ancestors in tree. void updateBIT(int index, LL val) { // index in BITree[] is 1 more than the index in arr[] index = index + 1; // Traverse all ancestors and add 'val' while (index <= mN) { // Add 'val' to current node of BI Tree mData[index] += val; // Update index to that of parent in update View index += index & (-index); } } LL getElem(int index) { LL res = getSum(index); // printf("bt::getElem(%d)=%lld\n", index, res); return res; } // SERVES THE PURPOSE OF getElement() // Returns sum of arr[0..index]. This function assumes // that the array is preprocessed and partial sums of // array elements are stored in BITree[] LL getSum(int index) { LL sum = 0; // Iniialize result // index in BITree[] is 1 more than the index in arr[] index = index + 1; // Traverse ancestors of BITree[index] while (index>0) { // Add current element of BITree to sum sum += mData[index]; // Move index to parent node in getSum View index -= index & (-index); } return sum; } // Updates such that getElement() gets an increased // value when queried from l to r. void update(int l, int r, LL val) { // Increase value at 'l' by 'val' updateBIT(l, val); // Decrease value at 'r+1' by 'val' updateBIT(r+1, -val); } void print() { printf("BitTree="); for (int i=0; i < mN; i++) { printf("%lld ", getElem(i)); } printf("\n"); } }; class PresenceIndicator { // TODO: w oparciu o drzewko licznikowe void mark(bool isPresent) { } int countDead(int left, int right) { } int countAlive(int left, int right) { } }; class RangeSumSolver { LL rangeSum(int left, int right) { return 0; } }; #define ASSERT_EQ(X, Y) // { if (!(X == Y)) printf(#X " != " #Y); } #define ASSERT_TRUE(X) // { if (!(X)) printf(#X " is false!"); } void selftest() { BitTree bt(100); bt.updateBIT(2,1); bt.updateBIT(4,1); bt.updateBIT(5, 2); bt.updateBIT(10,1); for (int i=0; i < 20; i++) { printf("%d %lld\n", i, bt.getElem(i)); } } class Query { public: const static int QUERY_TYPE_SZCZUPAK = 1; const static int QUERY_TYPE_DODAJ = 2; const static int QUERY_TYPE_USUN = 3; int queryType; LL wi; LL S, K; int arrIndex; int szprotkaIndex; Query() : queryType(0), wi(0), S(0), K(0), szprotkaIndex(-1) {} Query(int queryType, LL wi, int arrIndex) : queryType(queryType), wi(wi), S(0), K(0), szprotkaIndex(-1), arrIndex(arrIndex) {} Query(int queryType, LL S, LL K, int arrIndex) : queryType(queryType), wi(0), S(S), K(K), szprotkaIndex(-1), arrIndex(arrIndex) {} void print() const { if (queryType == 1) { printf("Query(qt:%d (%lld->%lld) [ai:%d,si:%d])", queryType, S, K, arrIndex, szprotkaIndex); } else { printf("Query(qt:%d (w:%lld) [ai:%d,si:%d])", queryType, wi, arrIndex, szprotkaIndex); } } }; class Szprotka { public: LL waga; int arrIndex; int queryIndex; LL potencjalnaWaga; // dla szprotek jeszcze nie instniejacych Szprotka(LL waga, int arrIndex, int queryIndex, LL potencjalnaWaga) : waga(waga), arrIndex(arrIndex), queryIndex(queryIndex), potencjalnaWaga(potencjalnaWaga) {} bool operator<(const Szprotka& peer) { if (waga == peer.waga) { return arrIndex < peer.arrIndex; } return waga < peer.waga; } void print() const { printf("Szp:(w=%lld", waga); if (queryIndex>=0) { printf(",q(%d)->%lld)", queryIndex, potencjalnaWaga); } else { printf(")"); } } void makeAlive() { if (waga != 0) printf("INCORRECT assumption: expected dead, got: alive\n"); waga = potencjalnaWaga; } void makeDead() { if (waga == 0) printf("INCORRECT assumption: expected Alive, got: dead"); waga = 0; } }; void print_szprotki(vector<Szprotka> &szprotki) { printf("szprotki:\n"); for (int i=0; i < szprotki.size(); i++) { szprotki[i].print();printf("\n"); } } class SzczupakAttacker { static int alreadyProcessed[400100]; static int alreadyProcessedCounter; static int jumpDown[400100]; public: const vector<Szprotka> &szprotki; LL S, K; SzczupakAttacker(const vector<Szprotka> &szprotki, LL S, LL K) : szprotki{szprotki}, S(S), K(K) { alreadyProcessedCounter++; } int moveSzczupak(int idx) { int p2 = 1; int lastOK = idx; int limit = szprotki.size(); while (idx < limit && S > szprotki[idx].potencjalnaWaga) { while (idx + p2 < limit && S > szprotki[idx+p2].potencjalnaWaga) { lastOK = idx + p2; p2 *= 2; } idx++; if (lastOK > idx) idx = lastOK; } return idx; } LL attack(int initialSzprotkaIndex) { LL totalEatenCount = 0; LL totalEateanWeight = 0; LL szprotkiSize = szprotki.size(); //printf("Szczupak rozmiar %lld zaczyna od szprotki %d\n", S, initialSzprotkaIndex); // To jest pierwsza, ktorej szczupak nie jest w stanie zjesc. Byc moze poza tablica (idx=szprotki.size()) int szczupakPos = initialSzprotkaIndex; ASSERT_TRUE(szczupakPos >= 0); while (S < K) { int i = szczupakPos - 1; if (K - S <= 0) return totalEatenCount; LL diff = K - S; if (szczupakPos < szprotkiSize) { diff = min(diff, 1 + szprotki[szczupakPos].potencjalnaWaga - S); } LL localSum = 0; LL localEaten = 0; int initialPos = i; while (i >= 0 && localSum < diff) { if (alreadyProcessed[i] == alreadyProcessedCounter) { int jump = jumpDown[i]; if (jump > i) { jump = jumpDown[jump]; } if (jump > 0 && jump < i) { i = jump; } } const Szprotka &sp = szprotki[i]; //printf("Trying szprotka %d ", i); sp.print(); printf("\n"); if (sp.waga && alreadyProcessed[i] != alreadyProcessedCounter) { localSum += sp.waga; localEaten++; alreadyProcessed[i] = alreadyProcessedCounter; jumpDown[i] = initialPos; //printf("Eaten szprotka "); sp.print(); printf("\n"); } i--; } if (initialPos > 0) { alreadyProcessed[initialPos] = alreadyProcessedCounter; jumpDown[initialPos] = i; } S += localSum; // printf("new szczupak weight: %lld\n", S); // jesli nie urusl if (localSum == 0) { return -1; } szczupakPos = moveSzczupak(szczupakPos); totalEateanWeight += localSum; totalEatenCount += localEaten; // ASSERRT: S >= szprotki[idx].potencjalnaWaga lub idx == szprotki.size() } return totalEatenCount; } }; int SzczupakAttacker::alreadyProcessed[400100] = {0}; int SzczupakAttacker::alreadyProcessedCounter = 1; int SzczupakAttacker::jumpDown[400100] = {0}; void answerQueries(const vector<Query> &queries, vector<Szprotka> &szprotki) { K3DBG(print_szprotki(szprotki)); for (int i=0; i < queries.size(); i++) { const Query &q = queries[i]; K3DBG(printf("Answering query Q=")); K3DBG(q.print()); K3DBG(printf("\n")); ASSERT_TRUE(q.szprotkaIndex >= 0); if (q.queryType == Query::QUERY_TYPE_SZCZUPAK) { K3DBG(printf("Szczupak %lld -> %lld\n", q.S, q.K)); SzczupakAttacker szczupak(szprotki, q.S, q.K); printf("%lld\n", szczupak.attack(q.szprotkaIndex)); } if (q.queryType == Query::QUERY_TYPE_DODAJ) { ASSERT_TRUE(q.szprotkaIndex >= 0); K3DBG(printf("wzbudzam szprotke %d do wagi %lld\n", q.szprotkaIndex, szprotki[q.szprotkaIndex].potencjalnaWaga)); Szprotka &sp = szprotki[q.szprotkaIndex]; sp.makeAlive(); // TODO: increase BitIndexTree in element X } if (q.queryType == Query::QUERY_TYPE_USUN) { K3DBG(printf("zaznaczam szprotke %d jaka martwa\n", q.szprotkaIndex)); Szprotka &sp = szprotki[q.szprotkaIndex]; sp.makeDead(); // add '0' to Binary BitTree (1 - szprotka alive, 0 - szprotka dead) // TODO: decrease BitIndexTree in element X } K3DBG(print_szprotki(szprotki)); } } void process(vector<Query> &queries, vector<Szprotka> &szprotki) { K3DBG(printf("After pre-processing. Processing queries:\n")); for (int i=0; i < queries.size(); i++) { Query q = queries[i]; if (q.queryType==Query::QUERY_TYPE_DODAJ) { szprotki.push_back(Szprotka(q.wi, szprotki.size(), i, q.wi)); } } sort(szprotki.begin(), szprotki.end()); for (int i=0; i < szprotki.size(); i++) { szprotki[i].arrIndex = i; if (szprotki[i].queryIndex >= 0) { szprotki[i].waga = 0; queries[szprotki[i].queryIndex].szprotkaIndex = i; } } K3DBG(print_szprotki(szprotki)); //unordered_map<pair<LL, int>, int> szprotkaIndexMap; unordered_map<LL, int> szprotkaFirstIndexMap; for (int i=0; i < szprotki.size(); i++) { Szprotka &sp = szprotki[i]; if (szprotki[i].waga>0) { if (szprotkaFirstIndexMap.find(sp.waga) == szprotkaFirstIndexMap.end()) { szprotkaFirstIndexMap[sp.waga] = i; } } } vector<Szprotka> copySzprotki = szprotki; for (int i=0; i < queries.size(); i++) { Query &q = queries[i]; //printf("pre-processing query %d type=%d\n", i, q.queryType); if (q.queryType == Query::QUERY_TYPE_DODAJ) { Szprotka &sp = copySzprotki[q.szprotkaIndex]; sp.makeAlive(); //printf("makeAlive szprotka w=%lld\n", sp.waga); if (szprotkaFirstIndexMap.find(sp.waga) == szprotkaFirstIndexMap.end()) { szprotkaFirstIndexMap[sp.waga] = q.szprotkaIndex; } } if (q.queryType==Query::QUERY_TYPE_USUN) { int idx = szprotkaFirstIndexMap[q.wi]; //printf("found candidate at idx=%d ", idx); szprotki[idx].print(); printf("\n"); // TODO: could be logaritmic here while (copySzprotki[idx].waga == 0) { //printf("increasing idx...\n"); idx++; } //printf("Usun: q.wi=%lld szprotki[idx].waga=%lld\n", q.wi, szprotki[idx].waga); ASSERT_EQ(copySzprotki[idx].waga, q.wi); szprotkaFirstIndexMap[q.wi] = idx; q.szprotkaIndex = idx; copySzprotki[idx].makeDead(); } } // printf("pre-process initial szczupak index\n"); vector<Query> qrSzczupaki; for (int i=0; i < queries.size(); i++) { Query &q = queries[i]; if (q.queryType == Query::QUERY_TYPE_SZCZUPAK) { qrSzczupaki.push_back(q); } } sort(qrSzczupaki.begin(), qrSzczupaki.end(), [](const Query& lhs, Query& rhs){ return lhs.S < rhs.S; }); int si = 0; for (int j=0; j < qrSzczupaki.size(); j++) { Query &q = qrSzczupaki[j]; while (si < szprotki.size() && szprotki[si].potencjalnaWaga < q.S) { si++; } queries[q.arrIndex].szprotkaIndex = si; } // TODO: answerQueries(queries, szprotki); } int main() { // selftest(); vector<Szprotka> szprotki; LL n; scanf("%lld",&n); for (int i=0; i < n; i++) { LL wi; scanf("%lld", &wi); szprotki.push_back(Szprotka(wi, i, -1, wi)); } LL qs; vector<Query> queries; (void)scanf("%lld", &qs); for (int i=0; i < qs; i++) { int queryType; (void)scanf("%d", &queryType); switch (queryType) { case 1: { LL szczupakK, szczupakS; (void)scanf("%lld%lld", &szczupakS, &szczupakK); queries.push_back(Query(1, szczupakS, szczupakK, i)); break; } case 2:{ LL wi; (void)scanf("%lld", &wi); queries.push_back(Query(2, wi, i)); break; } case 3:{ LL wi; (void)scanf("%lld", &wi); queries.push_back(Query(3, wi, i)); break; } } } process(queries, szprotki); 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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 | #include <algorithm> #include <map> #include <vector> #include <unordered_map> #include <set> #define LL long long #define BIGMOD 1000012177LL #define DNUM 31LL #define K3DBG(X) using namespace std; struct pair_hash { template <class T1, class T2> std::size_t operator() (const std::pair<T1, T2> &pair) const { return std::hash<T1>()(pair.first) ^ std::hash<T2>()(pair.second); } }; class BitTree { public: LL *mData; int mN; BitTree(int n) : mN(n) { mData = new LL[mN+1]; for (int i=0; i<=mN; i++) { mData[i] = 0; } } ~BitTree() { delete[] mData; } // Updates a node in Binary Index Tree (BITree) at given index // in BITree. The given value 'val' is added to BITree[i] and // all of its ancestors in tree. void updateBIT(int index, LL val) { // index in BITree[] is 1 more than the index in arr[] index = index + 1; // Traverse all ancestors and add 'val' while (index <= mN) { // Add 'val' to current node of BI Tree mData[index] += val; // Update index to that of parent in update View index += index & (-index); } } LL getElem(int index) { LL res = getSum(index); // printf("bt::getElem(%d)=%lld\n", index, res); return res; } // SERVES THE PURPOSE OF getElement() // Returns sum of arr[0..index]. This function assumes // that the array is preprocessed and partial sums of // array elements are stored in BITree[] LL getSum(int index) { LL sum = 0; // Iniialize result // index in BITree[] is 1 more than the index in arr[] index = index + 1; // Traverse ancestors of BITree[index] while (index>0) { // Add current element of BITree to sum sum += mData[index]; // Move index to parent node in getSum View index -= index & (-index); } return sum; } // Updates such that getElement() gets an increased // value when queried from l to r. void update(int l, int r, LL val) { // Increase value at 'l' by 'val' updateBIT(l, val); // Decrease value at 'r+1' by 'val' updateBIT(r+1, -val); } void print() { printf("BitTree="); for (int i=0; i < mN; i++) { printf("%lld ", getElem(i)); } printf("\n"); } }; class PresenceIndicator { // TODO: w oparciu o drzewko licznikowe void mark(bool isPresent) { } int countDead(int left, int right) { } int countAlive(int left, int right) { } }; class RangeSumSolver { LL rangeSum(int left, int right) { return 0; } }; #define ASSERT_EQ(X, Y) // { if (!(X == Y)) printf(#X " != " #Y); } #define ASSERT_TRUE(X) // { if (!(X)) printf(#X " is false!"); } void selftest() { BitTree bt(100); bt.updateBIT(2,1); bt.updateBIT(4,1); bt.updateBIT(5, 2); bt.updateBIT(10,1); for (int i=0; i < 20; i++) { printf("%d %lld\n", i, bt.getElem(i)); } } class Query { public: const static int QUERY_TYPE_SZCZUPAK = 1; const static int QUERY_TYPE_DODAJ = 2; const static int QUERY_TYPE_USUN = 3; int queryType; LL wi; LL S, K; int arrIndex; int szprotkaIndex; Query() : queryType(0), wi(0), S(0), K(0), szprotkaIndex(-1) {} Query(int queryType, LL wi, int arrIndex) : queryType(queryType), wi(wi), S(0), K(0), szprotkaIndex(-1), arrIndex(arrIndex) {} Query(int queryType, LL S, LL K, int arrIndex) : queryType(queryType), wi(0), S(S), K(K), szprotkaIndex(-1), arrIndex(arrIndex) {} void print() const { if (queryType == 1) { printf("Query(qt:%d (%lld->%lld) [ai:%d,si:%d])", queryType, S, K, arrIndex, szprotkaIndex); } else { printf("Query(qt:%d (w:%lld) [ai:%d,si:%d])", queryType, wi, arrIndex, szprotkaIndex); } } }; class Szprotka { public: LL waga; int arrIndex; int queryIndex; LL potencjalnaWaga; // dla szprotek jeszcze nie instniejacych Szprotka(LL waga, int arrIndex, int queryIndex, LL potencjalnaWaga) : waga(waga), arrIndex(arrIndex), queryIndex(queryIndex), potencjalnaWaga(potencjalnaWaga) {} bool operator<(const Szprotka& peer) { if (waga == peer.waga) { return arrIndex < peer.arrIndex; } return waga < peer.waga; } void print() const { printf("Szp:(w=%lld", waga); if (queryIndex>=0) { printf(",q(%d)->%lld)", queryIndex, potencjalnaWaga); } else { printf(")"); } } void makeAlive() { if (waga != 0) printf("INCORRECT assumption: expected dead, got: alive\n"); waga = potencjalnaWaga; } void makeDead() { if (waga == 0) printf("INCORRECT assumption: expected Alive, got: dead"); waga = 0; } }; void print_szprotki(vector<Szprotka> &szprotki) { printf("szprotki:\n"); for (int i=0; i < szprotki.size(); i++) { szprotki[i].print();printf("\n"); } } class SzczupakAttacker { static int alreadyProcessed[400100]; static int alreadyProcessedCounter; static int jumpDown[400100]; public: const vector<Szprotka> &szprotki; LL S, K; SzczupakAttacker(const vector<Szprotka> &szprotki, LL S, LL K) : szprotki{szprotki}, S(S), K(K) { alreadyProcessedCounter++; } int moveSzczupak(int idx) { int p2 = 1; int lastOK = idx; int limit = szprotki.size(); while (idx < limit && S > szprotki[idx].potencjalnaWaga) { while (idx + p2 < limit && S > szprotki[idx+p2].potencjalnaWaga) { lastOK = idx + p2; p2 *= 2; } idx++; if (lastOK > idx) idx = lastOK; } return idx; } LL attack(int initialSzprotkaIndex) { LL totalEatenCount = 0; LL totalEateanWeight = 0; LL szprotkiSize = szprotki.size(); //printf("Szczupak rozmiar %lld zaczyna od szprotki %d\n", S, initialSzprotkaIndex); // To jest pierwsza, ktorej szczupak nie jest w stanie zjesc. Byc moze poza tablica (idx=szprotki.size()) int szczupakPos = initialSzprotkaIndex; ASSERT_TRUE(szczupakPos >= 0); while (S < K) { int i = szczupakPos - 1; if (K - S <= 0) return totalEatenCount; LL diff = K - S; if (szczupakPos < szprotkiSize) { diff = min(diff, 1 + szprotki[szczupakPos].potencjalnaWaga - S); } LL localSum = 0; LL localEaten = 0; int initialPos = i; while (i >= 0 && localSum < diff) { if (alreadyProcessed[i] == alreadyProcessedCounter) { int jump = jumpDown[i]; if (jump > i) { jump = jumpDown[jump]; } if (jump > 0 && jump < i) { i = jump; } } const Szprotka &sp = szprotki[i]; //printf("Trying szprotka %d ", i); sp.print(); printf("\n"); if (sp.waga && alreadyProcessed[i] != alreadyProcessedCounter) { localSum += sp.waga; localEaten++; alreadyProcessed[i] = alreadyProcessedCounter; jumpDown[i] = initialPos; //printf("Eaten szprotka "); sp.print(); printf("\n"); } i--; } if (initialPos > 0) { alreadyProcessed[initialPos] = alreadyProcessedCounter; jumpDown[initialPos] = i; } S += localSum; // printf("new szczupak weight: %lld\n", S); // jesli nie urusl if (localSum == 0) { return -1; } szczupakPos = moveSzczupak(szczupakPos); totalEateanWeight += localSum; totalEatenCount += localEaten; // ASSERRT: S >= szprotki[idx].potencjalnaWaga lub idx == szprotki.size() } return totalEatenCount; } }; int SzczupakAttacker::alreadyProcessed[400100] = {0}; int SzczupakAttacker::alreadyProcessedCounter = 1; int SzczupakAttacker::jumpDown[400100] = {0}; void answerQueries(const vector<Query> &queries, vector<Szprotka> &szprotki) { K3DBG(print_szprotki(szprotki)); for (int i=0; i < queries.size(); i++) { const Query &q = queries[i]; K3DBG(printf("Answering query Q=")); K3DBG(q.print()); K3DBG(printf("\n")); ASSERT_TRUE(q.szprotkaIndex >= 0); if (q.queryType == Query::QUERY_TYPE_SZCZUPAK) { K3DBG(printf("Szczupak %lld -> %lld\n", q.S, q.K)); SzczupakAttacker szczupak(szprotki, q.S, q.K); printf("%lld\n", szczupak.attack(q.szprotkaIndex)); } if (q.queryType == Query::QUERY_TYPE_DODAJ) { ASSERT_TRUE(q.szprotkaIndex >= 0); K3DBG(printf("wzbudzam szprotke %d do wagi %lld\n", q.szprotkaIndex, szprotki[q.szprotkaIndex].potencjalnaWaga)); Szprotka &sp = szprotki[q.szprotkaIndex]; sp.makeAlive(); // TODO: increase BitIndexTree in element X } if (q.queryType == Query::QUERY_TYPE_USUN) { K3DBG(printf("zaznaczam szprotke %d jaka martwa\n", q.szprotkaIndex)); Szprotka &sp = szprotki[q.szprotkaIndex]; sp.makeDead(); // add '0' to Binary BitTree (1 - szprotka alive, 0 - szprotka dead) // TODO: decrease BitIndexTree in element X } K3DBG(print_szprotki(szprotki)); } } void process(vector<Query> &queries, vector<Szprotka> &szprotki) { K3DBG(printf("After pre-processing. Processing queries:\n")); for (int i=0; i < queries.size(); i++) { Query q = queries[i]; if (q.queryType==Query::QUERY_TYPE_DODAJ) { szprotki.push_back(Szprotka(q.wi, szprotki.size(), i, q.wi)); } } sort(szprotki.begin(), szprotki.end()); for (int i=0; i < szprotki.size(); i++) { szprotki[i].arrIndex = i; if (szprotki[i].queryIndex >= 0) { szprotki[i].waga = 0; queries[szprotki[i].queryIndex].szprotkaIndex = i; } } K3DBG(print_szprotki(szprotki)); //unordered_map<pair<LL, int>, int> szprotkaIndexMap; unordered_map<LL, int> szprotkaFirstIndexMap; for (int i=0; i < szprotki.size(); i++) { Szprotka &sp = szprotki[i]; if (szprotki[i].waga>0) { if (szprotkaFirstIndexMap.find(sp.waga) == szprotkaFirstIndexMap.end()) { szprotkaFirstIndexMap[sp.waga] = i; } } } vector<Szprotka> copySzprotki = szprotki; for (int i=0; i < queries.size(); i++) { Query &q = queries[i]; //printf("pre-processing query %d type=%d\n", i, q.queryType); if (q.queryType == Query::QUERY_TYPE_DODAJ) { Szprotka &sp = copySzprotki[q.szprotkaIndex]; sp.makeAlive(); //printf("makeAlive szprotka w=%lld\n", sp.waga); if (szprotkaFirstIndexMap.find(sp.waga) == szprotkaFirstIndexMap.end()) { szprotkaFirstIndexMap[sp.waga] = q.szprotkaIndex; } } if (q.queryType==Query::QUERY_TYPE_USUN) { int idx = szprotkaFirstIndexMap[q.wi]; //printf("found candidate at idx=%d ", idx); szprotki[idx].print(); printf("\n"); // TODO: could be logaritmic here while (copySzprotki[idx].waga == 0) { //printf("increasing idx...\n"); idx++; } //printf("Usun: q.wi=%lld szprotki[idx].waga=%lld\n", q.wi, szprotki[idx].waga); ASSERT_EQ(copySzprotki[idx].waga, q.wi); szprotkaFirstIndexMap[q.wi] = idx; q.szprotkaIndex = idx; copySzprotki[idx].makeDead(); } } // printf("pre-process initial szczupak index\n"); vector<Query> qrSzczupaki; for (int i=0; i < queries.size(); i++) { Query &q = queries[i]; if (q.queryType == Query::QUERY_TYPE_SZCZUPAK) { qrSzczupaki.push_back(q); } } sort(qrSzczupaki.begin(), qrSzczupaki.end(), [](const Query& lhs, Query& rhs){ return lhs.S < rhs.S; }); int si = 0; for (int j=0; j < qrSzczupaki.size(); j++) { Query &q = qrSzczupaki[j]; while (si < szprotki.size() && szprotki[si].potencjalnaWaga < q.S) { si++; } queries[q.arrIndex].szprotkaIndex = si; } // TODO: answerQueries(queries, szprotki); } int main() { // selftest(); vector<Szprotka> szprotki; LL n; scanf("%lld",&n); for (int i=0; i < n; i++) { LL wi; scanf("%lld", &wi); szprotki.push_back(Szprotka(wi, i, -1, wi)); } LL qs; vector<Query> queries; (void)scanf("%lld", &qs); for (int i=0; i < qs; i++) { int queryType; (void)scanf("%d", &queryType); switch (queryType) { case 1: { LL szczupakK, szczupakS; (void)scanf("%lld%lld", &szczupakS, &szczupakK); queries.push_back(Query(1, szczupakS, szczupakK, i)); break; } case 2:{ LL wi; (void)scanf("%lld", &wi); queries.push_back(Query(2, wi, i)); break; } case 3:{ LL wi; (void)scanf("%lld", &wi); queries.push_back(Query(3, wi, i)); break; } } } process(queries, szprotki); return 0; } |