Description:

During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally speaking, villages connected by tunnels lay in a line. Except the two at the ends, every village was directly connected with two neighboring ones.

Frequently the invaders launched attack on some of the villages and destroyed the parts of tunnels in them. The Eighth Route Army commanders requested the latest connection state of the tunnels and villages. If some villages are severely isolated, restoration of connection must be done immediately!

Input

The first line of the input contains two positive integers n and m (n, m ≤ 50,000) indicating the number of villages and events. Each of the next m lines describes an event.

There are three different events described in different format shown below:

D x: The x-th village was destroyed.

Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.

R: The village destroyed last was rebuilt.

Output

Output the answer to each of the Army commanders’ request in order on a separate line.

Sample Input

7 9 D 3 D 6 D 5 Q 4 Q 5 R Q 4 R Q 4

Sample Output

1 0 2 4

打地道战,n个村庄用地道连成一条直线,鬼子有时破坏掉一个村庄,这时八路军要修好这个据点。现在要求询问任意一个村庄,

得出这个村庄现在与几个村庄相连,包括它本身。

段树维护连续子区间

将村庄抽象成一个点,正常下值为1,被破坏后变成0,即可将题目抽象成一个求目标节点所在的最长连续1序列的区间的长度。

所以在线段树里面维护3个信息,前缀最长1序列,后缀最长1序列,以及最长1序列。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const ll INF=1e18;
const int maxn=50050;
char str[10];
int stak[maxn];

struct node
{
//ls表示从这个区间左端开始的最大连续区间
//rs表示从这个区间右端开始的最大连续区间
//ms表示这个区间的最大连续区间
int l,r;
int ls,rs,ms;
} tree[maxn*3];

void build(int l,int r,int rt)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].ls=tree[rt].ms=tree[rt].rs=r-l+1;
if(l==r)
return;
int m=(l+r)>>1;
build(l,m,rt<<1);
build(m+1,r,(rt<<1)|1);
}

void pushup(int rt)
{
tree[rt].ls=tree[rt<<1].ls;
tree[rt].rs=tree[(rt<<1)|1].rs;
if(tree[rt].ls==tree[rt<<1].r-tree[rt<<1].l+1)
tree[rt].ls+=tree[(rt<<1)|1].ls;
if(tree[rt].rs==tree[(rt<<1)|1].r-tree[(rt<<1)|1].l+1)
tree[rt].rs+=tree[rt<<1].rs;
tree[rt].ms=max(tree[rt<<1].ms,tree[(rt<<1)|1].ms);
tree[rt].ms=max(tree[rt].ms,tree[rt<<1].rs+tree[(rt<<1)|1].ls);
}

void update(int rt,int flag,int L)
{
if(tree[rt].l==tree[rt].r)
{
if(flag==1)
tree[rt].ls=tree[rt].ms=tree[rt].rs=1;
else
tree[rt].ls=tree[rt].ms=tree[rt].rs=0;
return;
}
int m=(tree[rt].l+tree[rt].r)>>1;
if(L<=m)
update(rt<<1,flag,L);
else
update((rt<<1)|1,flag,L);
pushup(rt);//上传标记
}

int query(int rt,int t)
{
if(tree[rt].l==tree[rt].r||tree[rt].ms==0||tree[rt].ms==tree[rt].r-tree[rt].l+1)
return tree[rt].ms;
int m=(tree[rt].l+tree[rt].r)>>1;
if(t<=m)
{
if(t>=tree[rt<<1].r-tree[rt<<1].rs+1)
return query(rt<<1,t)+query((rt<<1)|1,m+1);
else
return query(rt<<1,t);
}
else
{
if(t<=tree[(rt<<1)|1].l+tree[(rt<<1)|1].ls-1)
return query((rt<<1)|1,t)+query(rt<<1,m);
else
return query((rt<<1)|1,t);
}
}

int main()
{
int m,top,L;
int n;
while(scanf("%d %d",&n,&m)!=EOF)
{
build(1,n,1);
top=0;
for(int i=0; i<m; ++i)
{
scanf("%s",&str);
if(str[0]=='D')
{
scanf("%d",&L);
stak[top++]=L;
update(1,0,L);
}
else if(str[0]=='Q')
{
scanf("%d",&L);
printf("%d\n",query(1,L));
}
else
{
if(L>0)
{
L=stak[--top];
update(1,1,L);
}
}
}
}
return 0;
}