| 比赛 | 
    十一中校庆欢乐赛 | 
    评测结果 | 
    AAAAAAAAAA | 
    | 题目名称 | 
    求和问题 | 
    最终得分 | 
    100 | 
    | 用户昵称 | 
    高哥 | 
    运行时间 | 
    0.498 s  | 
    | 代码语言 | 
    C++ | 
    内存使用 | 
    0.39 MiB  | 
    | 提交时间 | 
    2014-10-23 18:33:12 | 
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#define N 10010
#define ll long long
using namespace std;
ll c[N],n;
int lowbit(int x)
{
	return x&-x;
}
void updata(int x,ll a)
{
	while(x<=n)
	{
		c[x]+=a;
		x+=lowbit(x);
	}
}
ll sum(int x)
{
	ll ans=0;
	while(x>0)
	{
		ans+=c[x];
		x-=lowbit(x);
	}
	return ans;
}
int main()
{
	freopen("sum.in","r",stdin);
	freopen("sum.out","w",stdout);
	ll a;
	scanf("%lld",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&a);
		updata(i,a);
	}
	int m;
	scanf("%d",&m);
	for(int i=1;i<=m;i++)
	{
		int a,b;
		scanf("%d%d",&a,&b);
		printf("%lld\n",sum(b)-sum(a-1));
	}
	return 0;
}