Contest Hunter 4301 Can you answer on these queries III

记住建树的时候赋值的节点是cnt节点,查询的时候返回的也是cnt的值 因为没有区间修改所以不用Lazy标记。。。

记录下自己犯下的低级错误,。。

#include<bits/stdc++.h>
using namespace std;
#define lson cnt<<1
#define rson cnt<<1|1
const int maxn = 5e5+10;
struct node{
	int l,r,lmax,rmax,dat,sum;
}tree[maxn<<2];
void push_up(int cnt)
{
	tree[cnt].sum = tree[lson].sum + tree[rson].sum;
	tree[cnt].lmax = max(tree[lson].lmax,tree[lson].sum + tree[rson].lmax);
	tree[cnt].rmax = max(tree[rson].rmax,tree[lson].rmax + tree[rson].sum);
	tree[cnt].dat = max(tree[lson].dat,tree[rson].dat);
	tree[cnt].dat = max(tree[cnt].dat,tree[lson].rmax + tree[rson].lmax); 
}
void build(int cnt,int l,int r)
{
	tree[cnt].l = l;
	tree[cnt].r = r;
	if(l == r)
	{	
		scanf("%d",&tree[cnt].dat);
		tree[cnt].lmax = tree[cnt].dat;
		tree[cnt].rmax = tree[cnt].dat;
		tree[cnt].sum = tree[cnt].dat;
		return ;
	}
	int mid = (l+r) >>1;
	build(lson,l,mid);
	build(rson,mid+1,r);
	push_up(cnt);
}
void update(int cnt,int x,int val)
{
	int l = tree[cnt].l;
	int r = tree[cnt].r;
	if(l == r)
	{
		tree[cnt].dat = val;
		tree[cnt].lmax = tree[cnt].dat;
		tree[cnt].rmax = tree[cnt].dat;
		tree[cnt].sum = tree[cnt].dat;
		return ; 
	}
	int mid = (l+r) >>1;
	if(mid >= x)
		update(lson,x,val);
	else
		update(rson,x,val);
	push_up(cnt);
}
node query(int cnt,int L,int R)
{
	int l = tree[cnt].l;
	int r = tree[cnt].r;
	if(L <= l && R >= r )
	{
		return tree[cnt];
	}
	int mid = (l+r) >>1;
	if(mid >= R)
		return query(lson,L,R);
	else if(L > mid)
		return query(rson,L,R);
	else
	{
		node a = query(lson,L,mid);
		node b = query(rson,mid+1,R);
		node c;
		c.sum = a.sum + b.sum;
        c.lmax = max(a.lmax, a.sum + b.lmax);
        c.rmax = max(b.rmax, b.sum + a.rmax);
        c.dat = max(max(a.dat, b.dat), a.rmax + b.lmax);
        return c;
	}
		
}
int main()
{
	int n,m;
	cin>>n>>m;
	build(1,1,n);
	int op,x,y;
	for(int i=1;i<=m;i++)
	{
		cin>>op>>x>>y;
		if(op == 1)
		{
			if(x > y)
				swap(x,y);
			cout<<query(1,x,y).dat<<endl;
		}
		else     
			update(1,x,y);
	}  
	return 0;	
} 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值