【HDU-1540】【Tunnel Warfare】

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

ac代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<stack>
#include<cstdlib>
#include<algorithm>
#define maxn 300000
using namespace std;
int n,m,rec;
typedef struct Node{
	int lnum;
	int rnum;
	int ok;
}node;
node tree[maxn];
void pushup(int l,int r,int cur)
{//向上更新 
	if(tree[cur<<1].ok==tree[cur<<1|1].ok)
	{//假如有某区域中间有毁坏的村庄,那么这个点就废了,即ok=-1 
		tree[cur].ok=tree[cur<<1].ok;
	}
	else
		tree[cur].ok=-1;
	int m=(l+r)>>1;
	if(tree[cur<<1].lnum==(m-l+1))//由子节点的lnum,rnum来判断父节点的lnum,rnum 
		tree[cur].lnum=tree[cur<<1].lnum+tree[cur<<1|1].lnum;
	else
		tree[cur].lnum=tree[cur<<1].lnum;
	if(tree[cur<<1|1].rnum==(r-m))
		tree[cur].rnum=tree[cur<<1|1].rnum+tree[cur<<1].rnum;
	else
		tree[cur].rnum=tree[cur<<1|1].rnum;
}
void pushdown(int l,int r,int cur)
{
	if(tree[cur].ok!=-1)
	{
		int m=(l+r)>>1;
		tree[cur<<1].ok=tree[cur<<1|1].ok;
		if(tree[cur].ok==0)
		{
			tree[cur<<1].lnum=tree[cur<<1].rnum=m-l+1;
			tree[cur<<1|1].lnum=tree[cur<<1|1].rnum=r-m;
		}
		else
		{
			tree[cur<<1].lnum=tree[cur<<1].rnum=0;
			tree[cur<<1|1].lnum=tree[cur<<1|1].rnum=0;
		}
	}
	tree[cur].ok=-1;
}
void build(int l,int r,int cur)
{//建树,设刚开始所有村庄都是ok=0的,好的 
	tree[cur].ok=0;
	if(l==r)
	{
		tree[cur].lnum=tree[cur].rnum=1;
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,cur<<1);
	build(mid+1,r,cur<<1|1);
	pushup(l,r,cur);	
}
void updata(int id,int v,int l,int r,int cur)
{
	if(l==r&&l==id)
	{
		tree[cur].ok=v;
		if(v==0)
			tree[cur].lnum=tree[cur].rnum=1;
		else
			tree[cur].lnum=tree[cur].rnum=0;
		return ;
	}
	pushdown(l,r,cur);
	int m=(l+r)>>1;
	if(id<=m)
		updata(id,v,l,m,cur<<1);
	else
		updata(id,v,m+1,r,cur<<1|1);
	pushup(l,r,cur);
}

node queryll(int lt,int rt,int l,int r,int cur)
{
	if(lt>rt)
	{
		node ans;
		ans.lnum=ans.rnum=0;
		return ans;
	}
	if(lt==l&&rt==r)
		return tree[cur];
	pushdown(l,r,cur);
	int m=(l+r)>>1;
	node ans;
	if(rt<=m)
		ans=queryll(lt,rt,l,m,cur<<1);
	else if(lt>m) ans=queryll(lt,rt,m+1,r,cur<<1|1);
	else
	{
		node a=queryll(lt,m,l,m,cur<<1);
		node b=queryll(m+1,rt,m+1,r,cur<<1|1);
		if(a.lnum==(m-lt+1))
			ans.lnum=a.lnum+b.lnum;
		else
			ans.lnum=a.lnum;
		if(b.rnum==(rt-m))
			ans.rnum=b.rnum+a.rnum;
		else 
			ans.rnum=b.rnum;	
	} 
	pushup(l,r,cur);
	return ans;
}
int main()
{
	stack <int> sta;
	char op[4];
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		while(!sta.empty())
			sta.pop();
		build(1,n,1);
		int a;
		while(m--)
		{
			cin>>op;
			if(op[0]!='R')
				scanf("%d",&a);
			if(op[0]=='D')
			{
				sta.push(a);
				updata(a,1,1,n,1);
			}
			else if(op[0]=='R')
			{
				int t=sta.top();
				sta.pop();
				updata(t,0,1,n,1);
			}
			else
			{
				node ans1=queryll(1,a-1,1,n,1);
				node ans2=queryll(a,n,1,n,1);
				if(ans2.lnum==0)
					printf("0\n");
				else
					printf("%d\n",ans1.rnum+ans2.lnum);
			}
		}
	}
	return 0;
}

这道题目主要就是考察区间合并,中间定义ok 来判断它是否能继续联通,如果它废了,那么上边的父区域就废了,需要及时的updata ,最后查询就可以,需要注意一点就是 如果a点毁坏了,就直接输出0.

之后就没了。需要注意的就是核心处 pushup  它是区间合并的关键

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值