平胸而论 线段树写错可太难调试了
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
int n,p,tot;
int t[N << 4];
void update(int x,int L = 1,int R = n,int rt = 1)
{
if(L == R)
{
t[rt] = x;
return;
}
int mid = L + R >> 1;
if(tot <= mid) update(x,L,mid,rt << 1);
else update(x,mid+1,R,rt<<1|1);
t[rt] = max(t[rt], max(t[rt << 1], t[rt << 1 | 1]));
//printf("%d %d %d %d\n",rt,t[rt],L,R);
}
int query(int l,int r,int L = 1,int R = n,int rt = 1)
{
if(l > r) return 0;
//printf("%d %d %d %d %d\n",l,r,L,R,r);
if(l <= L && R <= r) return t[rt];
int mid = L + R >> 1;
if(r <= mid) return query(l,r,L,mid,rt<<1);
else if(l > mid) return query(l,r,mid+1,R,rt<<1|1);
else return max(query(l,mid,L,mid,rt<<1),query(mid+1,r,mid+1,R,rt<<1|1));
}
int main()
{
cin >> n >> p;
char op[2];
int pre=0;
for(int i = 1,x; i <= n; ++i)
{
scanf("%s%d",op,&x);
if(op[0]=='A')
{
tot++;
update((x+pre)%p);
}
else
{
int l = tot - x + 1, r = tot;
pre = query(l,r);
printf("%d\n",pre);
}
}
}