Tunnel Warfare
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4366 Accepted Submission(s): 1670
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
Source
解题思路:线段树代码题
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define Max 50005
using namespace std;
bool lflag,rflag;
struct
{
int lsum;
int rsum;
int msum;
bool l;
bool r;
}tree[Max<<2];
void push_up(int rt,int m)
{
tree[rt].l=tree[rt<<1].l;
tree[rt].r=tree[rt<<1|1].r;
tree[rt].lsum=tree[rt<<1].lsum;
tree[rt].rsum=tree[rt<<1|1].rsum;
if(tree[rt<<1].r&&tree[rt<<1|1].l)
{
if(tree[rt].lsum==m-(m>>1))
tree[rt].lsum+=tree[rt<<1|1].lsum;
if(tree[rt].rsum==(m>>1))
tree[rt].rsum+=tree[rt<<1].rsum;
tree[rt].msum=max(tree[rt<<1].rsum+tree[rt<<1|1].lsum,max(tree[rt<<1].msum,tree[rt<<1|1].msum));
}
else
tree[rt].msum=max(tree[rt<<1].msum,tree[rt<<1|1].msum);
}
void build(int l,int r,int rt)
{
tree[rt].l=tree[rt].r=true;
tree[rt].lsum=tree[rt].rsum=tree[rt].msum=r-l+1;
int m=(l+r)>>1;
if(l==r)
return ;
build(lson);
build(rson);
}
void update(int pos,int c,int l,int r,int rt)
{
if(l==r)
{
if(c)
{
tree[rt].l=tree[rt].r=false;
tree[rt].lsum=tree[rt].rsum=tree[rt].msum=0;
}
else
{
tree[rt].l=tree[rt].r=true;
tree[rt].lsum=tree[rt].rsum=tree[rt].msum=1;
}
return ;
}
int m=(l+r)>>1;
if(pos<=m)
update(pos,c,lson);
else
update(pos,c,rson);
push_up(rt,r-l+1);
}
int query(int pos,int l,int r,int rt)
{
int ans=0;
if(l==r)
{
ans=tree[rt].msum;
return ans;
}
int m=(l+r)>>1;
int len=r-l+1;
if(pos<=m)
{
ans+=query(pos,lson);
if(tree[rt<<1|1].l&&tree[rt<<1].r&&rflag)
{
ans+=tree[rt<<1|1].lsum;
if(tree[rt<<1|1].lsum!=(len>>1))
rflag=false;
}
else
rflag=false;
}
else
{
ans+=query(pos,rson);
if(tree[rt<<1].r&&tree[rt<<1|1].l&&lflag)
{
ans+=tree[rt<<1].rsum;
if(tree[rt<<1].rsum!=len-(len>>1))
lflag=false;
}
else
lflag=false;
}
return ans;
}
int main()
{
int m,n,x;
char op[3];
bool visit[Max];
stack<int> s;
while(~scanf("%d%d",&n,&m))
{
memset(visit,false,sizeof(visit));
while(!s.empty())
s.pop();
build(1,n,1);
while(m--)
{
scanf("%s",op);
switch(op[0])
{
case 'D':
scanf("%d",&x);
s.push(x);
visit[x]=true;
update(x,1,1,n,1);
break;
case 'Q':
lflag=rflag=true;
scanf("%d",&x);
printf("%d\n",query(x,1,n,1));
break;
case 'R':
x=s.top();
s.pop();
while(!visit[x])
{
x=s.top();
s.pop();
}
update(x,0,1,n,1);
visit[x]=false;
break;
}
}
}
return 0;
}