Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10790 Accepted Submission(s): 4235
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!
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.
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<bits/stdc++.h>
using namespace std;
#define lson rt<<1
#define rson rt<<1|1
const int maxn=50007;
int a[maxn];
stack<int>P;
struct node
{
int left,right,mid;
int lx,rx,mx;
int dis()
{
return right-left+1;
}
} c[maxn<<2];
void Push_up(int rt)
{
c[rt].lx=c[lson].lx;
if(c[lson].lx==c[lson].dis())
c[rt].lx+=c[rson].lx;
c[rt].rx=c[rson].rx;
if(c[rson].rx==c[rson].dis())
c[rt].rx+=c[lson].rx;
c[rt].mx=max(c[lson].mx,c[rson].mx);
c[rt].mx=max(c[rt].mx,c[lson].rx+c[rson].lx);
}
void build(int l,int r,int rt)
{
c[rt].left=l;
c[rt].right=r;
c[rt].mid=(l+r)>>1;
if(l==r)
{
c[rt].lx=c[rt].rx=c[rt].mx=a[l];
return;
}
build(l,c[rt].mid,lson);
build(c[rt].mid+1,r,rson);
Push_up(rt);
}
void update(int L,int rt)
{
if(c[rt].left==c[rt].right)
{
c[rt].lx=c[rt].rx=c[rt].mx=a[L];
return;
}
if(L<=c[rt].mid) update(L,lson);
else update(L,rson);
Push_up(rt);
}
int query(int L,int rt)
{
if(c[rt].left==c[rt].right||c[rt].mx==c[rt].dis()||c[rt].mx==0)
return c[rt].mx;
//已到叶子节点或此区间已全为0或1时不必在往下查询。
if(L<=c[rt].mid)
{
if(c[rt].mid-c[lson].rx>=L)
return query(L,lson);
//若L不在左子树的右连续中,则只需查询左子树。
else
return query(L,lson)+query(c[rt].mid+1,rson);
//否则要查询左子树+右子树的左连续。
}
else
{ //同理
if(c[rt].mid+c[rson].lx<L)
return query(L,rson);
else
return query(L,rson)+query(c[rt].mid,lson);
}
}
int main()
{
int n,m,i,j;
while(~scanf("%d%d",&n,&m))
{
for(i=1; i<=n; i++)
a[i]=1;
build(1,n,1);
while(m--)
{
char t[2];
int x;
scanf("%s",&t);
if(t[0]=='D')
{
scanf("%d",&x);
a[x]=0;
P.push(x);
update(x,1);
}
else if(t[0]=='R')
{
if(P.empty())continue;
x=P.top();
P.pop();
a[x]=1;
update(x,1);
}
else
{
scanf("%d",&x);
printf("%d\n",query(x,1));
}
}
}
return 0;
}