Skip to content

Commit

Permalink
ダブリンぐに二分探索を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
potato167 committed Oct 3, 2024
1 parent fa8582b commit 3b41447
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions ds/Doubling.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ namespace po167{

template<class T, T(*op)(T, T)>
struct Doubling_op{
struct result{
long long times;
int ind;
T val;
};
int n;
int depth;
std::vector<std::vector<int>> index;
Expand All @@ -29,10 +34,11 @@ struct Doubling_op{
}
}
}
std::pair<int, T> query(int start_ind, T start_val, long long times){
result 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;
long long TIMES = times;
while (times){
if (times & 1){
start_val = op(start_val, val[i][start_ind]);
Expand All @@ -41,7 +47,19 @@ struct Doubling_op{
i++;
times >>= 1;
}
return std::make_pair(start_ind, start_val);
return {TIMES, start_ind, start_val};
}
result max_right(int start_ind, T start_val, auto f){
if (!f(start_val)) return {-1, start_ind, start_val};
long long times = 0;
for (int d = depth - 1; d >= 0; d--){
if (f(op(start_val, val[d][start_ind]))){
start_val = op(start_val, val[d][start_ind]);
start_ind = index[d][start_ind];
times += (1ll << d);
}
}
return {times, start_ind, start_val};
}
};

Expand Down
2 changes: 1 addition & 1 deletion test/ds/doubling_rmq.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ int main(){
while (Q--){
int l, r;
std::cin >> l >> r;
std::cout << D.query(l, 1 << 30, r - l).second << "\n";
std::cout << D.query(l, 1 << 30, r - l).val << "\n";
}
}

0 comments on commit 3b41447

Please sign in to comment.