Skip to content

Commit 94525b6

Browse files
committed
Add compress coords util
1 parent e184e8c commit 94525b6

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

cp-algo/util/compress_coords.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef CP_ALGO_UTIL_COMPRESS_COORDS_HPP
2+
#define CP_ALGO_UTIL_COMPRESS_COORDS_HPP
3+
#include <algorithm>
4+
#include <vector>
5+
namespace cp_algo {
6+
std::vector<int> compress_coords(auto &coords) {
7+
static_assert(std::is_pointer_v<std::ranges::range_value_t<decltype(coords)>>);
8+
std::vector<int> original;
9+
original.reserve(size(coords));
10+
std::ranges::sort(coords, {}, [](int* x) {return *x;});
11+
int idx = -1, prev = -1;
12+
for(auto x: coords) {
13+
if(*x != prev) {
14+
idx++;
15+
prev = *x;
16+
original.push_back(*x);
17+
}
18+
*x = idx;
19+
}
20+
return original;
21+
}
22+
}
23+
#endif // CP_ALGO_UTIL_COMPRESS_COORDS_HPP

verify/structures/fenwick/ordered_set.test.cpp

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,12 @@
22
#define PROBLEM "https://judge.yosupo.jp/problem/ordered_set"
33
#pragma GCC optimize("Ofast,unroll-loops")
44
#include "cp-algo/structures/fenwick_set.hpp"
5+
#include "cp-algo/util/compress_coords.hpp"
56
#include <bits/stdc++.h>
67

78
using namespace std;
89
using cp_algo::structures::fenwick_set;
910

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-
2511
void solve() {
2612
int n, q;
2713
cin >> n >> q;
@@ -38,7 +24,7 @@ void solve() {
3824
coords.push_back(&x);
3925
}
4026
}
41-
auto nums = compress(coords);
27+
auto values = cp_algo::compress_coords(coords);
4228
const int maxc = 1e6;
4329
fenwick_set<maxc> me(a);
4430
for(auto [t, x]: queries) {
@@ -48,15 +34,15 @@ void solve() {
4834
me.erase(x);
4935
} else if(t == 2) {
5036
int res = me.find_by_order(x-1);
51-
cout << (res == -1 ? -1 : nums[res]) << '\n';
37+
cout << (res == -1 ? -1 : values[res]) << '\n';
5238
} else if(t == 3) {
5339
cout << me.order_of_key(x+1) << '\n';
5440
} else if(t == 4) {
5541
int res = me.pre_upper_bound(x);
56-
cout << (res == -1 ? -1 : nums[res]) << '\n';
42+
cout << (res == -1 ? -1 : values[res]) << '\n';
5743
} else if(t == 5) {
5844
int res = me.lower_bound(x);
59-
cout << (res == -1 ? -1 : nums[res]) << '\n';
45+
cout << (res == -1 ? -1 : values[res]) << '\n';
6046
}
6147
}
6248
}

0 commit comments

Comments
 (0)