zhao_ry514114
赵若伊

导航

 
#include<bits/stdc++.h>
using namespace std;
int n,q;
const int N=1e6+10;
int a[N];
int tot;
struct node{
	long long sum,lazy;
	int ls,rs;
}tree[4*N];
void pushdown(int p,int l,int r){
	int mid=(l+r)>>1;
	if(!tree[p].ls)tree[p].ls=++tot;
	if(!tree[p].rs)tree[p].rs=++tot;
	tree[tree[p].ls].sum+=1LL*(mid-l+1)*tree[p].lazy;
	tree[tree[p].rs].sum+=1LL*(r-mid)*tree[p].lazy;
	tree[tree[p].ls].lazy+=tree[p].lazy;
	tree[tree[p].rs].lazy+=tree[p].lazy;
	tree[p].lazy=0;
}
void modify(int p,int l,int r,int ql,int qr,long long val){
	if(ql<=l&&r<=qr){
		tree[p].sum+=(r-l+1)*val;
		tree[p].lazy+=val;
		return;
	}
	pushdown(p,l,r);
	int mid=(l+r)>>1;
	if(ql<=mid)modify(tree[p].ls,l,mid,ql,qr,val);
	if(qr>mid)modify(tree[p].rs,mid+1,r,ql,qr,val);
	tree[p].sum=tree[tree[p].ls].sum+tree[tree[p].rs].sum;
}
long long query(int p,int l,int r,int ql,int qr){
	if(!p)return 0;
	if(ql<=l&&r<=qr){
		return tree[p].sum;
	}
	pushdown(p,l,r);
	int mid=(l+r)>>1;
	long long ans=0;
	if(ql<=mid)ans+=query(tree[p].ls,l,mid,ql,qr);
	if(qr>mid)ans+=query(tree[p].rs,mid+1,r,ql,qr);
	return ans;
}
int main(){
	cin>>n>>q;
	int root=1;
	tot=1;
	modify(1,1,n,1,n,0);
	while(q--){
		int op;
		cin>>op;
		if(op==1){
			int l,r;
			long long x;
			cin>>l>>r>>x;
			modify(1,1,n,l,r,x);
		}
		else{
			int l,r;
			cin>>l>>r;
			cout<<query(1,1,n,l,r)<<'\n';
		}
	}
	return 0;
}
posted on 2025-08-25 21:38  zhao_ry514114  阅读(18)  评论(0)    收藏  举报