70分求调
查看原帖
70分求调
794062
zhazesheng楼主2024/8/12 12:27

8,9,10WA 刚学线段树的蒟蒻求调

#include<bits/stdc++.h>
using namespace std;
#define N 1000005
#define ls (p<<1)
#define rs (p<<1 | 1)
#define mid ((l+r)>>1) 
#define pushup(p) sum[p]=sum[ls]+sum[rs]
#define int long long int
int sum[N<<2],n,m,a[N],lazy[N<<2];
int read(){
   int fl=1,x=0;
   char ch=getchar();
   while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
   if(ch=='-') fl=-1,ch=getchar();
   while(ch<='9'&&ch>='0') 
 		x=x*10+ch-'0',ch=getchar();
   x=x*fl;
   return x;
}
void pushdown(int l,int r,int p){
   if(!lazy[p]||l==r) return ;
   sum[ls]+=(mid-l+1)*lazy[p];
   lazy[ls]+=lazy[p];
   sum[rs]+=(r-mid)*lazy[p];
   lazy[rs]+=lazy[p];
   lazy[p]=0;
   return ;
}
void build(int l,int r,int p){
   if(l==r){	
   	sum[p]=a[l];
   	return ;
   }
   build(l,mid,ls);
   build(mid+1,r,rs);
   pushup(p);
   return ;
}
void change(int l,int r,int p,int x,int v){
   if(l==r){
   	sum[p]+=v;
   	return ;
   }
   if(x<=mid) change(l,mid,ls,x,v);
   else change(mid+1,r,rs,x,v);
   pushup(p);
   return ;
}
int get(int l,int r,int p,int ql,int qr){
   pushdown(l,r,p);
   if(ql<=l&&qr>=r){
   	return sum[p];
   }
   int ret=0;
   if(ql<=mid)
   	ret+=get(l,mid,ls,ql,qr);
   if(qr>mid)
   	ret+=get(mid+1,r,rs,ql,qr);
   return ret;
}
void update(int l,int r,int p,int ql,int qr,int detla){
   if(ql<=l&&qr>=r){
   	sum[p]+=(r-l+1)*detla;
   	lazy[p]+=detla;
   	return ;
   }
   pushdown(l,r,p);
   if(ql<=mid)
   	update(l,mid,ls,ql,qr,detla);
   if(qr>mid)
   	update(mid+1,r,rs,ql,qr,detla);
   pushup(p);
   return ;
}
signed main(){
   n=read();m=read();
   for(int i=1;i<=n;i++) a[i]=read();
   build(1,n,1);
   int op,a,b,c;
   for(int i=1;i<=m;i++){
   	op=read();
   	if(op==1){
   		a=read(),b=read(),c=read();
   		update(1,n,1,a,b,c);
   	}
   	else{
   		a=read(),b=read();
   		printf("%d\n",get(1,n,1,a,b));
   	}
   }
   return 0;
}
2024/8/12 12:27
加载中...