Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3076 Accepted Submission(s): 1175
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 <iostream>
#include <cstdio>
#include <cstring>
#define maxn 50005
using namespace std;
struct Tnode
{
int l,r;
int lel,ril,mal;
}node[maxn<<2];
int n,m,cnt;
int sta[maxn];
int vis[maxn];
char s[20];
int maxx(int x1,int x2)
{
return x1>x2?x1:x2;
}
void init(int ll,int rr,int rt)
{
node[rt].l=ll;
node[rt].r=rr;
node[rt].lel=node[rt].ril=node[rt].mal=rr-ll+1;
if(ll==rr) return ;
int mid=(ll+rr)>>1;
init(ll,mid,rt<<1);
init(mid+1,rr,rt<<1|1);
}
void update(int ll,int rr,int rt,int pos,int val)
{
if(ll==rr)
{
if(val) node[rt].lel=node[rt].ril=node[rt].mal=1;
else node[rt].lel=node[rt].ril=node[rt].mal=0;
return ;
}
int mid=(ll+rr)>>1;
if(pos<=mid)
{
update(ll,mid,rt<<1,pos,val);
}
else
{
update(mid+1,rr,rt<<1|1,pos,val);
}
node[rt].lel=node[rt<<1].lel;
node[rt].ril=node[rt<<1|1].ril;
if(node[rt<<1].lel==node[rt<<1].r-node[rt<<1].l+1) // 将区间进行连接 更新lel
node[rt].lel+=node[rt<<1|1].lel;
if(node[rt<<1|1].ril==node[rt<<1|1].r-node[rt<<1|1].l+1) // 更新ril
node[rt].ril+=node[rt<<1].ril;
node[rt].mal=maxx(node[rt<<1].mal,node[rt<<1|1].mal); // 更新mal
node[rt].mal=maxx(node[rt].mal,node[rt<<1].ril+node[rt<<1|1].lel);
}
int query(int rt,int pos)
{
if(node[rt].l==node[rt].r||node[rt].mal==0||node[rt].mal==(node[rt].r-node[rt].l+1))
{ // 这些情况就不用往下找了
return node[rt].mal;
}
int mid=(node[rt].l+node[rt].r)>>1;
if(pos<=mid)
{
if(pos<=node[rt<<1].r-node[rt<<1].ril) return query(rt<<1,pos);
else return query(rt<<1,pos)+query(rt<<1|1,mid+1); // 这个处理很经典 可惜不是我想到的
} // 有分治法的思想 逐渐转化为小的问题
else
{
if(pos>=node[rt<<1|1].l+node[rt<<1|1].lel) return query(rt<<1|1,pos);
else return query(rt<<1|1,pos)+query(rt<<1,mid);
}
}
int main()
{
int i,j,vv,l,r,t;
while(~scanf("%d%d",&n,&m))
{
cnt=0;
init(1,n,1);
memset(vis,0,sizeof(vis));
for(i=1;i<=m;i++)
{
getchar();
scanf("%s",s);
if(s[0]=='Q')
{
scanf("%d",&l);
printf("%d\n",query(1,l));
}
else if(s[0]=='D')
{
scanf("%d",&l);
sta[++cnt]=l;
vis[l]=1;
update(1,n,1,l,0);
}
else
{
if(cnt>=1)
{
if(vis[sta[cnt]]) // 假如全部都是好的 或者在之前已经修好的话就不用再修了
{
update(1,n,1,sta[cnt],1);
cnt--;
}
}
}
}
}
return 0;
}