#include <stdio.h> #include <string.h> #include <set> #include <iterator> // O(n*lgn * q) int n, q, a; signed long long int b, c; std::multiset <signed long long int, std::less <signed long long int> > w; std::multiset <signed long long int, std::less <signed long long int> > prey; std::multiset <signed long long int, std::less <signed long long int> > :: iterator itr; signed long long int getMaxSmaller(signed long long int x) { itr = w.upper_bound(0 - x); if (itr == w.end()) { return 0; } // printf("WTD: "); return *(itr); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &b); w.insert(0 - b); } scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d", &a); if (a == 1) { scanf("%lld %lld", &b, &c); // printf("><===>! (%llu -> %llu)\n", b, c); int ret = 0; while(b < c) { signed long long int max; max = getMaxSmaller(b); if (max == 0) { break; } // printf(" max: %llu\n", max); w.erase(w.find(max)); prey.insert(max); b -= max; ret++; } for (itr = prey.begin(); itr != prey.end(); ++itr) { w.insert(*itr); } prey.clear(); printf("%d\n", (b < c ? -1 : ret)); } else if (a == 2) { scanf("%lld", &b); w.insert(0 - b); // printf("+ ><> (%llu) [%d]\n", b, w.size()); } else { scanf("%lld", &b); w.erase(w.find(0 - b)); // printf("- <>< (%llu) [%d]\n", b, w.size()); } } /* for (itr = w.begin(); itr != w.end(); ++itr) { printf("%lld, ", *itr); } printf("\n"); for (int i = 0; i < 20; i++) { printf("%d %lld\n", i*10+1, getMaxSmaller(i*10+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 | #include <stdio.h> #include <string.h> #include <set> #include <iterator> // O(n*lgn * q) int n, q, a; signed long long int b, c; std::multiset <signed long long int, std::less <signed long long int> > w; std::multiset <signed long long int, std::less <signed long long int> > prey; std::multiset <signed long long int, std::less <signed long long int> > :: iterator itr; signed long long int getMaxSmaller(signed long long int x) { itr = w.upper_bound(0 - x); if (itr == w.end()) { return 0; } // printf("WTD: "); return *(itr); } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &b); w.insert(0 - b); } scanf("%d", &q); for (int i = 0; i < q; i++) { scanf("%d", &a); if (a == 1) { scanf("%lld %lld", &b, &c); // printf("><===>! (%llu -> %llu)\n", b, c); int ret = 0; while(b < c) { signed long long int max; max = getMaxSmaller(b); if (max == 0) { break; } // printf(" max: %llu\n", max); w.erase(w.find(max)); prey.insert(max); b -= max; ret++; } for (itr = prey.begin(); itr != prey.end(); ++itr) { w.insert(*itr); } prey.clear(); printf("%d\n", (b < c ? -1 : ret)); } else if (a == 2) { scanf("%lld", &b); w.insert(0 - b); // printf("+ ><> (%llu) [%d]\n", b, w.size()); } else { scanf("%lld", &b); w.erase(w.find(0 - b)); // printf("- <>< (%llu) [%d]\n", b, w.size()); } } /* for (itr = w.begin(); itr != w.end(); ++itr) { printf("%lld, ", *itr); } printf("\n"); for (int i = 0; i < 20; i++) { printf("%d %lld\n", i*10+1, getMaxSmaller(i*10+1)); }*/ return 0; } |