求调,输入有问题
查看原帖
求调,输入有问题
1071907
lizihangrq楼主2025/2/5 16:35
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<ctime>
#include<cstdlib>
#define LL long long
//#include<cmath>
using namespace std;
const int constant=1e5+5;
LL tree[constant],n,m,a[constant],tag[constant];
LL ll(LL x){return x<<1;}
LL rr(LL x){return x<<1|1;}
LL push_up(LL x){ return tree[ll(x)]+tree[rr((x))];}
void build(LL p,LL l,LL r){
	if(l==r){
		tree[p]=a[l];
		return ;
	}
	LL mid=l+r>>1;
	build(ll(p),l,mid);
	build(rr(p),mid+1,r);
	push_up(p);
}//构造线段树 
void push_down(LL p,LL l,LL r){
	LL mid=l+r>>1;
	tag[ll(p)]+=tag[p];
	tag[rr(p)]+=tag[p];
	tree[ll(p)]+=tag[p]*(mid-l+1);
	tree[rr(p)]+=tag[p]*(r-mid);
	tag[p]=0;
}//懒惰修改 
LL query(LL p,LL l,LL r,LL L,LL R){
	if(L<=l&&r<=R) return tree[p];
	LL mid=l+r>>1,sum=0;
	push_down(p,l,r);
	if(mid>=L) sum+=query(ll(p),l,mid,L,R);
	if(mid<R) sum+=query(rr(p),mid+1,r,L,R);
	return sum; 
}//实现区间查询 
void revise(LL p,LL l,LL r,LL L,LL R,LL k){
	if(l>=L&&r<=R){
		tag[p]+=k;
		tree[p]+=k*(r-l+1);
		return ;
	}
	LL mid=l+r>>1;
	push_down(p,l,r);
	if(mid>=l){
		revise(ll(p),l,mid,L,R,k);
	}
	if(mid<R){
		revise(rr(p),mid+1,r,L,R,k);
	}
	push_up(p);
}//区间修改 
int main(){
	scanf("%lld%lld",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i]);
	}
	build(1,1,n); 
	while(m--){
		LL op;
		scanf("%lld",&op);
		if(op==1){
			LL x,y,k;
			scanf("%lld%lld%lld",&x,&y,&k);
			revise(1,1,n,x,y,k);
		}
		else{
			LL x,y;
			scanf("%lld%lld",&x,&y);
			printf("%lld\n",query(1,1,n,x,y));
		}
	}
	return 0;
}
2025/2/5 16:35
加载中...