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

这题可以用线段树做,数据区间合并,这里要注意题意,每次R恢复的是上次炸过的地方,不管是不是重复炸过,如D 6 D 3 D 3 D 3 R R,那么6那个点并没有恢复,这里WA了快10次。。。
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
#define maxn 50005
char s[10];
int a[maxn],sum,vis[maxn];
struct node{
	int l,r,llen,rlen,tlen;
}b[4*maxn];

int max(int a,int b){
	return a>b?a:b;
}

void build(int l,int r,int i)
{
	int mid;
	b[i].l=l;b[i].r=r;b[i].llen=b[i].rlen=b[i].tlen=(b[i].r-b[i].l+1);
	if(l==r)return;
	mid=(l+r)/2;
	build(l,mid,i*2);
	build(mid+1,r,i*2+1);
}

void update(int id,int panduan,int i)
{
	int mid;
	if(b[i].l==b[i].r){
		if(panduan==1){
			b[i].llen=b[i].tlen=b[i].rlen=0;
		}
		else{
			b[i].llen=b[i].tlen=b[i].rlen=1;
		}
		return;
	}
	if(b[i*2].r>=id)update(id,panduan,i*2);
	else update(id,panduan,i*2+1);
	b[i].tlen=b[i*2].rlen+b[i*2+1].llen;
	b[i].llen=b[i*2].llen;b[i].rlen=b[i*2+1].rlen;
	if(b[i*2].llen==b[i*2].r-b[i*2].l+1){
		b[i].llen+=b[i*2+1].llen;
	}
	if(b[i*2+1].rlen==b[i*2+1].r-b[i*2+1].l+1){
		b[i].rlen+=b[i*2].rlen;
	}
}

int question(int id,int i)
{
	int mid;
	if(b[i].l==b[i].r || b[i].tlen==(b[i].r-b[i].l+1) || b[i].tlen==0){
		return b[i].tlen;
	}
	if(b[i*2].r>=id){
		if(b[i*2].r-b[i*2].rlen+1<=id){
			return b[i*2].rlen+b[i*2+1].llen;
		}
		else return question(id,i*2);
	}
	else{
		if(b[i*2+1].l+b[i*2+1].llen-1>=id){
			return b[i*2].rlen+b[i*2+1].llen;
		}
		else return question(id,i*2+1);
	}
}

int main()
{
	int n,m,i,j,c,num,flag;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		build(1,n,1);
		num=0;
		memset(vis,0,sizeof(vis));
		for(i=1;i<=m;i++){
			scanf("%s",s);
			if(s[0]=='D'){
				scanf("%d",&c);
					a[++num]=c;
                    update(c,1,1);
			}
			else if(s[0]=='R'){
				if(num>=1)update(a[num--],2,1);
			}
			else if(s[0]=='Q'){
				scanf("%d",&c);
				flag=0;
				printf("%d\n",question(c,1));
			}
		}
	}
	return 0;
}

时间: 2024-08-03 23:15:26

Tunnel Warfare的相关文章

hdu1540 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 con

HDU 1540 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 vill

hdu--1540 Tunnel Warfare(线段树+区间合并)

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

HDU 1540 Tunnel Warfare 平衡树 / 线段树:单点更新,区间合并

Tunnel Warfare                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast

HDU 1540 &amp;&amp; POJ 2892 Tunnel Warfare (线段树,区间合并).

~~~~ 第一次遇到线段树合并的题,又被律爷教做人.TAT. ~~~~ 线段树的题意都很好理解吧.. 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1540 http://poj.org/problem?id=2892 ~~~~ 我的代码:200ms #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #defin

POJ 题目2892 Tunnel Warfare(线段树单点更新查询,求单点所在最大连续区间长度)

Tunnel Warfare Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7307   Accepted: 2997 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Generally sp

hdu 1540 Tunnel Warfare 线段树 单点更新,查询区间长度,区间合并

Tunnel Warfare Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1540 Description During the War of Resistance Against Japan, tunnel warfare was carried out extensively in the vast areas of north China Plain. Gene

HDU1540 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 vill

HDU 1540 Tunnel Warfare 线段树区间合并

Tunnel Warfare 题意:D代表破坏村庄,R代表修复最后被破坏的那个村庄,Q代表询问包括x在内的最大连续区间是多少 思路:一个节点的最大连续区间由(左儿子的最大的连续区间,右儿子的最大连续区间,左儿子的最大连续右区间+右儿子的最大连续左区间)决定 所以线段树的节点应该维护当前节点的最大连续左区间,最大连续右区间,和最大连续区间. 注意更新的时候,如果左儿子全满,父亲节点的左连续区间还要加上右儿子的左区间.反之同理. 查询的时候,可以剪枝,如果是叶子,或为空,或满,则不用往下查询. 查询

hdu 1540 Tunnel Warfare(线段树)

题目链接:hdu 1540 Tunnel Warfare 题目大意:有连续的N个城镇,三种操作: D x:第x城镇被破坏 Q x:插叙第x城镇所在联通块有多少个城镇没有被破坏 R:修复最后一个被破坏的城镇 解题思路:线段树区间合并,每个城镇看成一个叶子节点,用一个vector记录破坏顺序.对于查询来说,每次只要判断是否在mid?R[lson(u)],mid+L[rson(u)]之间即可,否则即递归查询左右子树. #include <cstdio> #include <cstring>