#include <bits/stdc++.h> using namespace std; #define For(i, n) for (int i = 0; i < int(n); i++) #define ForD(i, n) for (int i = int(n) - 1; i >= 0; i--) #define SORT(x) sort(begin(x), end(x)) #define REP(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) template<typename T, typename U> pair<T, U>& operator+=(pair<T, U> &lhs, const pair<T, U> &rhs){ lhs.first += rhs.first; lhs.second += rhs.second; return lhs; } template<typename T, typename U> pair<T, U>& operator-=(pair<T, U> &lhs, const pair<T, U> &rhs){ lhs.first -= rhs.first; lhs.second -= rhs.second; return lhs; } template <class T> ostream &operator<<(ostream &os, const vector<T> &container) { for (auto &u : container) os << u << " "; return os; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update // #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; using namespace std; template<typename T> using pb_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #if DEBUG #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string>) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #else #define error(...) do {} while (0) #endif template<typename T, typename U> pair<T, U> operator-(const pair<T, U> &l, const pair<T, U> &r) { return {l.first - r.first, l.second - r.second}; } template<typename T, typename U> pair<T, U> operator+(const pair<T, U> &l, const pair<T, U> &r) { return {l.first + r.first, l.second + r.second}; } #define _upgrade do { ios::sync_with_stdio(0); cin.tie(0); } while (0) typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int N = 5 * 100 * 1000; struct seg_tree { ll t[2 * N]; seg_tree() {} void build() { for (int i = N - 1; i > 0; --i) t[i] = t[i << 1] + t[(i << 1) | 1]; } void add(int p, ll value) { for (t[p += N] += value; p > 1; p >>= 1) t[p >> 1] = t[p] + t[p ^ 1]; } ll query(int l, int r) { ll res = 0; for (l += N, r += N; l < r; l >>= 1, r >>= 1) { if (l & 1) res += t[l++]; if (r & 1) res += t[--r]; } return res; } }; seg_tree sum, cnt; unordered_map<ll, int> num_pos; map<ll, int> nums_present; vector<pii> eaten_intervals; int largest_pos(int p, int q, ll s) { if (p == q) return p; int beg = p; int end = q + 1; while (beg != end - 1) { int mid = (beg + end) >> 1; if (mid < q + 1 && sum.query(mid, q + 1) > s) { beg = mid; } else { end = mid; } } return beg; } void print_trees(int n) { cout << "sum: "; For (i, 2 * n) cout << sum.query(i + 1, i + 2) << " "; cout << endl; cout << "cnt: "; For (i, 2 * n) cout << cnt.query(i + 1, i + 2) << " "; cout << endl; cout << "num present\n"; for (auto [x, c] : nums_present) cout << x << " | " << num_pos[x] << " -> " << c << endl; cout << endl; } bool go(ll s, ll k) { if (s >= k) return true; auto eat_it = nums_present.lower_bound(s); if (eat_it == nums_present.begin()) return false; ll target = k - 1; if (eat_it != nums_present.end()) target = min(target, eat_it->first); eat_it--; auto [x, c] = *eat_it; int pos = num_pos[x] + c - 1; // cout << "s: " << s << endl; if (pos > eaten_intervals.back().second) { s += x; // cout << "adding at pos: " << pos << ", x: " << x << endl; if (eaten_intervals.back().second + 1 == pos) { eaten_intervals.back().second = pos; } else eaten_intervals.push_back({pos, pos}); return go(s, k); } else { while (eaten_intervals.size() >= 2) { auto [p, last_q] = eaten_intervals.back(); auto [_, q] = *(++eaten_intervals.rbegin()); ll extra = sum.query(q + 1, p); if (extra + s <= target) { eaten_intervals.pop_back(); eaten_intervals.back().second = last_q; s += extra; continue; } else { // cout << "ans: "; // for (auto [pp, qq] : eaten_intervals) { // cout << "[" << pp << ", " << qq << "] "; // } // cout << endl; int p_pos = largest_pos(q + 1, p - 1, target - s); if (p_pos == q + 1) { eaten_intervals.pop_back(); eaten_intervals.back().second = last_q; } else { eaten_intervals.back().first = p_pos; } // print_trees(20); // cout << "s: " << s << ", p_pos: " << p_pos << ", p: " << p << ", target: " << target << endl; // cout << "query: " << sum.query(p_pos, p) << endl; s += sum.query(p_pos, p); // cout << "s: " << s << endl; return go(s, k); } } } return false; } int solve(ll s, ll k) { if (s >= k) return 0; if (nums_present.empty()) return -1; auto eat_it = nums_present.lower_bound(s); if (eat_it == nums_present.begin()) return -1; eaten_intervals.clear(); eat_it--; auto [x, c] = *eat_it; s += x; int pos = num_pos[x] + c - 1; eaten_intervals.push_back({0, 0}); eaten_intervals.push_back({pos, pos}); if (!go(s, k)) return -1; int res = 0; // cout << "ans: "; for (auto [p, q] : eaten_intervals) { res += (int)cnt.query(p, q + 1); // cout << "[" << p << ", " << q << "] "; } return res; } int main() { _upgrade; int n; cin >> n; vector<ll> init_nums, all_nums; init_nums.reserve(n); all_nums.reserve(n); For (i, n) { ll x; cin >> x; init_nums.push_back(x); all_nums.push_back(x); } int q; cin >> q; vector<pair<int, pair<ll, ll>>> qs; For (i, q) { int t; ll x, y = 0; cin >> t >> x; if (t == 1) cin >> y; if (t == 2) all_nums.push_back(x); qs.push_back({t, {x, y}}); } SORT(all_nums); For (i, all_nums.size()) { ll x = all_nums[i]; if (i == 0 || all_nums[i] != all_nums[i - 1]) num_pos[x] = i + 1; } for (ll x : init_nums) { int c = nums_present[x]++; sum.t[N + num_pos[x] + c] += x; cnt.t[N + num_pos[x] + c] += 1; } sum.build(); cnt.build(); // print_trees(n * 2); for (auto [t, data] : qs) { auto [x, y] = data; if (t == 2) { int c = nums_present[x]++; int pos = num_pos[x] + c; sum.add(pos, x); cnt.add(pos, 1); } else if (t == 3) { int c = --nums_present[x]; int pos = num_pos[x] + c; sum.add(pos, -x); cnt.add(pos, -1); if (c == 0) nums_present.erase(x); } else { // print_trees(n * 2 + 5); cout << solve(x, y) << "\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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | #include <bits/stdc++.h> using namespace std; #define For(i, n) for (int i = 0; i < int(n); i++) #define ForD(i, n) for (int i = int(n) - 1; i >= 0; i--) #define SORT(x) sort(begin(x), end(x)) #define REP(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end))) template<typename T, typename U> pair<T, U>& operator+=(pair<T, U> &lhs, const pair<T, U> &rhs){ lhs.first += rhs.first; lhs.second += rhs.second; return lhs; } template<typename T, typename U> pair<T, U>& operator-=(pair<T, U> &lhs, const pair<T, U> &rhs){ lhs.first -= rhs.first; lhs.second -= rhs.second; return lhs; } template <class T> ostream &operator<<(ostream &os, const vector<T> &container) { for (auto &u : container) os << u << " "; return os; } template <class T, class U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << p.first << " " << p.second; return os; } #include <ext/pb_ds/assoc_container.hpp> // Common file #include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update // #include <ext/pb_ds/detail/standard_policies.hpp> using namespace __gnu_pbds; using namespace std; template<typename T> using pb_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; #if DEBUG #define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); } void err(istream_iterator<string>) {} template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << *it << " = " << a << endl; err(++it, args...); } #else #define error(...) do {} while (0) #endif template<typename T, typename U> pair<T, U> operator-(const pair<T, U> &l, const pair<T, U> &r) { return {l.first - r.first, l.second - r.second}; } template<typename T, typename U> pair<T, U> operator+(const pair<T, U> &l, const pair<T, U> &r) { return {l.first + r.first, l.second + r.second}; } #define _upgrade do { ios::sync_with_stdio(0); cin.tie(0); } while (0) typedef long long ll; typedef long double ld; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int N = 5 * 100 * 1000; struct seg_tree { ll t[2 * N]; seg_tree() {} void build() { for (int i = N - 1; i > 0; --i) t[i] = t[i << 1] + t[(i << 1) | 1]; } void add(int p, ll value) { for (t[p += N] += value; p > 1; p >>= 1) t[p >> 1] = t[p] + t[p ^ 1]; } ll query(int l, int r) { ll res = 0; for (l += N, r += N; l < r; l >>= 1, r >>= 1) { if (l & 1) res += t[l++]; if (r & 1) res += t[--r]; } return res; } }; seg_tree sum, cnt; unordered_map<ll, int> num_pos; map<ll, int> nums_present; vector<pii> eaten_intervals; int largest_pos(int p, int q, ll s) { if (p == q) return p; int beg = p; int end = q + 1; while (beg != end - 1) { int mid = (beg + end) >> 1; if (mid < q + 1 && sum.query(mid, q + 1) > s) { beg = mid; } else { end = mid; } } return beg; } void print_trees(int n) { cout << "sum: "; For (i, 2 * n) cout << sum.query(i + 1, i + 2) << " "; cout << endl; cout << "cnt: "; For (i, 2 * n) cout << cnt.query(i + 1, i + 2) << " "; cout << endl; cout << "num present\n"; for (auto [x, c] : nums_present) cout << x << " | " << num_pos[x] << " -> " << c << endl; cout << endl; } bool go(ll s, ll k) { if (s >= k) return true; auto eat_it = nums_present.lower_bound(s); if (eat_it == nums_present.begin()) return false; ll target = k - 1; if (eat_it != nums_present.end()) target = min(target, eat_it->first); eat_it--; auto [x, c] = *eat_it; int pos = num_pos[x] + c - 1; // cout << "s: " << s << endl; if (pos > eaten_intervals.back().second) { s += x; // cout << "adding at pos: " << pos << ", x: " << x << endl; if (eaten_intervals.back().second + 1 == pos) { eaten_intervals.back().second = pos; } else eaten_intervals.push_back({pos, pos}); return go(s, k); } else { while (eaten_intervals.size() >= 2) { auto [p, last_q] = eaten_intervals.back(); auto [_, q] = *(++eaten_intervals.rbegin()); ll extra = sum.query(q + 1, p); if (extra + s <= target) { eaten_intervals.pop_back(); eaten_intervals.back().second = last_q; s += extra; continue; } else { // cout << "ans: "; // for (auto [pp, qq] : eaten_intervals) { // cout << "[" << pp << ", " << qq << "] "; // } // cout << endl; int p_pos = largest_pos(q + 1, p - 1, target - s); if (p_pos == q + 1) { eaten_intervals.pop_back(); eaten_intervals.back().second = last_q; } else { eaten_intervals.back().first = p_pos; } // print_trees(20); // cout << "s: " << s << ", p_pos: " << p_pos << ", p: " << p << ", target: " << target << endl; // cout << "query: " << sum.query(p_pos, p) << endl; s += sum.query(p_pos, p); // cout << "s: " << s << endl; return go(s, k); } } } return false; } int solve(ll s, ll k) { if (s >= k) return 0; if (nums_present.empty()) return -1; auto eat_it = nums_present.lower_bound(s); if (eat_it == nums_present.begin()) return -1; eaten_intervals.clear(); eat_it--; auto [x, c] = *eat_it; s += x; int pos = num_pos[x] + c - 1; eaten_intervals.push_back({0, 0}); eaten_intervals.push_back({pos, pos}); if (!go(s, k)) return -1; int res = 0; // cout << "ans: "; for (auto [p, q] : eaten_intervals) { res += (int)cnt.query(p, q + 1); // cout << "[" << p << ", " << q << "] "; } return res; } int main() { _upgrade; int n; cin >> n; vector<ll> init_nums, all_nums; init_nums.reserve(n); all_nums.reserve(n); For (i, n) { ll x; cin >> x; init_nums.push_back(x); all_nums.push_back(x); } int q; cin >> q; vector<pair<int, pair<ll, ll>>> qs; For (i, q) { int t; ll x, y = 0; cin >> t >> x; if (t == 1) cin >> y; if (t == 2) all_nums.push_back(x); qs.push_back({t, {x, y}}); } SORT(all_nums); For (i, all_nums.size()) { ll x = all_nums[i]; if (i == 0 || all_nums[i] != all_nums[i - 1]) num_pos[x] = i + 1; } for (ll x : init_nums) { int c = nums_present[x]++; sum.t[N + num_pos[x] + c] += x; cnt.t[N + num_pos[x] + c] += 1; } sum.build(); cnt.build(); // print_trees(n * 2); for (auto [t, data] : qs) { auto [x, y] = data; if (t == 2) { int c = nums_present[x]++; int pos = num_pos[x] + c; sum.add(pos, x); cnt.add(pos, 1); } else if (t == 3) { int c = --nums_present[x]; int pos = num_pos[x] + c; sum.add(pos, -x); cnt.add(pos, -1); if (c == 0) nums_present.erase(x); } else { // print_trees(n * 2 + 5); cout << solve(x, y) << "\n"; } } } |