树状数组模板程序

#include<iostream>
#include<cstdio>
using namespace std;
int l;
int c[1000];
int lowbit(int x){
	return x & (x ^ (x-1));
}
void up(int x,int y){
	while(x<=l){
		c[x]+=y;
		x+=lowbit(x);
	}
}
int cx(int x){
	int sum=0;
	int k=x;
	while(k>0){
		sum+=c[k];
		k-=lowbit(k);
	}
	return sum;
}
int main(){
	int i,j,k,m,n;
	int x,y;	
	cin>>n>>m>>l;	
	for(i=1;i<=n;i++){
		cin>>x>>y;
		up(x,y);		
	}
	for(i=1;i<=m;i++){
		cin>>x>>y;
		cout<<cx(y)-cx(x-1)<<endl;
	}
	return 0;
}

你可能感兴趣的:(树状数组模板程序)