将整个序列从200000开始倒着插入 , 然后就是树状数组维护前缀最大了
#include<bits/stdc++.h>
#define N 200000
using namespace std;
int c[N+50],m,d,ans,tot;
void Up(int x,int val){for(;x<=N;x+=x&-x) c[x] = max(c[x],val);}
int Q(int x){int ans=0; for(;x;x-=x&-x) ans = max(ans,c[x]); return ans;}
int main(){
scanf("%d%d",&m,&d);
while(m--){
char s[3]; int x; scanf("%s%d",s,&x);
if(s[0]=='A'){x = (x+ans) % d; tot++;Up(N-tot,x);}
if(s[0]=='Q'){ans = Q(N-tot+x-1);printf("%d\n",ans);}
} return 0;
}