Skip to content

Commit 17d68ef

Browse files
committed
coodinate compression
1 parent c50ad83 commit 17d68ef

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cc.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Coodinate Compression
2+
// https://youtu.be/fR3W5IcBGLQ?t=8550
3+
template<typename T=int>
4+
struct CC {
5+
bool initialized;
6+
vector<T> xs;
7+
CC(): initialized(false) {}
8+
void add(T x) { xs.push_back(x);}
9+
void init() {
10+
sort(xs.begin(), xs.end());
11+
xs.erase(unique(xs.begin(),xs.end()),xs.end());
12+
initialized = true;
13+
}
14+
int operator()(T x) {
15+
if (!initialized) init();
16+
return upper_bound(xs.begin(), xs.end(), x) - xs.begin() - 1;
17+
}
18+
T operator[](int i) {
19+
if (!initialized) init();
20+
return xs[i];
21+
}
22+
int size() {
23+
if (!initialized) init();
24+
return xs.size();
25+
}
26+
};

0 commit comments

Comments
 (0)