http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3279
数状数组
#include <stdio.h>
#include <string.h>
#define lowbit(x) ((x)&(-x))
int n;
int a[100005],b[100005];
void update(int x,int v)
{
while(x <= n)
{
a[x] += v;
x += lowbit(x);
}
}
int sum(int x)
{
int ret = 0;
while(x)
{
ret += a[x];
x -= lowbit(x);
}
return ret;
}
int main()
{
int i,m,x,y;
char ch[2];
while(scanf("%d",&n) ==1)
{
memset(a,0,sizeof(a));
for(i = 1;i <= n;i ++)
{
scanf("%d",&b[i]);
update(i,b[i]);
}
scanf("%d",&m);
while(m --)
{
scanf("%s",ch);
if(ch[0] == 'p')
{
scanf("%d %d",&x,&y);
update(x,y-b[x]);
b[x] = y;
}
else
{
scanf("%d",&x);
int l=1;
int r=n;
int h;
while(l<=r)
{
h=(l+r)/2;
if(x>sum(h-1)&&x<=sum(h)) break;
if(x<=sum(h-1)) {r=h-1;continue;}
if(x>sum(h)) l=h+1;
}
printf("%d\n",h);
}
}
}
return 0;
}