zoukankan      html  css  js  c++  java
  • ZOJ 3279 Ants

     

    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
    Author: Lin, Yue
    Source: ZOJ Monthly, December 2009



    /*
      ZOJ 3279
    */

    #include <iostream>
    #include <cstdio>
    #include <cstring>

    using namespace std;

    const int maxn=100000;

    int Tree[100010];
    int k[100010];
    int lowbit(int x)
    {
        return -x&x;
    }

    void add(int x,int val)
    {
        for(int i=x;i<=maxn;i+=lowbit(i))
            Tree+=val;
    }

    int get(int x)
    {
        int sum=0;
        for(int i=x;i;i-=lowbit(i))
            sum+=Tree;
        return sum;
    }
    int n,m;
    int main()
    {
    while(scanf("%d",&n)!=EOF)
    {
        memset(Tree,0,sizeof(Tree));

        for(int i=1;i<=n;i++)
        {
            scanf("%d",&k);
            add(i,k);
        }
        scanf("%d",&m);
        char ch[2];int a,b;
        while(m--)
        {
            scanf("%s",ch);
            if(ch[0]=='p')
            {
                scanf("%d%d",&a,&b);
                add(a,-k[a]);
                add(a,b);
                k[a]=b;
            }
            if(ch[0]=='q')
            {
                scanf("%d",&a);
                int high=n,low=1;
                int ans=n;
                while(low<=high)
                {
                   int mid=(high+low)/2;
                   if(get(mid)>=a)
                   {
                       high=mid-1;
                       ans=mid;
                   }
                   else
                   {
                       low=mid+1;
                   }
                }
                printf("%d ",ans);
            }
        }
    }

        return 0;
    }
    * This source code was highlighted by YcdoiT. ( style: Codeblocks )
  • 相关阅读:
    【spring】基于AspectJ的AOP
    【matlab】stanford线性回归,logistic regression 实验
    【Python】列表、字典和元组的排序
    PHP 二叉树的深度优先与广度优先遍历
    PHP 定义栈结构,实现min函数,获取栈最小元素,要求时间复杂度为O(1)
    PHP 短连接生成
    一条SQL查询访问记录表(visit_log)中某个类目(catalog_id)的访问量(visit)排前两名的记录行
    利用 p, 1p 随机数发生器知道等概率发生器
    PHP 将二叉查找树转换为双向链表,要求不能创建新节点,只能调节节点指针
    PHP 求最大递增子序列长度
  • 原文地址:https://www.cnblogs.com/CKboss/p/3350896.html
Copyright © 2011-2022 走看看