传送门: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();                  \
  cout.tie();
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;
;
const ll mod = 1000000007LL;
 << ;
ll dat[N];
int num;
int tree_node;
void init(int n)
{
    tree_node = ;
    while(tree_node < n)
        tree_node <<= ;
    _for(i, , tree_node *  -) dat[i] = ;
}
void update(int pos, int date)
{
    pos += tree_node - ;
    dat[pos] = date;
    )
    {
        pos >>= ;
        dat[pos] = dat[pos<<] + dat[pos<<|];
    }
}
int query(int rank, int k)
{
    )
        return k;
    ])
        );
    else
        ], k<<|);
}
void solve()
{
    ll x,p,q;
    char str;
    _for(i, , num)
    {
        scanf("%lld",&x);
        update(i,x);
    }
    scanf("%lld",&x);

    ; i <= x; i++)
    {
        getchar();
        scanf("%c",&str);
        if(str=='q')
        {
            scanf("%lld",&p);
            ])
            {
                printf("%d\n",num);
            }
            else
                printf() - tree_node + );
        }
        else
        {
            scanf("%lld%lld",&p,&q);
            update(p,q);
        }
    }
}
int main()
{

    while(scanf("%d",&num)!=EOF)
    {
        init(num);
        solve();
    }
    ;
}

ZOJ 3279-Ants(线段树)的更多相关文章

  1. ZOJ - 1610 经典线段树染色问题

    这个是一个经典线段树染色问题,不过题目给的是左右左右坐标,即[0,3]包含0-1这一段 1-2这一段 2-3这一段,和传统的染色不太一样,不过其实也不用太着急. 我们把左边的坐标+1,即可,那么[0, ...

  2. zoj 3349 dp + 线段树优化

    题目:给出一个序列,找出一个最长的子序列,相邻的两个数的差在d以内. /* 线段树优化dp dp[i]表示前i个数的最长为多少,则dp[i]=max(dp[j]+1) abs(a[i]-a[j])&l ...

  3. 线段树区间染色 ZOJ 1610

    Count the Colors ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...

  4. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  5. ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)

    ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...

  6. zoj 3511 Cake Robbery(线段树)

    problemCode=3511" target="_blank" style="">题目链接:zoj 3511 Cake Robbery 题目 ...

  7. ZOJ 1859 Matrix Searching(二维线段树)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1859 Matrix Searching Time Limit: 10 Seco ...

  8. zoj 1610 Count the Colors(线段树延迟更新)

    所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...

  9. 主席树[可持久化线段树](hdu 2665 Kth number、SP 10628 Count on a tree、ZOJ 2112 Dynamic Rankings、codeforces 813E Army Creation、codeforces960F:Pathwalks )

    在今天三黑(恶意评分刷上去的那种)两紫的智推中,突然出现了P3834 [模板]可持久化线段树 1(主席树)就突然有了不详的预感2333 果然...然后我gg了!被大佬虐了! hdu 2665 Kth ...

随机推荐

  1. 动态切换input的 disables 属性

    $("input[type='text']").each(function(){ if($(this).data('parent_id')){ var _each_this_par ...

  2. 洛谷 P2800 又上锁妖塔

    https://www.luogu.org/problem/show?pid=2800 题目背景 小D在X星买完了想要的东西,在飞往下一个目的地的途中,正无聊的他转头看了看身边的小A,发现小A正在玩& ...

  3. python实现堆栈、队列

    一.利用python列表实现堆栈和队列 堆栈: 堆栈是一个后进先出的数据结构,其工作方式就像生活中常见到的直梯,先进去的人肯定是最后出. 我们可以设置一个类,用列表来存放栈中的元素的信息,利用列表的a ...

  4. Robot Framework 自定义关键字 Ignore error

    以上是关键字的完整写法. 一下是调用该关键字的实例.

  5. 【BZOJ】2151 种树

    [算法]贪心+堆 [题意]n个数字的序列,要求选择互不相邻的k个数字使和最大. [题解] 贪心,就是按一定顺序选取即可最优,不会反悔. 考虑第一个数字选择权值最大的,那么它相邻的两个数字就不能选择,那 ...

  6. cocos2dx 某缩放的页面 CCTableView最后一个标签无法点中

    有一个二级界面,在ipad4下面放大到1.6倍,直接对最外层的CCLayer缩放的,里面包含有CCTableView.结果运行的时候无法选中到最后一个标签,无论总的标签是2个还是更多,单步调试,发现到 ...

  7. Frogs' Neighborhood(POJ1659+Havel-Hakimi定理)

    题目链接:http://poj.org/problem?id=1659 题目: 题意:根据他给你的每个点的度数构造一张无向图. 思路:自己WA了几发(好菜啊……)后看到discuss才知道这个要用Ha ...

  8. AndroidStudio创建jinLibs文件夹

    在文件中的buildTypes节点下添加 sourceSets.main {          jniLibs.srcDir 'libs'      } 如图

  9. Swift 特殊关键字 与符号

    #available() 函数来检查API函数的可用性 // 判断当前版本是否 iOS8.0+,OSX10.10+以及以其他平台 if #available(iOS 8.0, OSX 10.10, * ...

  10. iOS 真机调试报错汇总

    1. iphone is busy: processing symbol files 引起原因第一次运行真机, 会处理一些文件, 上面会有一个进度条给予显示 等100%之后再编译 2. xcode c ...