Tunnel Warfare
Problem 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
分析:用三个变量分别来记录当前节点的左边连续区间,右边连续区间和最大连续区间(具体详见代码)
代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
#define maxn 50005
struct node
{
int ls,rs,ms,le,ri;//ls为左端最大连续区间,rs为右端最大连续区间,ms为区间内最大连续区间
int mid()
{
return (le+ri)>>1;
}
} tree[maxn<<2];
int stack[maxn];
void Build(int rt,int le,int ri)//建树
{
tree[rt].le=le;
tree[rt].ri=ri;
tree[rt].ls=tree[rt].rs=tree[rt].ms=ri-le+1;
if(le==ri)
return ;
int mid=tree[rt].mid();
Build(rt<<1,le,mid);
Build(rt<<1|1,mid+1,ri);
}
void Upfather(int rt)//更新当前节点
{
tree[rt].ls=tree[rt<<1].ls;
tree[rt].rs=tree[rt<<1|1].rs;
tree[rt].ms=max(max(tree[rt<<1].ms,tree[rt<<1|1].ms),tree[rt<<1].rs+tree[rt<<1|1].ls);
//父亲区间内的最大区间必定是,左子树最大区间,右子树最大区间和左右子树合并的中间区间,三者中最大的那个区间值
if(tree[rt<<1].ls==tree[rt<<1].ri-tree[rt<<1].le+1)//左子树区间满了的话,父亲左区间要加上右孩子的左区间
tree[rt].ls+=tree[rt<<1|1].ls;
if(tree[rt<<1|1].rs==tree[rt<<1|1].ri-tree[rt<<1|1].le+1)//同理
tree[rt].rs+=tree[rt<<1].rs;
}
void Updata(int rt,int k,int op)
{
if(tree[rt].le==tree[rt].ri)
{
tree[rt].ls=tree[rt].rs=tree[rt].ms=op;
return ;
}
int mid=tree[rt].mid();
if(k<=mid)
Updata(rt<<1,k,op);
else
Updata(rt<<1|1,k,op);
Upfather(rt);
}
int Query(int rt,int k)
{
if((tree[rt].ri==tree[rt].le)||(tree[rt].ms==0)||(tree[rt].ms==tree[rt].ri-tree[rt].le+1))
return tree[rt].ms;//到了叶子节点或者该访问区间为空或者已满都不必要往下走了
int mid=tree[rt].mid();
if(k<=mid)
{
if(k>=tree[rt<<1].ri-tree[rt<<1].rs+1)//k<=mid,看左子树,tree[rt<<1].ri-tree[rt<<1].rs+1代表左子树右边连续区间的
return Query(rt<<1,k)+Query(rt<<1|1,mid+1);//左边界值,如果k在左子树的右区间内,则要也要计算右子树的左区间有多长
else
return Query(rt<<1,k);//如果不在左子树的右边界区间内,则只需要计算左子树
}
else
{
if(k<=tree[rt<<1|1].le+tree[rt<<1|1].ls-1)//同理
return Query(rt<<1,mid)+Query(rt<<1|1,k);
else
return Query(rt<<1|1,k);
}
}
int main()
{
int n,m,x;
while(~scanf("%d%d",&n,&m))
{
int top=0;
Build(1,1,n);
char s[5];
for(int i=1; i<=m; ++i)
{
scanf("%s",s);
if(s[0]=='D')
{
scanf("%d",&x);
stack[++top]=x;
Updata(1,x,0);
}
else if(s[0]=='Q')
{
scanf("%d",&x);
printf("%d\n",Query(1,x));
}
else
{
if(top>0)
{
x=stack[top--];
Updata(1,x,1);
}
}
}
}
return 0;
}