-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
#pragma once | ||
#include <vector> | ||
#include <cassert> | ||
namespace po167{ | ||
|
||
template<class T, T(*op)(T, T)> | ||
struct Doubling_op{ | ||
int n; | ||
int depth; | ||
std::vector<std::vector<int>> index; | ||
std::vector<std::vector<T>> val; | ||
Doubling_op(std::vector<int> p, std::vector<T> v, long long lim = (1ll << 60) - 1){ | ||
n = p.size(); | ||
for (auto x : p) assert(0 <= x && x < n); | ||
assert(n == (int)v.size()); | ||
depth = 1; | ||
while ((1ll << depth) <= lim) depth++; | ||
index.resize(depth); | ||
val.resize(depth); | ||
index[0] = p; | ||
val[0] = v; | ||
for (int i = 1; i < depth; i++){ | ||
index[i].resize(n); | ||
val[i].resize(n); | ||
for (int j = 0; j < n; j++){ | ||
int tmp = index[i - 1][j]; | ||
index[i][j] = index[i - 1][tmp]; | ||
val[i][j] = op(val[i - 1][j], val[i - 1][tmp]); | ||
} | ||
} | ||
} | ||
std::pair<int, T> query(int start_ind, T start_val, long long times){ | ||
assert(0 <= start_ind && start_ind < n); | ||
assert(0 <= times && times < (1ll << depth)); | ||
int i = 0; | ||
while (times){ | ||
if (times & 1){ | ||
start_val = op(start_val, val[i][start_ind]); | ||
start_ind = index[i][start_ind]; | ||
} | ||
i++; | ||
times >>= 1; | ||
} | ||
return std::make_pair(start_ind, start_val); | ||
} | ||
}; | ||
|
||
struct Doubling{ | ||
int n; | ||
int depth; | ||
std::vector<std::vector<int>> index; | ||
Doubling(std::vector<int> p, long long lim = (1ll << 60) - 1){ | ||
n = p.size(); | ||
for (auto x : p) assert(0 <= x && x < n); | ||
depth = 1; | ||
while ((1ll << depth) <= lim) depth++; | ||
index.resize(depth); | ||
index[0] = p; | ||
for (int i = 1; i < depth; i++){ | ||
index[i].resize(n); | ||
for (int j = 0; j < n; j++){ | ||
index[i][j] = index[i - 1][index[i - 1][j]]; | ||
} | ||
} | ||
} | ||
int query(int ind, long long times){ | ||
assert(0 <= ind && ind < n); | ||
assert(0 <= times && times < (1ll << depth)); | ||
int i = 0; | ||
while (times){ | ||
if (times & 1) ind = index[i][ind]; | ||
i++; | ||
times >>= 1; | ||
} | ||
return ind; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#define PROBLEM "https://judge.yosupo.jp/problem/staticrmq" | ||
|
||
#include "../../ds/Doubling.hpp" | ||
|
||
int op(int a, int b){ | ||
return std::min(a, b); | ||
} | ||
|
||
#include <iostream> | ||
|
||
int main(){ | ||
std::ios::sync_with_stdio(false); | ||
std::cin.tie(nullptr); | ||
int N, Q; | ||
std::cin >> N >> Q; | ||
std::vector<int> A(N), P(N); | ||
for (int i = 0; i < N; i++){ | ||
std::cin >> A[i]; | ||
P[i] = std::min(i + 1, N - 1); | ||
} | ||
po167::Doubling_op<int, op> D(P, A, N); | ||
while (Q--){ | ||
int l, r; | ||
std::cin >> l >> r; | ||
std::cout << D.query(l, 1 << 30, r - l).second << "\n"; | ||
} | ||
} |