hdu1540 线段树维护左右第一个0的位置

线段树维护村庄状态。

1表示未摧毁

0表示摧毁。

问最大连续村庄,只需要求左右最近摧毁村庄位置相减即可。//包括自身的村庄

以上过程也可以用set进行维护,即:set存被摧毁村庄id。用二分快速查找。

//KX
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
//unordered_map<int,int>mp;
const int M= 1e5+7;
#define ls o*2
#define rs o*2+1
#define m (l+r)/2
//单点更新,维护最小值,区间查询,区间最右边0,和最左边的0的坐标。 
int mn[M<<2],a[M];
int n;
void pu(int o)
{
	mn[o]=min(mn[ls],mn[rs]);
}
void bd(int o,int l,int r)
{

	if(l==r)
	{
		mn[o]=1;
		if(l==0||l==n+1)mn[o]=0; 
		return;
	}
	bd(ls,l,m);
	bd(rs,m+1,r);
	pu(o);
 } 
void up(int o,int l,int r,int x,int d)
{
	if(l==r)
	{
		mn[o]=d;
		return ;
	}
	if(x<=m)up(ls,l,m,x,d);
	else up(rs,m+1,r,x,d);
	pu(o);
}
int pos,f;
void qul(int o,int l,int r,int x)
{
	if(f||r<x)return ;
	if(l==r)
	{
		if(mn[o]==1)return ;
		pos=l;
		f=true;
		return ;
	}
	if(mn[ls]==0) qul(ls,l,m,x);
	qul(rs,m+1,r,x);
}
void qur(int o,int l,int r,int x)
{
	if(x==0)return ;
	if(f||l>x)return ;
	if(l==r)
	{
		if(mn[o]==1)return;
		pos=l;
		f=true;
		return ;
	}
	if(mn[rs]==0) qur(rs,m+1,r,x);
	qur(ls,l,m,x);
}
int qu(int o,int l,int r,int x)
{
	if(l==r)
		return mn[o];
	if(x<=m)return qu(ls,l,m,x);
	return qu(rs,m+1,r,x);
}
int sk[M];
int main()
{
	int q;
	while(~scanf("%d%d",&n,&q))
	{
		char s[3];
		int x;
		int N=n+1;
		bd(1,0,N);
		int top=0;
		for(int i=1;i<=q;i++)
		{
		//	puts("ooooooooooooooo");
			scanf("%s",s);
			if(s[0]=='D')
			{
				scanf("%d",&x);
				up(1,0,N,x,0);
		//		printf("=======   %d            %d\n",x,qu(1,1,n,x));
				sk[++top]=x;
			}
			else if(s[0]=='Q')
			{
				scanf("%d",&x);
				if(qu(1,0,N,x)==0)
				{
					printf("%d\n",0);
					continue;
				} 
				f=false;
				int l,r;
				qul(1,0,N,x);//右边区间 最左边的0的位置
				r=pos;
				f=false;
				qur(1,0,N,x);//左边区间 最右边的0的位置
				l=pos;
				printf("%d\n",r-l-1);
			}
			else 
			{
				if(top==0)continue;
				up(1,0,N,sk[top],1);
				top--;
			}
		}
	}
	
   	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值