链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1012
做法就是倒着做ST表,这样就可以实现log(n)更新,O(1)查询。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
int stmax[maxn][20];
int lo[maxn];
int n,m,t,D,x;
char op[3];
int query(int l){
return max(stmax[n][lo[l]],stmax[n-l+(1<<lo[l])][lo[l]]);
}
int main(){
lo[1]=0;
for(int i=2;i<maxn;i++){
lo[i]=lo[i>>1]+1;
}
scanf("%d%d",&m,&D);
while(m--){
scanf("%s%d",op,&x);
if(op[0]=='Q'){
t=query(x);
printf("%d\n",t);
}else{
stmax[++n][0]=(1LL*x+t)%D;
for(int i=1;n-(1<<i)>=0;i++){
stmax[n][i]=max(stmax[n][i-1],stmax[n-(1<<(i-1))][i-1]);
}
}
}
return 0;
}