最后一个测试点RE了,求检查:
#include <bits/stdc++.h>
#define maxn 100005
#define int long long
using namespace std;
int n,m;
int a[maxn];
struct Node{
int l,r,max_val,add,set;
bool has_set;
}tree[4*maxn];
void push_up(int p){
tree[p].max_val=max(tree[2*p].max_val,tree[2*p+1].max_val);
}
void push_down(int p){
if(tree[p].has_set){
tree[2*p].max_val=tree[p].set;
tree[2*p].set=tree[p].set;
tree[2*p].has_set=true;
tree[2*p].add=0;
tree[2*p+1].max_val=tree[p].set;
tree[2*p+1].set=tree[p].set;
tree[2*p+1].has_set=true;
tree[2*p+1].add=0;
tree[p].has_set=false;
}
if(tree[p].add!=0){
tree[2*p].max_val+=tree[p].add;
tree[2*p].add+=tree[p].add;
tree[2*p+1].max_val+=tree[p].add;
tree[2*p+1].add+=tree[p].add;
tree[p].add=0;
}
}
void build(int p,int l,int r){
tree[p].l=l;
tree[p].r=r;
tree[p].add=0;
tree[p].has_set=false;
if(l==r){
tree[p].max_val=a[l];
return;
}
int mid=(l+r)/2;
build(2*p,l,mid);
build(2*p+1,mid+1,r);
push_up(p);
}
void update_set(int p,int l,int r,int val){
if(tree[p].r<l||tree[p].l>r)return;
if(l<=tree[p].l&&tree[p].r<=r) {
tree[p].max_val=val;
tree[p].set=val;
tree[p].has_set=true;
tree[p].add=0;
return;
}
push_down(p);
update_set(2*p,l,r,val);
update_set(2*p+1,l,r,val);
push_up(p);
}
void update_add(int p,int l,int r,int val){
if(tree[p].r<l||tree[p].l>r)return;
if(l<=tree[p].l&&tree[p].r<=r){
tree[p].max_val+=val;
tree[p].add+=val;
return;
}
push_down(p);
update_add(2*p,l,r,val);
update_add(2*p+1,l,r,val);
push_up(p);
}
int query_max(int p,int l,int r){
if(tree[p].r<l||tree[p].l>r)return -1e18;
if(l<=tree[p].l&&tree[p].r<= r){
return tree[p].max_val;
}
push_down(p);
return max(query_max(2*p,l,r),query_max(2*p+1,l,r));
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i];
}
build(1,1,n);
while(m--){
int op;
cin>>op;
if(op==1){
int l,r,x;
cin>>l>>r>>x;
update_set(1,l,r,x);
}else if(op==2){
int l,r,x;
cin>>l>>r>>x;
update_add(1,l,r,x);
}else if(op==3){
int l,r;
cin>>l>>r;
cout<<query_max(1,l,r)<<endl;
}
}
return 0;
}