来源:BZOJ1012
BZOJ竟然用cout会RE。。。。
很简单的线段树
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int m,MOD,pre,cnt;
const int INF = 0x7fffffff;
struct Tree{
int l,r;
int d;
};
Tree tree[800005];
void build(int rt,int left,int right){
tree[rt].l=left;
tree[rt].r=right;
tree[rt].d=-INF;
if(left==right) return ;
int mid=(left+right)>>1;
build(rt<<1,left,mid);
build(rt<<1|1,mid+1,right);
}
void update(int rt,int left,int data){
int l=tree[rt].l;
int r=tree[rt].r;
if(l==r){
tree[rt].d=data;
return ;
}
int mid=(l+r)>>1;
if(left<=mid) update(rt<<1,left,data);
else update(rt<<1|1,left,data);
tree[rt].d=max(tree[rt<<1].d,tree[rt<<1|1].d);
}
int query(int rt,int left,int right){
int l=tree[rt].l;
int r=tree[rt].r;
if(left==l&&r==right){
return tree[rt].d;
}
int mid=(l+r)>>1;
if(right<=mid) return query(rt<<1,left,right);
else if(left>mid) return query(rt<<1|1,left,right);
else return max(query(rt<<1,left,mid),query(rt<<1|1,mid+1,right));
}
int main(){
scanf("%d%d",&m,&MOD);
build(1,1,m);
for(int i=0;i<m;i++){
char ch[5];
scanf("%s",&ch);
if(ch[0]=='A'){
cnt++;
int x;
scanf("%d",&x);
x=(x+pre)%MOD;
update(1,cnt,x);
}
else{
int x;
scanf("%d",&x);
pre=query(1,cnt-x+1,cnt);
printf("%d\n",pre);
}
}
return 0;
}