POJ-2892 Tunnel Warfare

Tunnel Warfare
Time Limit: 1000MS Memory Limit: 131072K
   

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 (nm  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:

  1. D x: The x-th village was destroyed.
  2. Q x: The Army commands requested the number of villages that x-th village was directly or indirectly connected with including itself.
  3. 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

Hint

An illustration of the sample input:

      OOOOOOO

D 3   OOXOOOO

D 6   OOXOOXO

D 5   OOXOXXO

R     OOXOOXO

R     OOXOOOO

————————————————————刚吃饭的分割线————————————————————

思路:这道题本身并不难,但是很考验人的细心程度。要询问连续点的个数,那么存储区间内左起连续长度和右起连续长度即可。如果所查询点在这两者内,直接输出区间长度,否则递归查询。

细节1:同一个村庄如果被毁坏多次怎么办

细节2:怎样确定查询点在相应区间内

细节3:多次连续的修复怎么处理

细节4:相邻区间可以合并怎么办

代码如下:

/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <iostream>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
/****************************************/
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mid int m = (l+r) >> 1
#define node int l, int r, int rt
#define quote Node &ll = t[rt<<1], &rr = t[rt<<1|1], &tt = t[rt]
const int N = 5e4+5;
struct Node {
	int left, right;
	int cov;
}t[N<<2];
bool vis[N<<2];
int sta[N];

void Up(int rt, int len)
{
	quote;
	tt.left = ll.left;
	tt.right = rr.right;
	if(tt.left == (len+1>>1)) tt.left += rr.left;
	if(tt.right == (len>>1)) tt.right += ll.right;
}

void Down(int rt, int len)
{
	quote;
	if(tt.cov != -1) {
		ll.cov = rr.cov = tt.cov;
		ll.left = ll.right = tt.cov ? 0 : len+1>>1;
		rr.left = rr.right = tt.cov ? 0 : len>>1;
		tt.cov = -1;
	}
}

void build(node)
{
	t[rt].left = t[rt].right = r-l+1;
	t[rt].cov = -1;
	if(l == r) return ;
	mid;
	build(lson); build(rson);
}

void update(int A, bool op, node)
{
	if(l == r) {
		t[rt].cov = op;
		t[rt].left = t[rt].right = op ? 0 : r-l+1;
		return ;
	}
	Down(rt, r-l+1);
	mid;
	if(A <= m) update(A, op, lson);
	else update(A, op, rson);
	Up(rt, r-l+1);
}

int query(int A, node)
{
	quote;
	mid;
	if(A <= m) {//在左儿子的右区间需要判断连续性,和右儿子的左区间是连着的
		if(A >= m - ll.right + 1) return ll.right + rr.left;
		return query(A, lson);
	}
	else {//同理
		if(A <= m + rr.left) return rr.left + ll.right;
		return query(A, rson);
	}
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	int n, m;
	while(scanf("%d%d", &n, &m)!=EOF) {
		memset(vis, 0, sizeof(vis));//vis
		build(1, n, 1);
		char op[2];
		int A, head = 0;
		while(m--) {
			scanf("%s", op);
			if(op[0] == 'D') {
				scanf("%d", &A);
				update(A, true, 1, n, 1);
				sta[head++] = A;//每摧毁一个村庄就直接入栈
				vis[A] = 1;
			}
			else if(op[0] == 'Q') {
				scanf("%d", &A);
				printf("%d\n", vis[A] ? 0 : query(A, 1, n, 1));//未修复的村庄直接输出0
			}
			else {
				update(sta[--head], false, 1, n, 1);
				vis[sta[head]] = 0;//修复栈顶的村庄,出栈
			}
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值