View Code of Problem 3939

#include<bits/stdc++.h>
using namespace std;
long long c[100005];
int n;
int lowbit(int x){
	return x&-x;
}
void update(int x,long long d){
	while(x<=n)c[x]+=d,x+=lowbit(x); 
}
int getsum(int x){
	int ret=0;
	while(x)ret+=c[x],x-=lowbit(x);
	return ret;
}
int main(void)
{
	int m;
	cin>>n>>m;
	for(int i=1;i<=n;++i){
		int x;
		scanf("%d",&x);
		update(i,x);
	}
	for(int i=1;i<=m;++i){
		int y,z;
		scanf("%d %d",&y,&z);
		printf("%d\n",getsum(z)-getsum(y-1));
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 3939