树状数组的区间更新 ,区间查询

http://codevs.cn/problem/1082/

#include 
#include 
#include 
using namespace std;
const int maxn = 1e5+1;
int C[maxn];
int C2[maxn];
int p[maxn];
int n;
int lowbit(int x){
	return (-x)&x;
}
void update(int i,int num){
	while(i <= n){
		C[i]+=num;
		i+=lowbit(i);
	}
}
int sigma(int c[maxn],int i){
	int s = 0;
	for(in i = n; i > 0 ; i-=lowbit(i))	
		s+=c[i];
	return s;
}
int main(){
	memset(C,0,sizeof C);
	memset(p,0,sizeof p);
	cin>>n;
	for(int i = 1; i <= n;i++ )
	{	
	 
		cin>>p[i];
		update(i,p[i]-p[i-1]);
	}
	int w;
	cin>>w;
	if(w == 1)
	{
		int a,b;
		int x;
		cin>>a>>b>>x;
		update(a,x);
		update(b+1,-x);	
	}
	else{
		int a,b;
		cin>>a>>b;
		for(int i = 1; i <= n;i++)
			C2[i]=C[i]*(i-1);
		a--;
		int sum1 = b*sigma(C,b)-sigma(C2,b);
		int sum2 = a*sigma(C,a)-sigma(C,a);
		cout<

你可能感兴趣的:(树状数组的区间更新 ,区间查询)