rt,线段树只得30pts,求大佬查错
代码如下
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e5+10;
int n,q,l,r,c,a[maxn];
char opt;
struct tree{
int l,r,sum,res,add;
}tr[maxn<<2];
void build(int p,int l,int r){
tr[p].l=l,tr[p].r=r;
if(l==r){
tr[p].sum=tr[p].res=a[l];
return;
}
int mid=l+r>>1;
build(p<<1,l,mid),build(p<<1|1,mid+1,r);
tr[p].sum=tr[p<<1].sum+tr[p<<1|1].sum;
tr[p].res=min(tr[p<<1].res,tr[p<<1|1].res);
}
void spread(int p){
if(tr[p].add){
tr[p<<1].sum+=tr[p].add*(tr[p<<1].r-tr[p<<1].l+1);
tr[p<<1|1].sum+=tr[p].add*(tr[p<<1|1].r-tr[p<<1|1].l+1);
tr[p<<1].res+=tr[p].add;
tr[p<<1|1].res+=tr[p].add;
tr[p<<1].add+=tr[p].add;
tr[p<<1|1].add+=tr[p].add;
tr[p].add=0;
}
}
void change(int p,int l,int r,int v){
if(l<=tr[p].l&&tr[p].r<=r){
tr[p].sum+=v*(tr[p].r-tr[p].l+1);
tr[p].res+=v;
tr[p].add+=v;
return;
}
spread(p);
int mid=tr[p].l+tr[p].r>>1;
if(mid>=l)
change(p<<1,l,r,v);
if(mid<r)
change(p<<1|1,l,r,v);
tr[p].sum=tr[p<<1].sum+tr[p<<1|1].sum;
tr[p].res=min(tr[p<<1].res,tr[p<<1|1].res);
}
int query(int p,int l,int r){
if(l<=tr[p].l&&tr[p].r<=r)
return tr[p].sum;
spread(p);
int mid=tr[p].l+tr[p].r>>1,ans=0;
if(mid>=l)
ans+=query(p<<1,l,r);
if(mid<r)
ans+=query(p<<1|1,l,r);
return ans;
}
int mintree(int p,int l,int r){
if(l<=tr[p].l&&tr[p].r<=r)
return tr[p].res;
spread(p);
int mid=tr[p].l+tr[p].r>>1,ans=INT_MAX;
if(mid>=l)
ans=min(ans,mintree(p<<1,l,r));
if(mid<r)
ans=min(ans,mintree(p<<1|1,l,r));
return ans;
}
signed main(){
ios::sync_with_stdio(0),cin.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,1,n);
while(q--){
cin>>opt;
if(opt=='P'){
cin>>l>>r>>c;
change(1,l,r,c);
}
if(opt=='S'){
cin>>l>>r;
cout<<query(1,l,r)<<endl;
}
if(opt=='M'){
cin>>l>>r;
cout<<mintree(1,l,r)<<endl;
}
}
return 0;
}