线段树代码强烈建议在洛谷提交!某luogu管理员@yjjr 丧心病狂加强数据竟使线段树代码大面积迷之re=。=
//bzoj1012 最大数
#include<iostream>
#include<cstring>
#include<cstdio>
#define lson l,m,pos<<1
#define rson m+1,r,pos<<1|1
#define ll long long
#define inf 0x3f3f3f3f
#define maxn 200010
using namespace std;
int m,n;
ll sum[801000];
ll t,p,a;
char c;
void pushup(int pos){sum[pos]=max(sum[pos<<1],sum[pos<<1|1]);}
void build(int l,int r,int pos)
{
if (l==r) {sum[pos]=-inf;return;}
int m=(l+r)>>1;
build(lson);build(rson);
}
void update(int o,int d,int l,int r,int pos)
{
if (l==r) {sum[pos]=d;return;}
int m=(l+r)>>1;
if (o<=m) update(o,d,lson);
else update(o,d,rson);
pushup(pos);
}
ll query(int L,int R,int l,int r,int pos)
{
if (L<=l&&r<=R) return sum[pos];
int m=(l+r)>>1;ll ret=0;
if (L<=m) ret=query(L,R,lson);
if (R>m) ret=max(ret,query(L,R,rson));
return ret;
}
int main()
{
n=0;t=0;
scanf("%d%lld",&m,&p);
build(1,maxn,1);
while(m--)
{
scanf(" %c%lld",&c,&a);
if (c=='Q'&&a==0) {puts("0");continue;}
if (c=='Q') t=query(n-a+1,n,1,maxn,1),printf("%lld\n",t);
else n++,update(n,(a+t)%p,1,maxn,1);
}
return 0;
}