#include<bits/stdc++.h> #define putchar putchar_unlocked #define getchar getchar_unlocked //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //#define indexed_set tree<pair<long long,long long>, null_type, less<pair<long long,long long>>, rb_tree_tag, tree_order_statistics_node_update> using namespace std; //using namespace __gnu_pbds; long long sum=0; map<long long, int>mp; map<long long, int>::iterator it; inline void writeI(int_fast32_t l) { if(!l) { putchar(48); } else { if(l<0) { l^=-1; ++l; putchar(45); } uint_fast32_t p=0, h[21]; while(l) { ++p; h[p]=(l%10)+48; l/=10; } for(uint_fast32_t i=p+1;--i;) { putchar(h[i]); } } } inline void readUI(uint_fast32_t &i) { register uint_fast32_t r=0; register char c=getchar(); while(c<48 || c > 57) { c=getchar(); } while(c>=48 && c<=57) { r=(r<<3)+(r<<1)+c-48; c=getchar(); } i=r; } inline void readULL(uint_fast64_t &l) { register uint_fast64_t r=0; register char c=getchar(); while(c<48 || c>57) { c=getchar(); } while(c>=48 && c<=57) { r=(r<<3)+(r<<1)+c-48; c=getchar(); } l=r; } int_fast32_t atak(uint_fast64_t &teraz, uint_fast64_t &docel) { vector<uint_fast64_t>tab; int_fast32_t licznik=0; if(sum+teraz<docel) return -1; if(teraz>=docel) return 0; while(teraz<docel) { it=mp.lower_bound(teraz); if(it==mp.begin()) { for(auto e:tab) ++mp[e]; return -1; } --it; teraz+=it->first; tab.push_back(it->first); --(it->second); if(it->second==0) mp.erase(it); ++licznik; } for(auto e:tab) ++mp[e]; return licznik; } int_fast32_t main() { uint_fast64_t tmp, s, k; uint_fast32_t n, q, r; readUI(n);//scanf("%d", &n); for(int i=0;i^n;++i) { readULL(tmp);//scanf("%lld", &tmp); sum+=tmp; ++mp[tmp]; } readUI(q);//scanf("%d", &q); ++q; while(--q) { readUI(r);//scanf("%d", &r); switch(r) { case 1: readULL(s);readULL(k);//scanf("%llu%llu", &s, &k); writeI(atak(s,k));//printf("%d\n", atak(s, k)); putchar(10); break; case 2: readULL(s);//scanf("%llu", &s); ++mp[s]; sum+=s; break; default: readULL(s);//scanf("%llu", &s); it=mp.find(s); if(it->second==1) mp.erase(it); else --(it->second); sum-=s; } } 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 | #include<bits/stdc++.h> #define putchar putchar_unlocked #define getchar getchar_unlocked //#include <ext/pb_ds/assoc_container.hpp> //#include <ext/pb_ds/tree_policy.hpp> //#define indexed_set tree<pair<long long,long long>, null_type, less<pair<long long,long long>>, rb_tree_tag, tree_order_statistics_node_update> using namespace std; //using namespace __gnu_pbds; long long sum=0; map<long long, int>mp; map<long long, int>::iterator it; inline void writeI(int_fast32_t l) { if(!l) { putchar(48); } else { if(l<0) { l^=-1; ++l; putchar(45); } uint_fast32_t p=0, h[21]; while(l) { ++p; h[p]=(l%10)+48; l/=10; } for(uint_fast32_t i=p+1;--i;) { putchar(h[i]); } } } inline void readUI(uint_fast32_t &i) { register uint_fast32_t r=0; register char c=getchar(); while(c<48 || c > 57) { c=getchar(); } while(c>=48 && c<=57) { r=(r<<3)+(r<<1)+c-48; c=getchar(); } i=r; } inline void readULL(uint_fast64_t &l) { register uint_fast64_t r=0; register char c=getchar(); while(c<48 || c>57) { c=getchar(); } while(c>=48 && c<=57) { r=(r<<3)+(r<<1)+c-48; c=getchar(); } l=r; } int_fast32_t atak(uint_fast64_t &teraz, uint_fast64_t &docel) { vector<uint_fast64_t>tab; int_fast32_t licznik=0; if(sum+teraz<docel) return -1; if(teraz>=docel) return 0; while(teraz<docel) { it=mp.lower_bound(teraz); if(it==mp.begin()) { for(auto e:tab) ++mp[e]; return -1; } --it; teraz+=it->first; tab.push_back(it->first); --(it->second); if(it->second==0) mp.erase(it); ++licznik; } for(auto e:tab) ++mp[e]; return licznik; } int_fast32_t main() { uint_fast64_t tmp, s, k; uint_fast32_t n, q, r; readUI(n);//scanf("%d", &n); for(int i=0;i^n;++i) { readULL(tmp);//scanf("%lld", &tmp); sum+=tmp; ++mp[tmp]; } readUI(q);//scanf("%d", &q); ++q; while(--q) { readUI(r);//scanf("%d", &r); switch(r) { case 1: readULL(s);readULL(k);//scanf("%llu%llu", &s, &k); writeI(atak(s,k));//printf("%d\n", atak(s, k)); putchar(10); break; case 2: readULL(s);//scanf("%llu", &s); ++mp[s]; sum+=s; break; default: readULL(s);//scanf("%llu", &s); it=mp.find(s); if(it->second==1) mp.erase(it); else --(it->second); sum-=s; } } return 0; } |