求助 线段树板子 自己弄的数据 输入 5 5 1 2 3 4 5 2 2 4 1 3 5 2 3 1 5 1 1 3 3 3 3 5 输出 9 7 8
但我输出老是 9 7 13 最后一行那个求区间[3,5]的最大值一直是错的不知道为啥 调了半个小时了啊啊还是不知道错哪了 求助求助救救孩子
(代码最后那个输出是调试用的%%%
#include<cstdio>
#include<bits/stdc++.h>
using namespace std;
const int N=100003;
const int oo=0x3f3f3f3f;
int n,T,x,y,z,jjj,add[N];
int sum[N],mx[N],a[N];
void pushup(int rt){
sum[rt]=sum[rt*2]+sum[rt*2+1];
mx[rt]=max(mx[rt*2],mx[rt*2+1]);
}
void build(int l,int r,int rt){
if(l==r){
sum[rt]=a[l];
mx[rt]=a[l];
return;
}
int m=(l+r)/2;
build(l,m,rt*2);
build(m+1,r,rt*2+1);
pushup(rt);
}
void pushdown(int rt,int ln,int rn){
if(add[rt]){
add[rt*2]+=add[rt];
add[rt*2+1]+=add[rt];
mx[rt*2]+=add[rt];
mx[rt*2+1]+=add[rt];
sum[rt*2]+=add[rt]*ln;
sum[rt*2+1]+=add[rt]*rn;
add[rt]=0;
}
}
void update(int L,int R,int c,int l,int r,int rt){
if(L<=l && r<=R){
sum[rt]+=c*(r-l+1);
add[rt]+=c;
mx[rt]+=c;
return ;
}
int m=(l+r)/2;
pushdown(rt,m-l+1,r-m);
if(L<=m) update(L,R,c,l,m,rt*2);
if(R>m) update(L,R,c,m+1,r,rt*2+1);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l && r<=R) return sum[rt];
int m=(l+r)/2;
pushdown(rt,m-l+1,r-m);
int ans=0;
if(L<=m) ans+=query(L,R,l,m,rt*2);
if(R>m) ans+=query(L,R,m+1,r,rt*2+1);
return ans; //pushup(rt);
}
int workmx(int L,int R,int l,int r,int rt){
if(L<=l && r<=R) return mx[rt];
int m=(l+r)/2;
pushdown(rt,m-l+1,r-m);
int ans=-oo;
if(L<=m) ans=max(ans,query(L,R,l,m,rt*2));
if(R>m) ans=max(ans,query(L,R,m+1,r,rt*2+1));
return ans; //pushup(rt);
}
int main(){
freopen("xds.in","r",stdin);
freopen("xds.out","w",stdout);
scanf("%d %d",&n,&T);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
build(1,n,1);
while(T--){
scanf("%d",&jjj);
if(jjj==1) {
scanf("%d %d %d",&x,&y,&z);
update(x,y,z,1,n,1);
}
if(jjj==2){
scanf("%d %d",&x,&y);
cout<<query(x,y,1,n,1)<<endl;
}
if(jjj==3){
scanf("%d %d",&x,&y);
cout<<workmx(x,y,1,n,1)<<endl;
}
}
for(int i=1;i<=n;i++){
cout<<query(i,i,1,n,1)<<' ';
}
return 0;
}