mirror of
https://gitcode.com/Zengtudor/alg2025.git
synced 2025-08-21 18:52:07 +00:00
update
This commit is contained in:
parent
244f30692c
commit
48ef07ec43
47
src/8/18/sparsetable.cpp
Normal file
47
src/8/18/sparsetable.cpp
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
|
#include <istream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
using ll = int64_t;
|
||||||
|
|
||||||
|
std::vector<ll> lg2,nums;
|
||||||
|
std::vector<std::vector<ll>> st;
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
std::iostream::sync_with_stdio(false);
|
||||||
|
std::cin.tie(nullptr);
|
||||||
|
ll n;
|
||||||
|
std::cin>>n;
|
||||||
|
ll q;
|
||||||
|
std::cin>>q;
|
||||||
|
nums.resize(n+1);
|
||||||
|
lg2.resize(1e5+5);
|
||||||
|
for(ll i=2;i<1e5+5;i++){
|
||||||
|
lg2[i]=lg2[i>>1]+1;
|
||||||
|
}
|
||||||
|
st.resize(n+1,std::vector<ll>(lg2[n]+2));
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
std::cin>>nums[i];
|
||||||
|
st[i][0]=nums[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
for(ll j=1;j<lg2[n]+2;j++){
|
||||||
|
for(ll i=1;i<=n;i++){
|
||||||
|
if(i+(1ll<<j)-1>n){
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
st[i][j]=std::max(st[i][j-1],st[i+(1ll<<(j-1))][j-1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while(q--){
|
||||||
|
ll l,r;
|
||||||
|
std::cin>>l>>r;
|
||||||
|
ll range = r-l+1;
|
||||||
|
ll k=lg2[range];
|
||||||
|
std::cout<<
|
||||||
|
std::max(st[l][k],st[r-(1ll<<k)+1][k])
|
||||||
|
<<"\n";
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user