zoukankan      html  css  js  c++  java
  • ZOJ 3279-Ants(线段树)

    传送门: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

     

    题意:

    给你每个等级蚂蚁的数量 有两种操作:

    (1)rank x的蚂蚁的等级是多少

    (2)将等级为a的数量更改为b

    题解:

    还是一个简单的线段树,处理区间要仔细(调了一晚上bug-.-)

    代码:

    #include <stdio.h>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <deque>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <queue>
    #include <set>
    #include <utility>
    #include <vector>
    #define mem(arr, num) memset(arr, 0, sizeof(arr))
    #define _for(i, a, b) for (int i = a; i <= b; i++)
    #define __for(i, a, b) for (int i = a; i >= b; i--)
    #define IO                     
      ios::sync_with_stdio(false); 
      cin.tie(0);                  
      cout.tie(0);
    using namespace std;
    typedef long long ll;
    const ll inf = 0x3f3f3f3f;
    const double EPS = 1e-10;
    const ll mod = 1000000007LL;
    const int N = 1 << 19;
    ll dat[N];
    int num;
    int tree_node;
    void init(int n)
    {
        tree_node = 1;
        while(tree_node < n)
            tree_node <<= 1;
        _for(i, 1, tree_node * 2 -1) dat[i] = 0;
    }
    void update(int pos, int date)
    {
        pos += tree_node - 1;
        dat[pos] = date;
        while(pos > 1)
        {
            pos >>= 1;
            dat[pos] = dat[pos<<1] + dat[pos<<1|1];
        }
    }
    int query(int rank, int k)
    {
        if(k > tree_node - 1)
            return k;
        if(rank <= dat[k<<1])
            return query(rank, k<<1);
        else
            return query(rank - dat[k<<1], k<<1|1);
    }
    void solve()
    {
        ll x,p,q;
        char str;
        _for(i, 1, num)
        {
            scanf("%lld",&x);
            update(i,x);
        }
        scanf("%lld",&x);
    
        for(int i = 1; i <= x; i++)
        {
            getchar();
            scanf("%c",&str);
            if(str=='q')
            {
                scanf("%lld",&p);
                if(p > dat[1])
                {
                    printf("%d
    ",num);
                }
                else
                    printf("%d
    ",query(p,1) - tree_node + 1);
            }
            else
            {
                scanf("%lld%lld",&p,&q);
                update(p,q);
            }
        }
    }
    int main()
    {
    
        while(scanf("%d",&num)!=EOF)
        {
            init(num);
            solve();
        }
        return 0;
    }
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    互联网预言大神:凯文·凯利——预测未来的12个趋势
    仅有算法远远不够:AI突破下一站,需要对硬件重新审视
    软件开发中经常使用的5种设计模式
    强化学习到底是什么,它如何运作?
    2020年网络安全的六大经验教训
    选择困难终结者:不同问题之下的机器学习算法
    为什么物联网如此重要?
    Pku3080 Blue Jeans
    pku1734 Musical Theme
    [Usaco2006 Dec]Milk Patterns
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8886964.html
Copyright © 2011-2022 走看看