#include <bits/stdc++.h> using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef long double ldbl; typedef pair<int, int> pii; typedef pair<uint, uint> puu; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<uint> vu; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pii> vpii; typedef vector<puu> vpuu; typedef vector<pll> vpll; typedef vector<pull> vpull; typedef vector<string> vstr; typedef vector<double> vdbl; typedef vector<ldbl> vldbl; #define pb push_back #define ppb pop_back #define pfr push_front #define ppfr pop_front #define emp emplace #define empb emplace_back #define be begin #define rbe rbegin #define all(x) (x).be(), (x).end() #define rall(x) (x).rbe(), (x).rend() #define fir first #define sec second #define mkp make_pair #define brif(cond) if (cond) break #define ctif(cond) if (cond) continue #define retif(cond) if (cond) return void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);} template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;} template<typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a/b; a %= b; swap(a, b); x0 -= q*x1; swap(x0, x1); y0 -= q*y1; swap(y0, y1); } x = x0; y = y0; return a; } int ctz(uint x) {return __builtin_ctz(x);} int ctzll(ull x) {return __builtin_ctzll(x);} int clz(uint x) {return __builtin_clz(x);} int clzll(ull x) {return __builtin_clzll(x);} int popcnt(uint x) {return __builtin_popcount(x);} int popcntll(ull x) {return __builtin_popcountll(x);} int bsr(uint x) {return 31^clz(x);} int bsrll(ull x) {return 63^clzll(x);} #define N 300016 #define Q 100016 #define M (N+Q) /// 400032 #define SZ (1<<20) /// nextpow2(2*M-1) set<ll> vset; int cnt[M]; ll v[M]; ll w[N]; pll queries[Q]; bool rflag[SZ]; int treecnt[SZ], refcnt[SZ]; ll treesum[SZ], refsum[SZ]; int get_id(int m, ll x) { return lower_bound(v, v+m, x)-v; } /// perm tree void build(int u, int b, int e) { if (b+1 == e) { refcnt[u] = cnt[b]; refsum[u] = cnt[b]*v[b]; return; } int m = (b+e)/2, lc = 2*u, rc = 2*u+1; build(lc, b, m); build(rc, m, e); refcnt[u] = refcnt[lc]+refcnt[rc]; refsum[u] = refsum[lc]+refsum[rc]; } /// perm tree void add_fish(int m, ll x) { int u = 1, b = 0, e = m; int i = get_id(m, x); while (b+1 < e) { int mid = (b+e)/2; u *= 2; if (i < mid) e = mid; else ++u, b = mid; } ++refcnt[u]; refsum[u] += x; while (u > 1) { int p = u/2; refcnt[p] = refcnt[u]+refcnt[u^1]; refsum[p] = refsum[u]+refsum[u^1]; u = p; } if (!cnt[i]++) vset.insert(x); } /// perm tree void del_fish(int m, ll x) { int u = 1, b = 0, e = m; int i = get_id(m, x); while (b+1 < e) { int mid = (b+e)/2; u *= 2; if (i < mid) e = mid; else ++u, b = mid; } --refcnt[u]; refsum[u] -= x; while (u > 1) { int p = u/2; refcnt[p] = refcnt[u]+refcnt[u^1]; refsum[p] = refsum[u]+refsum[u^1]; u = p; } if (!--cnt[i]) vset.erase(x); } /// temp tree void restore(int u) { treecnt[u] = refcnt[u]; treesum[u] = refsum[u]; rflag[u] = true; } /// temp tree int eat(int u, int b, int e, int r, ll &need) { /// outside || nothing left retif(b >= r || !treesum[u]) 0; /// inside && sum <= need if (r >= e && need >= treesum[u]) { need -= treesum[u]; treesum[u] = 0; int res = treecnt[u]; treecnt[u] = 0; return res; } /// sz = 1 if (b+1 == e) { ll q = min((need-1)/v[b]+1, (ll)treecnt[u]); treecnt[u] -= q; ll d = q*v[b]; treesum[u] -= d; need -= d; return q; } int m = (b+e)/2, lc = 2*u, rc = 2*u+1; if (rflag[u]) { rflag[u] = false; restore(lc); restore(rc); } int res = eat(rc, m, e, r, need); if (need > 0) res += eat(lc, b, m, r, need); treecnt[u] = treecnt[lc]+treecnt[rc]; treesum[u] = treesum[lc]+treesum[rc]; return res; } void attack(int m, ll s, ll k) { int ans = 0; restore(1); while (s < k) { ll nxt = k; int r = get_id(m, s); /// index of first too big auto it = vset.lower_bound(s); if (it != vset.end()) nxt = min(nxt, (*it)+1); ll need = nxt-s; int c = eat(1, 0, m, r, need); ans += c; s = nxt-need; brif(need > 0); } if (s < k) cout << "-1\n"; else cout << ans << '\n'; } int main() { canhazfast(); int n, m, q; cin >> n; for (int i = 0; i < n; ++i) cin >> w[i]; copy_n(w, n, v); m = n; cin >> q; for (int i = 0; i < q; ++i) { int t; ll s, k; cin >> t >> s; if (t == 1) { cin >> k; queries[i] = {s, k}; } else if (t == 2) { queries[i] = {s, 0}; v[m++] = s; } else { queries[i] = {-s, 0}; } } sort(v, v+m); m = unique(v, v+m)-v; for (int i = 0; i < n; ++i) { int j = get_id(m, w[i]); if (!cnt[j]++) vset.insert(v[j]); } build(1, 0, m); for (int i = 0; i < q; ++i) { ll s, k; tie(s, k) = queries[i]; if (k) { attack(m, s, k); continue; } if (s > 0) add_fish(m, s); else del_fish(m, -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 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 | #include <bits/stdc++.h> using namespace std; typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef long double ldbl; typedef pair<int, int> pii; typedef pair<uint, uint> puu; typedef pair<ll, ll> pll; typedef pair<ull, ull> pull; typedef pair<double, double> pdd; typedef vector<int> vi; typedef vector<uint> vu; typedef vector<ll> vll; typedef vector<ull> vull; typedef vector<pii> vpii; typedef vector<puu> vpuu; typedef vector<pll> vpll; typedef vector<pull> vpull; typedef vector<string> vstr; typedef vector<double> vdbl; typedef vector<ldbl> vldbl; #define pb push_back #define ppb pop_back #define pfr push_front #define ppfr pop_front #define emp emplace #define empb emplace_back #define be begin #define rbe rbegin #define all(x) (x).be(), (x).end() #define rall(x) (x).rbe(), (x).rend() #define fir first #define sec second #define mkp make_pair #define brif(cond) if (cond) break #define ctif(cond) if (cond) continue #define retif(cond) if (cond) return void canhazfast() {ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);} template<typename T> T gcd(T a, T b) {return b ? gcd(b, a%b) : a;} template<typename T> T extgcd(T a, T b, T &x, T &y) { T x0 = 1, y0 = 0, x1 = 0, y1 = 1; while (b) { T q = a/b; a %= b; swap(a, b); x0 -= q*x1; swap(x0, x1); y0 -= q*y1; swap(y0, y1); } x = x0; y = y0; return a; } int ctz(uint x) {return __builtin_ctz(x);} int ctzll(ull x) {return __builtin_ctzll(x);} int clz(uint x) {return __builtin_clz(x);} int clzll(ull x) {return __builtin_clzll(x);} int popcnt(uint x) {return __builtin_popcount(x);} int popcntll(ull x) {return __builtin_popcountll(x);} int bsr(uint x) {return 31^clz(x);} int bsrll(ull x) {return 63^clzll(x);} #define N 300016 #define Q 100016 #define M (N+Q) /// 400032 #define SZ (1<<20) /// nextpow2(2*M-1) set<ll> vset; int cnt[M]; ll v[M]; ll w[N]; pll queries[Q]; bool rflag[SZ]; int treecnt[SZ], refcnt[SZ]; ll treesum[SZ], refsum[SZ]; int get_id(int m, ll x) { return lower_bound(v, v+m, x)-v; } /// perm tree void build(int u, int b, int e) { if (b+1 == e) { refcnt[u] = cnt[b]; refsum[u] = cnt[b]*v[b]; return; } int m = (b+e)/2, lc = 2*u, rc = 2*u+1; build(lc, b, m); build(rc, m, e); refcnt[u] = refcnt[lc]+refcnt[rc]; refsum[u] = refsum[lc]+refsum[rc]; } /// perm tree void add_fish(int m, ll x) { int u = 1, b = 0, e = m; int i = get_id(m, x); while (b+1 < e) { int mid = (b+e)/2; u *= 2; if (i < mid) e = mid; else ++u, b = mid; } ++refcnt[u]; refsum[u] += x; while (u > 1) { int p = u/2; refcnt[p] = refcnt[u]+refcnt[u^1]; refsum[p] = refsum[u]+refsum[u^1]; u = p; } if (!cnt[i]++) vset.insert(x); } /// perm tree void del_fish(int m, ll x) { int u = 1, b = 0, e = m; int i = get_id(m, x); while (b+1 < e) { int mid = (b+e)/2; u *= 2; if (i < mid) e = mid; else ++u, b = mid; } --refcnt[u]; refsum[u] -= x; while (u > 1) { int p = u/2; refcnt[p] = refcnt[u]+refcnt[u^1]; refsum[p] = refsum[u]+refsum[u^1]; u = p; } if (!--cnt[i]) vset.erase(x); } /// temp tree void restore(int u) { treecnt[u] = refcnt[u]; treesum[u] = refsum[u]; rflag[u] = true; } /// temp tree int eat(int u, int b, int e, int r, ll &need) { /// outside || nothing left retif(b >= r || !treesum[u]) 0; /// inside && sum <= need if (r >= e && need >= treesum[u]) { need -= treesum[u]; treesum[u] = 0; int res = treecnt[u]; treecnt[u] = 0; return res; } /// sz = 1 if (b+1 == e) { ll q = min((need-1)/v[b]+1, (ll)treecnt[u]); treecnt[u] -= q; ll d = q*v[b]; treesum[u] -= d; need -= d; return q; } int m = (b+e)/2, lc = 2*u, rc = 2*u+1; if (rflag[u]) { rflag[u] = false; restore(lc); restore(rc); } int res = eat(rc, m, e, r, need); if (need > 0) res += eat(lc, b, m, r, need); treecnt[u] = treecnt[lc]+treecnt[rc]; treesum[u] = treesum[lc]+treesum[rc]; return res; } void attack(int m, ll s, ll k) { int ans = 0; restore(1); while (s < k) { ll nxt = k; int r = get_id(m, s); /// index of first too big auto it = vset.lower_bound(s); if (it != vset.end()) nxt = min(nxt, (*it)+1); ll need = nxt-s; int c = eat(1, 0, m, r, need); ans += c; s = nxt-need; brif(need > 0); } if (s < k) cout << "-1\n"; else cout << ans << '\n'; } int main() { canhazfast(); int n, m, q; cin >> n; for (int i = 0; i < n; ++i) cin >> w[i]; copy_n(w, n, v); m = n; cin >> q; for (int i = 0; i < q; ++i) { int t; ll s, k; cin >> t >> s; if (t == 1) { cin >> k; queries[i] = {s, k}; } else if (t == 2) { queries[i] = {s, 0}; v[m++] = s; } else { queries[i] = {-s, 0}; } } sort(v, v+m); m = unique(v, v+m)-v; for (int i = 0; i < n; ++i) { int j = get_id(m, w[i]); if (!cnt[j]++) vset.insert(v[j]); } build(1, 0, m); for (int i = 0; i < q; ++i) { ll s, k; tie(s, k) = queries[i]; if (k) { attack(m, s, k); continue; } if (s > 0) add_fish(m, s); else del_fish(m, -s); } return 0; } |