记住建树的时候赋值的节点是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;
}