有1到n 那个点 每一个点有a[i]只蚂蚁
两种操作
p a b 把第a个点的蚂蚁数量改成b
q a 查询第a只蚂蚁在哪里
简单的树状数组+二分就可以了
echo is a curious and clever girl, and she is addicted to the ants recently.
She knows that the ants are divided into many levels depends on ability, also, she finds the number of each level will change.
Now, she will give two kinds of operations as follow :
First, "p a b", the number of ants in level a change to b.
Second, "q x", it means if the ant's ability is rank xth in all ants, what level will it in?
Input
There are multi-cases, and you should use EOF to check whether it is in the end of the input. The first line is an integer n, means the number of level. (1 <= n <= 100000). The second line follows n integers, the ith integer means the number in level i. The third line is an integer k, means the total number of operations. Then following k lines, each line will be "p a b" or "q x", and 1 <= x <= total ants, 1 <= a <= n, 0 <= b. What's more, the total number of ants won't exceed 2000000000 in any time.
Output
Output each query in order, one query each line.
Sample Input
3 1 2 3 3 q 2 p 1 2 q 2
Sample Output
2 1
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define MAXN 110000
int n,m;
int c[MAXN];
int a[MAXN];
int lowbit(int t)
{
return t&(-t);
}
int sum(int i)
{
int s=0;
while(i>0)
{
s+=c[i];
i-=lowbit(i);
}
return s;
}
void modify(int i,int x)
{
while(i<=n)
{
c[i]+=x;
i+=lowbit(i);
}
}
int query(int x)
{
int l=1,r=n,m;
while(l<r)
{
m=(l+r)>>1;
if(sum(m)<x)
l=m+1;
else
r=m;
}
return r;
}
int main()
{
while(~scanf("%d",&n))
{
memset(c,0,sizeof(c));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
modify(i,a[i]);
}
scanf("%d",&m);
char task[3];
int x,y;
for(int i=1;i<=m;i++)
{
scanf("%s",task);
if(task[0]=='p')
{
scanf("%d%d",&x,&y);
modify(x,-a[x]);
modify(x,y);
a[x]=y;
}
else
{
scanf("%d",&x);
printf("%d\n",query(x));
}
}
}
return 0;
}