hdu1540 && POJ2892 Tunnel Warfare

【比赛提醒】BestCoder 你报名了吗?(点击报名)

【科普】什么是BestCoder?如何参加?

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4513    Accepted Submission(s): 1725

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

Source

POJ Monthly

Recommend

LL   |   We have carefully selected several similar problems for you:  1542 1255 1698 1828 2871

这道题也是典型的线段树区间合并的题, 主要是要记录一段区间左边连续的长度, 右边连续长度, 单点更新,查询时只要判断要查的点是不是落在中间连续处,否则就继续递归查询

/*************************************************************************
    > File Name: hdu1540.cpp
    > Author: ALex
    > Mail: [email protected]
    > Created Time: 2015年01月11日 星期日 12时24分34秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 500010;
stack <int> re;

struct node
{
	int l, r;
	int l_sum;
	int r_sum;
}tree[N << 2];

void pushup (int p)
{
	tree[p].l_sum = tree[p << 1].l_sum;
	tree[p].r_sum = tree[p << 1 | 1].r_sum;
	if (tree[p << 1].l_sum == tree[p << 1].r - tree[p << 1].l + 1)
	{
		tree[p].l_sum += tree[p << 1 | 1].l_sum;
	}
	if (tree[p << 1 | 1].r_sum == tree[p << 1 | 1].r - tree[p << 1 | 1].l + 1)
	{
		tree[p].r_sum += tree[p << 1].r_sum;
	}
}

void build (int p, int l, int r)
{
	tree[p].l = l;
	tree[p].r = r;
	tree[p].l_sum = tree[p].r_sum = (r - l + 1);
	if (l == r)
	{
		return;
	}
	int mid = (l + r) >> 1;
	build (p << 1, l, mid);
	build (p << 1 | 1, mid + 1, r);
}

void update (int p, int pos, int sta)
{
	if (tree[p].l == tree[p].r)
	{
		tree[p].l_sum = tree[p].r_sum = sta;
		return;
	}
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (pos <= mid)
	{
		update (p << 1, pos, sta);
	}
	else
	{
		update (p << 1 | 1, pos, sta);
	}
	pushup (p);
}

int query (int p, int pos)
{
	if (tree[p].l == tree[p].r)
	{
		if (tree[p].l_sum == 1)
		{
			return 1;
		}
		return 0;
	}
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (pos <= mid)
	{
		if (pos >= mid - tree[p << 1].r_sum + 1)
		{
			return tree[p << 1].r_sum + tree[p << 1 | 1].l_sum;
		}
		return query (p << 1, pos);
	}
	else
	{
		if (pos <= mid + tree[p << 1 | 1].l_sum)
		{
			return tree[p << 1 | 1].l_sum + tree[p << 1].r_sum;
		}
		return query (p << 1 | 1, pos);
	}
}

int main()
{
	int n, m;
	int x;
	char op[10];
	while (~scanf("%d%d", &n, &m))
	{
		build (1, 1, n);
		while (!re.empty())
		{
			re.pop();
		}
		while (m--)
		{
			scanf("%s", op);
			if(op[0] == 'R')
			{
				if (re.empty())
				{
					continue;
				}
				update (1, re.top(), 1);
				re.pop();
			}
			else
			{
				scanf("%d", &x);
				if (op[0] == 'D')
				{
					update (1, x, 0);
					re.push(x);
				}
				else
				{
					printf("%d\n", query (1, x));
				}
			}
		}
	}
	return 0;
}
时间: 2024-10-14 14:54:46

hdu1540 && POJ2892 Tunnel Warfare的相关文章

POJ2892 Tunnel Warfare 题解

POJ2892 Tunnel Warfare 线段树例题解析合集 题意:有一条相邻节点相连的链(1和2,2和3,...n-1和n相连),有3种操作:1.破坏某一个节点 2.问从某个点可以到的点有多少个,即中间没有被破坏的点(包括自己) 3.重建当前最后一个被破坏的点 建立线段树维护每个点能到达的左右边界(初始值都为1,n),破坏一个点k时(设k能到达的左右边界为l1,r1),将l1到k-1的区间内的点的右边界修改为k,将k+1到r1的区间内的点的左边界修改为k 用一个栈存储点的破坏顺序,重建一个

POJ2892 Tunnel Warfare

题解: 树状数组+二分裸题. 对于二分时的细节问题,要仔细分析再确定. 代码: #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<map> #include<set> using namespace std; #define pb push_back #define mp make_pair #define se sec

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

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(线段树+区间合并)

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

kuangbin专题七 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 平衡树 / 线段树:单点更新,区间合并

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