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;
    }
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    二、缴费证明(完税凭证)开具渠道
    DateTime的具体用法
    获取HTML页面高度和分辨率
    JS获取当前日期及时间
    CSS textindent 属性
    combotree初始化加载折叠
    关于C#中将数字转换为指定格式
    jquery cookie用法(获取cookie值,删除cookie)
    如何在Sql Server中读取最近一段时间的记录,比如取最近3天的或最近3个月的记录。
    定位/定位偏移量
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8886964.html
Copyright © 2011-2022 走看看