#include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define PB push_back #define SIZE(a) (int(a.size())) struct Tree { static const int x2 = (1<<20); static const int x1 = (1<<19); ll t[x2]; Tree() { for(int i=0; i < x2; i++) { t[i] = 0; } } void update(int a, ll val) { a+=x1; t[a] = val; a/=2; while(a) { t[a] = t[2*a] + t[2*a+1]; a/=2; } } ll get(int le, int ri) { le+=x1; ri+=x1; ll res=0; if(le < ri) { res = t[le]+t[ri]; } else { res = t[le]; } while(le/2 != ri/2) { if(le%2 == 0) { res+=t[le+1]; } if(ri%2 == 1) { res+=t[ri-1]; } le/=2; ri/=2; } return res; } ll get_point(int x) { return get(x, x); } }; Tree t, c; ll compute_on_interval(int le, int ri, vector<pair<pii, ll>> &groups, Tree &tr) { for(int i=SIZE(groups)-1; i >= 0; i--) { if(le <= groups[i].ST.ND) { le = min(le, (int)groups[i].ST.ST); } else { break; } } ll r = tr.get(le, ri); for(int i=SIZE(groups)-1; i >= 0; i--) { if(le <= groups[i].ST.ND) { r-=groups[i].ND; } else { break; } } return r; } void add(map<ll, ll> &ms, ll w) { auto it = ms.find(w); if(it == ms.end()) { ms[w] = 1; } else { ms[w]++; } } void rem(map<ll, ll> &ms, ll w) { if(ms[w] == 1) { ms.erase(w); } else { ms[w]--; } } void show(vector<pair<pii, ll>> &groups) { // cerr << "G: "; // for(auto g : groups) { // cerr << g.ST.ST << "," << g.ST.ND << ";" << g.ND << " "; // } // cerr << "\n"; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<ll> weights; vector<pll> queries; weights.PB(0); for(int i=0; i < n; i++) { ll w; cin >> w; weights.PB(w); queries.PB({w, 0}); } int nq; cin >> nq; for(int i=0; i < nq; i++) { int t; cin >> t; if(t == 1) { ll a, b; cin >> a >> b; queries.PB({a, b}); } else if(t == 2) { ll w; cin >> w; weights.PB(w); queries.PB({w, 0}); } else { ll w; cin >> w; queries.PB({w, -1}); } } sort(weights.begin(), weights.end()); map<ll, ll> ms; for(pll q : queries) { if(q.ND == 0) { //add int le=0, ri = SIZE(weights)-1; while(le < ri) { int mid = (le+ri)/2; if(weights[mid] < q.ST || (weights[mid] == q.ST && t.get_point(mid) == q.ST)) { le = mid+1; } else { ri = mid; } } // cerr << "le: " << le << "\n"; t.update(le, q.ST); c.update(le, 1); add(ms, q.ST); } else if(q.ND < 0) { //remove int le=0, ri=SIZE(weights)-1; while(le < ri) { int mid = (le+ri+1)/2; if(weights[mid] > q.ST || (weights[mid] == q.ST && t.get_point(mid) == 0)) { ri = mid-1; } else { le = mid; } } // cerr << "del: " << le << "\n"; t.update(le, 0); c.update(le, 0); rem(ms, q.ST); } else { //answer ll current = q.ST; // cerr << "cur: " << current << "\n"; ll total=0; vector<pair<pii, ll>> groups; vector<pair<pii, ll>> groups_sizes; while(current < q.ND) { // cerr << current << " "; ll ne = q.ND-1; auto it = ms.upper_bound(current-1); if(it != ms.end()) { ne = min(ne, it->ST); } // cerr << "ne: " << ne << "\n"; int le=0, ri=SIZE(weights)-1; while(le < ri) { int mid = (le+ri+1)/2; if(weights[mid] >= current) { ri = mid-1; } else { le = mid; } } int p = le; show(groups); // cerr << "p: " << p << " maxv: " << compute_on_interval(0, p, groups, t)+current << "\n"; if(compute_on_interval(0, p, groups, t)+current <= ne) { break; } // int d = 1; // while(d < p) { // if(compute_on_interval(p-d, p, groups, t) > ne) { // break; // } // d = 2*d; // } // le = max(0, p-d), ri = p; le=0, ri=p; while(le < ri) { int mid = (le+ri+1)/2; ll val = compute_on_interval(mid, p, groups, t); if(val+current <= ne) { ri = mid-1; } else { le = mid; } } pll r; r.ST = compute_on_interval(le, p, groups, t); r.ND = compute_on_interval(le, p, groups_sizes, c); current+=r.ST; total+=r.ND; groups.PB({{le, p}, r.ST}); groups_sizes.PB({{le, p}, r.ND}); } if(current < q.ND) { cout << "-1\n"; } else { cout << total << "\n"; } } } }
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 | #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ST first #define ND second #define PB push_back #define SIZE(a) (int(a.size())) struct Tree { static const int x2 = (1<<20); static const int x1 = (1<<19); ll t[x2]; Tree() { for(int i=0; i < x2; i++) { t[i] = 0; } } void update(int a, ll val) { a+=x1; t[a] = val; a/=2; while(a) { t[a] = t[2*a] + t[2*a+1]; a/=2; } } ll get(int le, int ri) { le+=x1; ri+=x1; ll res=0; if(le < ri) { res = t[le]+t[ri]; } else { res = t[le]; } while(le/2 != ri/2) { if(le%2 == 0) { res+=t[le+1]; } if(ri%2 == 1) { res+=t[ri-1]; } le/=2; ri/=2; } return res; } ll get_point(int x) { return get(x, x); } }; Tree t, c; ll compute_on_interval(int le, int ri, vector<pair<pii, ll>> &groups, Tree &tr) { for(int i=SIZE(groups)-1; i >= 0; i--) { if(le <= groups[i].ST.ND) { le = min(le, (int)groups[i].ST.ST); } else { break; } } ll r = tr.get(le, ri); for(int i=SIZE(groups)-1; i >= 0; i--) { if(le <= groups[i].ST.ND) { r-=groups[i].ND; } else { break; } } return r; } void add(map<ll, ll> &ms, ll w) { auto it = ms.find(w); if(it == ms.end()) { ms[w] = 1; } else { ms[w]++; } } void rem(map<ll, ll> &ms, ll w) { if(ms[w] == 1) { ms.erase(w); } else { ms[w]--; } } void show(vector<pair<pii, ll>> &groups) { // cerr << "G: "; // for(auto g : groups) { // cerr << g.ST.ST << "," << g.ST.ND << ";" << g.ND << " "; // } // cerr << "\n"; } int main() { ios_base::sync_with_stdio(0); int n; cin >> n; vector<ll> weights; vector<pll> queries; weights.PB(0); for(int i=0; i < n; i++) { ll w; cin >> w; weights.PB(w); queries.PB({w, 0}); } int nq; cin >> nq; for(int i=0; i < nq; i++) { int t; cin >> t; if(t == 1) { ll a, b; cin >> a >> b; queries.PB({a, b}); } else if(t == 2) { ll w; cin >> w; weights.PB(w); queries.PB({w, 0}); } else { ll w; cin >> w; queries.PB({w, -1}); } } sort(weights.begin(), weights.end()); map<ll, ll> ms; for(pll q : queries) { if(q.ND == 0) { //add int le=0, ri = SIZE(weights)-1; while(le < ri) { int mid = (le+ri)/2; if(weights[mid] < q.ST || (weights[mid] == q.ST && t.get_point(mid) == q.ST)) { le = mid+1; } else { ri = mid; } } // cerr << "le: " << le << "\n"; t.update(le, q.ST); c.update(le, 1); add(ms, q.ST); } else if(q.ND < 0) { //remove int le=0, ri=SIZE(weights)-1; while(le < ri) { int mid = (le+ri+1)/2; if(weights[mid] > q.ST || (weights[mid] == q.ST && t.get_point(mid) == 0)) { ri = mid-1; } else { le = mid; } } // cerr << "del: " << le << "\n"; t.update(le, 0); c.update(le, 0); rem(ms, q.ST); } else { //answer ll current = q.ST; // cerr << "cur: " << current << "\n"; ll total=0; vector<pair<pii, ll>> groups; vector<pair<pii, ll>> groups_sizes; while(current < q.ND) { // cerr << current << " "; ll ne = q.ND-1; auto it = ms.upper_bound(current-1); if(it != ms.end()) { ne = min(ne, it->ST); } // cerr << "ne: " << ne << "\n"; int le=0, ri=SIZE(weights)-1; while(le < ri) { int mid = (le+ri+1)/2; if(weights[mid] >= current) { ri = mid-1; } else { le = mid; } } int p = le; show(groups); // cerr << "p: " << p << " maxv: " << compute_on_interval(0, p, groups, t)+current << "\n"; if(compute_on_interval(0, p, groups, t)+current <= ne) { break; } // int d = 1; // while(d < p) { // if(compute_on_interval(p-d, p, groups, t) > ne) { // break; // } // d = 2*d; // } // le = max(0, p-d), ri = p; le=0, ri=p; while(le < ri) { int mid = (le+ri+1)/2; ll val = compute_on_interval(mid, p, groups, t); if(val+current <= ne) { ri = mid-1; } else { le = mid; } } pll r; r.ST = compute_on_interval(le, p, groups, t); r.ND = compute_on_interval(le, p, groups_sizes, c); current+=r.ST; total+=r.ND; groups.PB({{le, p}, r.ST}); groups_sizes.PB({{le, p}, r.ND}); } if(current < q.ND) { cout << "-1\n"; } else { cout << total << "\n"; } } } } |