树状数组(区间修改&&区间查询)

#include<bits/stdc++.h>	
#define int long long
using namespace std;
int n,m,x,x1,y,z;
int a[100010],d[100010],c[100010];
int lowbit(int num){return num&-num;}
void add(int x,int y){
	int a=x;
	while(x<=n) d[x]+=y,c[x]+=(a-1)*y,x+=lowbit(x);
	return;
}
int get(int x){
	int ans=0,ans1=0,a=x;
	while(x) ans+=d[x],ans1+=c[x],x-=lowbit(x);
	return ans*a-ans1;
}
signed main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i) scanf("%ld",&a[i]),add(i,a[i]-a[i-1]);
	for(int i=1;i<=m;++i){
		scanf("%ld",&x);
		if(x==1){
			scanf("%ld%ld%ld",&x1,&y,&z);
			add(x1,z);
			add(y+1,-z);
		}else{
			scanf("%ld%ld",&y,&z);
			cout<<get(z)-get(y-1)<<endl;
		}
	}
	return 0;
}
posted @ 2024-01-26 17:08  Arthur_Douglas  阅读(8)  评论(0编辑  收藏  举报