Skip to content

Commit 5382014

Browse files
committed
Update compression in ordered set test
1 parent 3a14350 commit 5382014

File tree

1 file changed

+28
-19
lines changed

1 file changed

+28
-19
lines changed

verify/structures/fenwick/ordered_set.test.cpp

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,55 @@
77
using namespace std;
88
using cp_algo::structures::fenwick_set;
99

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+
1025
void solve() {
1126
int n, q;
1227
cin >> n >> q;
1328
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+
}
1634
vector queries(q, pair{0, 0});
1735
for(auto &[t, x]: queries) {
1836
cin >> t >> x;
19-
if(t == 0) {
20-
nums.push_back(x);
37+
if(t != 2) {
38+
coords.push_back(&x);
2139
}
2240
}
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);
3042
const int maxc = 1e6;
3143
fenwick_set<maxc> me(a);
3244
for(auto [t, x]: queries) {
3345
if(t == 0) {
34-
me.insert(hashify(x));
46+
me.insert(x);
3547
} else if(t == 1) {
36-
int y = hashify(x);
37-
if(nums[y] == x) {
38-
me.erase(y);
39-
}
48+
me.erase(x);
4049
} else if(t == 2) {
4150
int res = me.find_by_order(x-1);
4251
cout << (res == -1 ? -1 : nums[res]) << '\n';
4352
} else if(t == 3) {
44-
cout << me.order_of_key(hashify(x+1)) << '\n';
53+
cout << me.order_of_key(x+1) << '\n';
4554
} else if(t == 4) {
46-
int res = me.pre_upper_bound(hashify(x+1)-1);
55+
int res = me.pre_upper_bound(x);
4756
cout << (res == -1 ? -1 : nums[res]) << '\n';
4857
} else if(t == 5) {
49-
int res = me.lower_bound(hashify(x));
58+
int res = me.lower_bound(x);
5059
cout << (res == -1 ? -1 : nums[res]) << '\n';
5160
}
5261
}

0 commit comments

Comments
 (0)