#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, n) for(auto &i : n) #define SZ(x) (int)(x).size() #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; const ll INF = 1000000000ll*1000000000ll; std::vector<ll> P; int pos(ll a){ return std::lower_bound(P.begin(), P.end(), a)-P.begin(); } struct Tree{ int BOTTOM; std::vector<ll> t; VI ile; void build(VI bd){ BOTTOM = 1; while(BOTTOM < SZ(P)) BOTTOM *= 2; t.resize(BOTTOM*2); ile.resize(BOTTOM*2); FOR(i, SZ(bd)) t[i+BOTTOM] = P[i]*bd[i], ile[i+BOTTOM] = bd[i]; for(int i = BOTTOM-1; i >= 1; --i) t[i] = t[i<<1] + t[(i<<1)+1], ile[i] = ile[i<<1] + ile[(i<<1)+1]; } void add(int a, int what){ ll val = what*P[a]; a += BOTTOM; t[a] += val; ile[a] += what; while(a > 1){ a >>= 1; t[a] += val; ile[a] += what; } } PII ile_last; PR<int, ll> query(int a, int b, ll chce, int v=1, int lo=0, int hi=-2){ if(hi==-2) hi = BOTTOM-1; if(a>b) return MP(0, 0ll); if(a==lo && b==hi){ if(t[v] <= chce){ ile_last = MP(lo, -1); return MP(ile[v], t[v]); } if(lo == hi){ ll val = P[lo]; int potrz = chce/val + (chce%val == 0 ? 0 : 1); ile_last = MP(lo, potrz); if(potrz == ile[v]) ile_last.Y = -1; return MP(potrz, val*potrz); } int mid = (lo+hi)/2; auto pr = query(mid+1, hi, chce, (v<<1)+1, mid+1, hi); chce -= pr.Y; if(chce <= 0) return pr; auto pr2 = query(lo, mid, chce, v<<1, lo, mid); chce -= pr2.Y; pr.X += pr2.X; pr.Y += pr2.Y; return pr; } int mid = (lo+hi)/2; auto pr = query(std::max(a, mid+1), b, chce, (v<<1)+1, mid+1, hi); chce -= pr.Y; if(chce <= 0) return pr; auto pr2 = query(a, std::min(b, mid), chce, v<<1, lo, mid); chce -= pr2.Y; pr.X += pr2.X; pr.Y += pr2.Y; return pr; } }; int main(){ int n; std::scanf("%d", &n); std::vector<ll> A(n); FOR(i, n) std::scanf("%lld", &A[i]), P.push_back(A[i]); int que; std::scanf("%d", &que); std::vector<PR<int, PR<ll, ll> > > Q; FOR(i, que){ int what; std::scanf("%d", &what); if(what == 1){ ll s, k; std::scanf("%lld%lld", &s, &k); Q.push_back(MP(what, MP(s, k))); }else{ ll w; std::scanf("%lld", &w); Q.push_back(MP(what, MP(w, -1ll))); if(what == 2) P.push_back(w); } } std::sort(P.begin(), P.end()); P.erase(std::unique(P.begin(), P.end()), P.end()); Tree tree; std::multiset<ll> ryby; VI build(SZ(P)); TRAV(i, A){ build[pos(i)]++; ryby.insert(i); } tree.build(build); TRAV(q, Q){ if(q.X == 1){ ll s = q.Y.X; ll k = q.Y.Y; if(s >= k) std::printf("0\n"); else{ int cnt = 0; std::vector<PII> block; std::vector<PII> add; block.push_back(MP(-1, 0)); while(s < k){ auto next = ryby.lower_bound(s); if(next == ryby.begin()){ cnt = -1; break; } ll ile = k-s; if(next != ryby.end()) ile = std::min(ile, *next-s+1); int prw; if(next != ryby.end()) prw = pos(*next)-1; else prw = SZ(P)-1; if(prw == block.back().X){ prw -= block.back().Y; if(prw < 0){ cnt = -1; break; } int lw = block[SZ(block)-2].X+1; tree.ile_last = MP(-1, -1); auto pr = tree.query(lw, prw, ile); if(tree.ile_last.Y != -1){ tree.add(tree.ile_last.X, -tree.ile_last.Y); add.emplace_back(tree.ile_last.X, tree.ile_last.Y); tree.ile_last.X++; tree.ile_last.Y = -1; } int myl = tree.ile_last.X; int myr = prw; if(myl > myr){ }else if(myl == block[SZ(block)-2].X+1){ PII nw = MP(block.back().X, block.back().X -(block[SZ(block)-2].X-block[SZ(block)-2].Y)); block.pop_back(); block.pop_back(); block.push_back(nw); }else{ block.back().Y = block.back().X-myl+1; } cnt += pr.X; s += pr.Y; }else{ int lw = block.back().X+1; tree.ile_last = MP(-1, -1); auto pr = tree.query(lw, prw, ile); if(tree.ile_last.Y != -1){ tree.add(tree.ile_last.X, -tree.ile_last.Y); add.emplace_back(tree.ile_last.X, tree.ile_last.Y); tree.ile_last.X++; tree.ile_last.Y = -1; } int myl = tree.ile_last.X; int myr = prw; if(myl > myr){ }else if(myl == block.back().X+1){ block.back().Y += myr-myl+1; block.back().X = myr; }else{ block.push_back(MP(myr, myr-myl+1)); } cnt += pr.X; s += pr.Y; } } TRAV(pr, add) tree.add(pr.X, pr.Y); std::printf("%d\n", cnt); } }else if(q.X == 2){ tree.add(pos(q.Y.X), 1); ryby.insert(q.Y.X); }else{ tree.add(pos(q.Y.X), -1); ryby.erase(ryby.find(q.Y.X)); } } 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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, n) for(auto &i : n) #define SZ(x) (int)(x).size() #define PR std::pair #define MP std::make_pair #define X first #define Y second typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; const ll INF = 1000000000ll*1000000000ll; std::vector<ll> P; int pos(ll a){ return std::lower_bound(P.begin(), P.end(), a)-P.begin(); } struct Tree{ int BOTTOM; std::vector<ll> t; VI ile; void build(VI bd){ BOTTOM = 1; while(BOTTOM < SZ(P)) BOTTOM *= 2; t.resize(BOTTOM*2); ile.resize(BOTTOM*2); FOR(i, SZ(bd)) t[i+BOTTOM] = P[i]*bd[i], ile[i+BOTTOM] = bd[i]; for(int i = BOTTOM-1; i >= 1; --i) t[i] = t[i<<1] + t[(i<<1)+1], ile[i] = ile[i<<1] + ile[(i<<1)+1]; } void add(int a, int what){ ll val = what*P[a]; a += BOTTOM; t[a] += val; ile[a] += what; while(a > 1){ a >>= 1; t[a] += val; ile[a] += what; } } PII ile_last; PR<int, ll> query(int a, int b, ll chce, int v=1, int lo=0, int hi=-2){ if(hi==-2) hi = BOTTOM-1; if(a>b) return MP(0, 0ll); if(a==lo && b==hi){ if(t[v] <= chce){ ile_last = MP(lo, -1); return MP(ile[v], t[v]); } if(lo == hi){ ll val = P[lo]; int potrz = chce/val + (chce%val == 0 ? 0 : 1); ile_last = MP(lo, potrz); if(potrz == ile[v]) ile_last.Y = -1; return MP(potrz, val*potrz); } int mid = (lo+hi)/2; auto pr = query(mid+1, hi, chce, (v<<1)+1, mid+1, hi); chce -= pr.Y; if(chce <= 0) return pr; auto pr2 = query(lo, mid, chce, v<<1, lo, mid); chce -= pr2.Y; pr.X += pr2.X; pr.Y += pr2.Y; return pr; } int mid = (lo+hi)/2; auto pr = query(std::max(a, mid+1), b, chce, (v<<1)+1, mid+1, hi); chce -= pr.Y; if(chce <= 0) return pr; auto pr2 = query(a, std::min(b, mid), chce, v<<1, lo, mid); chce -= pr2.Y; pr.X += pr2.X; pr.Y += pr2.Y; return pr; } }; int main(){ int n; std::scanf("%d", &n); std::vector<ll> A(n); FOR(i, n) std::scanf("%lld", &A[i]), P.push_back(A[i]); int que; std::scanf("%d", &que); std::vector<PR<int, PR<ll, ll> > > Q; FOR(i, que){ int what; std::scanf("%d", &what); if(what == 1){ ll s, k; std::scanf("%lld%lld", &s, &k); Q.push_back(MP(what, MP(s, k))); }else{ ll w; std::scanf("%lld", &w); Q.push_back(MP(what, MP(w, -1ll))); if(what == 2) P.push_back(w); } } std::sort(P.begin(), P.end()); P.erase(std::unique(P.begin(), P.end()), P.end()); Tree tree; std::multiset<ll> ryby; VI build(SZ(P)); TRAV(i, A){ build[pos(i)]++; ryby.insert(i); } tree.build(build); TRAV(q, Q){ if(q.X == 1){ ll s = q.Y.X; ll k = q.Y.Y; if(s >= k) std::printf("0\n"); else{ int cnt = 0; std::vector<PII> block; std::vector<PII> add; block.push_back(MP(-1, 0)); while(s < k){ auto next = ryby.lower_bound(s); if(next == ryby.begin()){ cnt = -1; break; } ll ile = k-s; if(next != ryby.end()) ile = std::min(ile, *next-s+1); int prw; if(next != ryby.end()) prw = pos(*next)-1; else prw = SZ(P)-1; if(prw == block.back().X){ prw -= block.back().Y; if(prw < 0){ cnt = -1; break; } int lw = block[SZ(block)-2].X+1; tree.ile_last = MP(-1, -1); auto pr = tree.query(lw, prw, ile); if(tree.ile_last.Y != -1){ tree.add(tree.ile_last.X, -tree.ile_last.Y); add.emplace_back(tree.ile_last.X, tree.ile_last.Y); tree.ile_last.X++; tree.ile_last.Y = -1; } int myl = tree.ile_last.X; int myr = prw; if(myl > myr){ }else if(myl == block[SZ(block)-2].X+1){ PII nw = MP(block.back().X, block.back().X -(block[SZ(block)-2].X-block[SZ(block)-2].Y)); block.pop_back(); block.pop_back(); block.push_back(nw); }else{ block.back().Y = block.back().X-myl+1; } cnt += pr.X; s += pr.Y; }else{ int lw = block.back().X+1; tree.ile_last = MP(-1, -1); auto pr = tree.query(lw, prw, ile); if(tree.ile_last.Y != -1){ tree.add(tree.ile_last.X, -tree.ile_last.Y); add.emplace_back(tree.ile_last.X, tree.ile_last.Y); tree.ile_last.X++; tree.ile_last.Y = -1; } int myl = tree.ile_last.X; int myr = prw; if(myl > myr){ }else if(myl == block.back().X+1){ block.back().Y += myr-myl+1; block.back().X = myr; }else{ block.push_back(MP(myr, myr-myl+1)); } cnt += pr.X; s += pr.Y; } } TRAV(pr, add) tree.add(pr.X, pr.Y); std::printf("%d\n", cnt); } }else if(q.X == 2){ tree.add(pos(q.Y.X), 1); ryby.insert(q.Y.X); }else{ tree.add(pos(q.Y.X), -1); ryby.erase(ryby.find(q.Y.X)); } } return 0; } |