【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
 

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


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值