templates/coordinate-compression.cpp
compilable
$cat templates/coordinate-compression
Coordinate Compression
Maps sparse values to dense 0-indexed ranks for use with BIT/segment tree.
#coordinate-compression#discretization#rank
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;
vector<int> coords;
void compressInit() {
sort(coords.begin(), coords.end());
coords.erase(unique(coords.begin(), coords.end()), coords.end());
}
int getRank(int x) {
return lower_bound(coords.begin(), coords.end(), x) - coords.begin();
}
int getValue(int idx) { return coords[idx]; }
int compressSize() { return coords.size(); }17 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)
Coordinate Compression
Transforms a sparse set of values into contiguous ranks .