ZOJ3279 Ants 树状数组

有1到n 那个点 每一个点有a[i]只蚂蚁
两种操作 

p a b 把第a个点的蚂蚁数量改成b

q  a  查询第a只蚂蚁在哪里

简单的树状数组+二分就可以了


Ants

Time Limit: 2 Seconds       Memory Limit: 32768 KB

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;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值