|
7 | 7 | using namespace std;
|
8 | 8 | using cp_algo::structures::fenwick_set;
|
9 | 9 |
|
| 10 | +vector<int> compress(vector<int*> a) { |
| 11 | + vector<int> nums; |
| 12 | + ranges::sort(a, {}, [](int* x) {return *x;}); |
| 13 | + int idx = -1, prev = -1; |
| 14 | + for(auto x: a) { |
| 15 | + if(*x != prev) { |
| 16 | + idx++; |
| 17 | + prev = *x; |
| 18 | + nums.push_back(*x); |
| 19 | + } |
| 20 | + *x = idx; |
| 21 | + } |
| 22 | + return nums; |
| 23 | +} |
| 24 | + |
10 | 25 | void solve() {
|
11 | 26 | int n, q;
|
12 | 27 | cin >> n >> q;
|
13 | 28 | vector a(n, 0);
|
14 |
| - for(auto &it: a) {cin >> it;} |
15 |
| - auto nums = a; |
| 29 | + vector<int*> coords; |
| 30 | + for(auto &it: a) { |
| 31 | + cin >> it; |
| 32 | + coords.push_back(&it); |
| 33 | + } |
16 | 34 | vector queries(q, pair{0, 0});
|
17 | 35 | for(auto &[t, x]: queries) {
|
18 | 36 | cin >> t >> x;
|
19 |
| - if(t == 0) { |
20 |
| - nums.push_back(x); |
| 37 | + if(t != 2) { |
| 38 | + coords.push_back(&x); |
21 | 39 | }
|
22 | 40 | }
|
23 |
| - nums.push_back(0); |
24 |
| - ranges::sort(nums); |
25 |
| - nums.erase(ranges::unique(nums).begin(), end(nums)); |
26 |
| - auto hashify = [&](int x) { |
27 |
| - return ranges::lower_bound(nums, x) - begin(nums); |
28 |
| - }; |
29 |
| - ranges::transform(a, begin(a), hashify); |
| 41 | + auto nums = compress(coords); |
30 | 42 | const int maxc = 1e6;
|
31 | 43 | fenwick_set<maxc> me(a);
|
32 | 44 | for(auto [t, x]: queries) {
|
33 | 45 | if(t == 0) {
|
34 |
| - me.insert(hashify(x)); |
| 46 | + me.insert(x); |
35 | 47 | } else if(t == 1) {
|
36 |
| - int y = hashify(x); |
37 |
| - if(nums[y] == x) { |
38 |
| - me.erase(y); |
39 |
| - } |
| 48 | + me.erase(x); |
40 | 49 | } else if(t == 2) {
|
41 | 50 | int res = me.find_by_order(x-1);
|
42 | 51 | cout << (res == -1 ? -1 : nums[res]) << '\n';
|
43 | 52 | } else if(t == 3) {
|
44 |
| - cout << me.order_of_key(hashify(x+1)) << '\n'; |
| 53 | + cout << me.order_of_key(x+1) << '\n'; |
45 | 54 | } else if(t == 4) {
|
46 |
| - int res = me.pre_upper_bound(hashify(x+1)-1); |
| 55 | + int res = me.pre_upper_bound(x); |
47 | 56 | cout << (res == -1 ? -1 : nums[res]) << '\n';
|
48 | 57 | } else if(t == 5) {
|
49 |
| - int res = me.lower_bound(hashify(x)); |
| 58 | + int res = me.lower_bound(x); |
50 | 59 | cout << (res == -1 ? -1 : nums[res]) << '\n';
|
51 | 60 | }
|
52 | 61 | }
|
|
0 commit comments