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;
}


你可能感兴趣的:(线段树)