一道线段树。看代码吧
#pragma GCC optimize("-O2") #include<bits/stdc++.h> //#define getchar nc #define sight(c) (‘0‘<=c&&c<=‘9‘) #define siight(c) (‘A‘<=c&&c<=‘Z‘) #define LL long long #define deg printf #define dput put #define dputc putchar #define db double #define N 200007 #define eho(x) for(int i=head[x];i;i=net[i]) inline char nc(){ static char buf[1000000],*p1=buf,*p2=buf; return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++; } inline void read(int &x){ static char c; for (c=getchar();!sight(c);c=getchar()); for (x=0;sight(c);c=getchar())x=x*10+c-48; } inline void readC(char &x){ for (x=getchar();!siight(x);x=getchar()); } void write(int x){if (x<10) {putchar(‘0‘+x); return;} write(x/10); putchar(‘0‘+x%10);} inline void writeln(int x){ if (x<0) putchar(‘-‘),x*=-1; write(x); putchar(‘\n‘); } using namespace std; int p[N<<2|N],n,mo,x,lastans,tot; #define Mid (l+r>>1) char ch; void change(int node,int l,int r,int op,int dal){ if (l==r) { p[node]=dal;return;} if (op<=Mid) change(node<<1,l,Mid,op,dal); else change(node<<1|1,Mid+1,r,op,dal); p[node]=max(p[node<<1],p[node<<1|1]); } int query(int node,int l,int r,int L,int R){ if (L<=l&&r<=R) return p[node]; if (R<=Mid) return query(node<<1,l,Mid,L,R); if (L> Mid) return query(node<<1|1,Mid+1,r,L,R); return max(query(node<<1,l,Mid,L,Mid),query(node<<1|1,Mid+1,r,Mid+1,R)); } int main () { // freopen("1.in","r",stdin); read(n); read(mo); while (n--) { readC(ch); read(x); if (ch==‘A‘) { x=(1ll*x+lastans)%mo,change(1,1,N,++tot,x); } else { lastans=query(1,1,N,tot-x+1,tot), writeln(lastans); } } return 0; }