水题
ACcode:
#include<stdio.h>
#include<iostream>
using namespace std;
const int size=55555;
struct Node
{
int len,lc,rc;
} tre[size<<2];
void build(int rt,int l,int r)
{
tre[rt].lc=tre[rt].rc=tre[rt].len=r-l+1;
if (l==r) return ;
int m=(l+r)>>1;
build(rt<<1,l,m);
build(rt<<1|1,m+1,r);
}
void PushUp(int rt)
{
int r1=rt<<1,r2=r1|1;
tre[rt].lc=tre[r1].lc;
if (tre[r1].lc==tre[r1].len) tre[rt].lc+=tre[r2].lc;
tre[rt].rc=tre[r2].rc;
if (tre[r2].rc==tre[r2].len) tre[rt].rc+=tre[r1].rc;
}
void update(int rt,int l,int r,int p,int v)
{
if (l==r)
{
tre[rt].lc=tre[rt].rc=v;
return ;
}
int m=(l+r)>>1;
if (p<=m) update(rt<<1,l,m,p,v);
else update(rt<<1|1,m+1,r,p,v);
PushUp(rt);
}
Node query(int rt,int l,int r,int p)
{
if (l==r) return tre[rt];
Node tmp;
int m=(l+r)>>1,f;
int r1=rt<<1,r2=r1|1;
if (p<=m) f=1,tmp=query(r1,l,m,p);
else f=2,tmp=query(r2,m+1,r,p);
if (f==1&&(p+tmp.rc-1)>=m) tmp.rc=m-p+1+tre[r2].lc;
else if (f==2&&(p-tmp.lc)<=m) tmp.lc=p-m+tre[r1].rc;
return tmp;
}
int main()
{
char op[5];
int n,q,pos,top;
int vage[size];
while (~scanf("%d %d",&n,&q))
{
top=0;
build(1,1,n);
while (q--)
{
scanf("%s",op);
if (op[0]=='R')
{
pos=vage[--top];
while (vage[top-1]==pos) top--;
update(1,1,n,pos,1);
}
else
{
scanf("%d",&pos);
if (op[0]=='D')
{
vage[top++]=pos;
update(1,1,n,pos,0);
}
else
{
Node tmp=query(1,1,n,pos);
printf("%d\n",tmp.lc>0?(tmp.lc+tmp.rc-1):0);
}
}
}
}
return 0;
}