N个数,求和,更新某个数,再求和
#include <iostream>
#include <stdio.h>
#include <string.h>
#define maxn 10000 + 10
using namespace std;
int a[maxn];
int c[maxn];
int n;
int lowbit(int x)
{
return x&(-x);
}
void update(int m,int d)
{
while(m<=n)
{
c[m] += d;
m+=lowbit(m);
}
}
int getsum( int m)
{
int ans = 0;
while( m > 0 )
{
ans += c[m];
m -= lowbit(m);
}
return ans;
}
int main()
{
int a,b;
while(scanf("%d",&n) != EOF)
{
memset(c,0,sizeof(c));
cin>>a>>b;
for( int i = 1; i <= n ; i++)
{
scanf("%d",&a[i]);
update(i,a[i]);
}
update(a,b);
cout<<getsum(n)<<endl;
}
return 0;
}