1. 程式人生 > >【HDU1540】【線段樹】【區間合併】【一個PushUp合併還有下去的時候取區間】【可作為模板】

【HDU1540】【線段樹】【區間合併】【一個PushUp合併還有下去的時候取區間】【可作為模板】

Tunnel Warfare

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5675    Accepted Submission(s): 2194


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  
Statistic |  Submit |  Discuss | 
Note



#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
    
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_back
#define lson l,m,rt<<1
#define rson m,r,rt<<1|1

int n,m;
bool ok[50010];
const int MAXN = 50010;
int lmax[MAXN*4];
int rmax[MAXN*4];
int mmax[MAXN*4];

void PushUp(int rt,int l1,int l2)
{

	if(lmax[rt<<1] == l1)
	{
		lmax[rt] = l1 + lmax[rt<<1|1];
	}
	else
	{
		lmax[rt] = lmax[rt<<1];
	}
	if(rmax[rt<<1|1] == l2)
	{
		rmax[rt] = l2 + rmax[rt<<1];
	}
	else
	{
		rmax[rt] = rmax[rt<<1|1];
	}
	mmax[rt] = max(lmax[rt],rmax[rt]);
	mmax[rt] = max(mmax[rt],max(mmax[rt<<1],mmax[rt<<1|1]));

	mmax[rt] = max(mmax[rt],rmax[rt<<1] + lmax[rt<<1|1]);
}

void build(int l,int r,int rt)
{

	lmax[rt] = rmax[rt] = mmax[rt] = r - l;
	if(r - l == 1)
	   return ;
	
	int m = (l + r) >> 1;
	build(lson);
	build(rson);

}

void update(int p,int v,int l,int r,int rt)
{
	if(r - l == 1)
	{
		lmax[rt] = rmax[rt] = mmax[rt] = v;
		return;
	}

	int m = (l + r) >> 1;
	if(p <= m-1) update(p,v,lson);
	else update(p,v,rson);

	PushUp(rt,m-l,r-m);
}

int query(int u,int l,int r,int rt)
{
	if(r - l == 1 || mmax[rt] == r -l || mmax[rt] == 0)
	{
		return mmax[rt];
	}

	int m = (l + r) >> 1;
	if(u <= m-1)
	{
		int mid = m - rmax[rt<<1];
		if(u >= mid)
		{
			return query(u,lson)+query(m,rson);
		}
		else
			return query(u,lson);
	}
	else
	{
		int mid = m + lmax[rt<<1|1];
		if(u < mid)
		{
			return query(m-1,lson)+query(u,rson);
		}
		else
			return query(u,rson);

	}
}

char str[2];
int main()
{
	while(scanf("%d%d",&n,&m) != EOF)
	{
		stack<int> s;
		for(int i=1;i<=n;i++) ok[i] = true;

		build(1,n+1,1);

		for(int i=0;i<m;i++)
		{
			scanf("%s",str);
			if(str[0] == 'D')
			{
				int u;
				scanf("%d",&u);
				//if(!ok[u]) continue;
				ok[u] = false;
				s.push(u);
				update(u,0,1,n+1,1);
			}	
			else if(str[0] == 'R')
			{
				if(s.empty()) continue;
				int u = s.top();
				s.pop();
				if(ok[u]) continue; 
				update(u,1,1,n+1,1);
				ok[u] = true; 
			}
			else
			{
				int u;
				scanf("%d",&u);
				printf("%d\n",query(u,1,n+1,1));
			}
		}
	}
}