rt
关于更新的时候可以把val直接+lazy然后下推?
这里是直接把所有的值+到lazy
应该没有什么问题,有问题再改
#include<cstdio>
#include<algorithm>
#include<vector>
#include<map>
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f
//ABCUVBA
struct node{
int l,r,val,lazy;
}tree[maxn*4];
int a[maxn],add[maxn];
void pushup(int x){
tree[x].val=max(tree[x<<1].val+tree[x<<1].lazy,tree[x<<1|1].val+tree[x<<1|1].lazy);
return;
}
void pushdown(int root){
if(tree[root].lazy){
tree[root<<1].lazy+=tree[root].lazy;
tree[root<<1|1].lazy+=tree[root].lazy;
tree[root].lazy=0;
}
}
void build(int root,int l,int r){
tree[root].l=l;
tree[root].r=r;
tree[root].lazy=0;
if(tree[root].l==tree[root].r){
tree[root].val=a[l];
return;
}
int mid=(l+r)/2;
build(root<<1,l,mid);
build(root<<1|1,mid+1,r);
pushup(root);
}
void update(int root,int tl,int tr,int c){ //区间(tl,tr)修改+c
if(tl<=tree[root].l&&tree[root].r<=tr){
//tree[root].val+=c;
tree[root].lazy+=c;
return;
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)/2;
if(tl<=mid)update(root<<1,tl,tr,c);
if(tr>mid)update(root<<1|1,tl,tr,c);
pushup(root);
}
int query(int root,int tl,int tr){
if(tl<=tree[root].l&&tree[root].r<=tr){
return tree[root].val+tree[root].lazy;
}
pushdown(root);
int mid=(tree[root].l+tree[root].r)/2;
int res=-1;
if(tl<=mid){
res=max(res,query(root<<1,tl,tr));
}
if(tr>mid){
res=max(res,query(root<<1|1,tl,tr));
}
return res;
}
int main(){
int n,m;
//printf("%d\n",max(1,5));
//printf("%d\n",max(5,1));
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
build(1,1,n);
scanf("%d",&m);
for(int i=1;i<=m;i++){
int x,l,r,c;
scanf("%d",&x);
if(x==1){
scanf("%d%d%d",&l,&r,&c);
update(1,l,r,c);
}else{
scanf("%d%d",&l,&r);
printf("%d\n",query(1,l,r));
}
}
return 0;
}
/*
10
1 2 3 4 5 6 7 8 9 10
5
1 1 3 5 (6 7 8 4 5 6 7 8 9 10)
2 1 8
2 1 10
1 1 1 4 (10 7 8 4 5 6 7 8 9 10)
2 1 10
*/