hdu 3308(单点更新,区间合并)

没什么好说的,赤裸裸的线段树...


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define lc l,m,index<<1
#define rc m+1,r,index<<1|1
#define N 100005
struct node
{
	int most;
	int left,right;
	int lmost,rmost;
}seg[N<<2];
int n,q;
void pushup(int l,int r,int index)
{
	int m=(l+r)>>1;
	node& father=seg[index];
	node& lson=seg[index<<1];
	node& rson=seg[index<<1|1];
	father.left=lson.left;
	father.right=rson.right;
	father.lmost=lson.lmost;
	father.rmost=rson.rmost;
	father.most=max(lson.most,rson.most);
	if(lson.right<rson.left)
	{
		father.most=max(father.most,lson.rmost+rson.lmost);
		if(lson.lmost==m-l+1)father.lmost+=rson.lmost;
		if(rson.rmost==r-m)father.rmost+=lson.rmost;
	}
}
void build(int l,int r,int index)
{
	int m=(l+r)>>1;
	if(l==r)
	{
		scanf("%d",&seg[index].left);
		seg[index].right=seg[index].left;
		seg[index].most=seg[index].lmost=seg[index].rmost=1;
		return;
	}
	build(lc);
	build(rc);
	pushup(l,r,index);
}
void updata(int id,int num,int l,int r,int index)
{
	int m=(l+r)>>1;
	if(l==r)
	{
		seg[index].left=seg[index].right=num;
		return;
	}
	if(id<=m)updata(id,num,lc);
	else updata(id,num,rc);
	pushup(l,r,index);
}
int query(int L,int R,int l,int r,int index)
{
	int m=(l+r)>>1;
	if(L==l&&R==r)return seg[index].most;
	if(R<=m)return query(L,R,lc);
	else if(L>m)return query(L,R,rc);
	else
	{
		int ret;
		ret=max(query(L,m,lc),query(m+1,R,rc));
		if(seg[index<<1].right<seg[index<<1|1].left)
		{
			ret=max(ret,min(R,m+seg[index<<1|1].lmost)-max(L,m-seg[index<<1].rmost+1)+1);
		}
		return ret;
	}
}
int main()
{
	int t,a,b;
	char op[2];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&q);
		build(0,n-1,1);
		while(q--)
		{
			scanf("%s%d%d",op,&a,&b);
			if(op[0]=='U')
			{
				updata(a,b,0,n-1,1);
			}
			else
			{
				printf("%d\n",query(a,b,0,n-1,1));
			}
		}
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值