hdu 3308 - 线段树

题目链接:https://vjudge.net/problem/HDU-3308

 

解题思路:

题目很明显的线段树,但不是那么好写。要求区间最长的连续递增长度,那么需要多考虑的就是当左右区间合并时如果右区间的左边界值大于左区间的右边界值时,会合并出一个新的连续递增段,所以我们还要维护一个区间的左右边界最长上升长度才行。

 

#include<bits/stdc++.h>
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
using namespace std;
const int mx = 1e5 + 10;
int a[mx];
struct node
{
	int len,ls,rs;
	int sum;
}s[mx*3];
node push_up(int mid,node p1,node p2)
{
	node now;
	now.len = max(p1.len,p2.len);
	now.ls = now.rs = 0;
	now.sum = p1.sum + p2.sum;
	if(a[mid+1]>a[mid]){
		now.len = max(now.len,p1.rs+p2.ls);
		if(p1.len==p1.sum) now.ls = p1.len + p2.ls;
		if(p2.len==p2.sum) now.rs = p2.len + p1.rs;
	}
	now.ls = max(now.ls,p1.ls);
	now.rs = max(now.rs,p2.rs);
	return now;
}
void build(int l,int r,int rt)
{
	if(l==r){
		scanf("%d",a+l);
		s[rt].len = s[rt].ls = s[rt].rs = 1;
		s[rt].sum = 1;
		return ;
	}
	int mid = (l+r)>>1;
	build(lson);build(rson);
	s[rt] = push_up(mid,s[rt<<1],s[rt<<1|1]);
}
void update(int l,int r,int rt,int p,int v)
{
	if(l==r){
		a[l] = v;
		return ;
	}
	int mid = (l+r)>>1;
	if(p<=mid) update(lson,p,v);
	else update(rson,p,v);
	s[rt] = push_up(mid,s[rt<<1],s[rt<<1|1]);
}
node query(int l,int r,int rt,int L,int R)
{
	if(L<=l&&r<=R) return s[rt];
	int mid = (l+r)>>1;
	if(L<=mid&&R>mid) 
	return push_up(mid,query(lson,L,R),query(rson,L,R));
	if(L<=mid) return query(lson,L,R);
	return query(rson,L,R);
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--){
		int n , m ;
		scanf("%d%d",&n,&m);
		build(1,n,1);
		char c[10];
		int x,y;
		while(m--){
			scanf("%s%d%d",c,&x,&y);
			if(c[0]=='Q'){
				printf("%d\n",query(1,n,1,x+1,y+1).len);
			}else{
				update(1,n,1,x+1,y);
			}
		}
	}
	return 0;
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值