这个题是真毒瘤啊$qnq$
那么实际上,对于这个题来说,我们对于每个插入的数,将它插入线段树的尾部。插入之后再递归修改区间最大值。对于每个询问,我们就像普通的询问一样询问即可。
是时候温习温习线段树的板子了。(背不过了)
那么实际上,我们只需要一个单点修改操作和一个询问操作。
#include<iostream>
#include<cstdio>
using namespace std;
#define LL long long
LL t[800001],res=0,length=0;
void insert(LL l,LL r,LL num,LL v,LL root){
if(l==r)t[root]=v;
else {
LL mid=(l+r)>>1;
if(mid>=num)insert(l,mid,num,v,root<<1);
if(mid<num)insert(mid+1,r,num,v,root<<1|1);
t[root]=max(t[root<<1],t[root<<1|1]);
}
}
LL query(LL L,LL R,LL l,LL r,LL root){
if(L>=l&&R<=r){
return t[root];
}
LL lres=0,rres=0,mid=(L+R)>>1;
if(mid>=l)lres=query(L,mid,l,r,root<<1);
if(mid<r)rres=query(mid+1,R,l,r,root<<1|1);
return max(lres,rres);
}
int main(){
LL m,d,a;
char c;
cin>>m>>d;
for(int i=1;i<=m;i++){
cin>>c>>a;
if(c=='Q'){
if(a==0)cout<<(res=0)<<endl;
else cout<<(res=query(1,m,length-a+1,length,1))<<endl;
}
else {
length++;
insert(1,m,length,(a+res)%d,1);
}
}
}