summaryrefslogtreecommitdiff
path: root/content/datastructures/sparseTable.cpp
diff options
context:
space:
mode:
authorGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 01:24:14 +0100
committerGloria Mundi <gloria@gloria-mundi.eu>2024-11-16 01:24:14 +0100
commit98567ec798aa8ca2cfbcb85c774dd470f30e30d4 (patch)
tree5113d5cc24d1ad5f93810b6442ce584a36950dc8 /content/datastructures/sparseTable.cpp
parentad3856a6b766087df0036de0b556f4700a6498c9 (diff)
parent8d11c6c8213f46f0fa19826917c255edd5d43cb1 (diff)
mzuenni tests
Diffstat (limited to 'content/datastructures/sparseTable.cpp')
-rw-r--r--content/datastructures/sparseTable.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/content/datastructures/sparseTable.cpp b/content/datastructures/sparseTable.cpp
new file mode 100644
index 0000000..5e84236
--- /dev/null
+++ b/content/datastructures/sparseTable.cpp
@@ -0,0 +1,24 @@
+struct SparseTable {
+ vector<vector<int>> st;
+ ll *a;
+
+ int better(int lidx, int ridx) {
+ return a[lidx] <= a[ridx] ? lidx : ridx;
+ }
+
+ void init(vector<ll> &vec) {
+ int n = sz(vec);
+ a = vec.data();
+ st.assign(__lg(n) + 1, vector<int>(n));
+ iota(all(st[0]), 0);
+ for (int j = 0; (2 << j) <= n; j++) {
+ for (int i = 0; i + (2 << j) <= n; i++) {
+ st[j + 1][i] = better(st[j][i] , st[j][i + (1 << j)]);
+ }}}
+
+ int queryIdempotent(int l, int r) {
+ if (r <= l) return -1;
+ int j = __lg(r - l); //31 - builtin_clz(r - l);
+ return better(st[j][l] , st[j][r - (1 << j)]);
+ }
+};