Skip to content

Commit

Permalink
add slightly faster RMQ
Browse files Browse the repository at this point in the history
  • Loading branch information
bqi343 committed Feb 20, 2023
1 parent 779cda3 commit b61cf39
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
@@ -0,0 +1,24 @@
/**
* Description: Faster 1D range minimum query.
* Source: KACTL
* Verification:
* https://judge.yosupo.jp/submission/126814
* Memory: O(N\log N)
* Time: O(1)
*/

tcT, size_t SZ> struct RMQ { // floor(log_2(x))
static constexpr int level(int x) { return 31-__builtin_clz(x); }
array<array<T,SZ>, level(SZ)+1> jmp;
T cmb(T a, T b) { return min(a, b); }
void init(const V<T>& v) { assert(sz(v) <= SZ);
copy(all(v), begin(jmp[0]));
for (int j = 1; 1<<j <= sz(v); ++j) {
F0R(i,sz(v)-(1<<j)+1) jmp[j][i] = cmb(jmp[j-1][i],
jmp[j-1][i+(1<<(j-1))]);
}
}
T query(int l, int r) {
assert(l <= r); int d = level(r-l+1);
return cmb(jmp[d][l],jmp[d][r-(1<<d)+1]); }
};
Expand Up @@ -10,7 +10,7 @@
* Time: O(1)
*/

tcT> struct RMQ {
tcT> struct RMQ { // floor(log_2(x))
int level(int x) { return 31-__builtin_clz(x); }
V<T> v; V<vi> jmp;
int cmb(int a, int b) {
Expand Down

0 comments on commit b61cf39

Please sign in to comment.