1. 程式人生 > >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

分析:每次更新的時候都遞迴到端點進行更新

當查詢排名的 時候,左邊的值大於等於所查排名就向左邊搜,否則則向右邊搜尋,直到搜到葉子節點為止。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=100000;
int sum=0;
int tr[4*N];
void PushUp(int i)
{
    tr[i]=tr[2*i]+tr[2*i+1];
}
void build(int i,int l,int r)
{
    if(l==r)
    {
        scanf("%d",&tr[i]);
        return;
    }
    int mid=(l+r)/2;
    build(2*i,l,mid);
    build(2*i+1,mid+1,r);
    PushUp(i);
}
void update(int i,int l,int r,int x,int c)
{
    if(l==r&&l==x)
    {
        tr[i]=c;
        return;
    }
    int mid=(l+r)/2;
    if(x<=mid) update(2*i,l,mid,x,c);
    else update(2*i+1,mid+1,r,x,c);
    PushUp(i);
}
int query(int i,int l,int r,int x)
{
    int c;
    if(l==r)
    {
        return l;
    }
    int mid=(l+r)/2;
    if(tr[i<<1]>=x) c=query(2*i,l,mid,x);
    else c=query(2*i+1,mid+1,r,x-tr[i<<1]);
    return c;
}
int main()
{
    int n,k,x,c;
    while(~scanf("%d",&n))
    {
        build(1,1,n);
        scanf("%d",&k);
        for(int i=1;i<=k;i++)
        {
            char a[5];
            scanf("%s",a);
            if(a[0]=='q')
            {
                scanf("%d",&x);
                printf("%d\n",query(1,1,n,x));
            }
            else
            {
                scanf("%d%d",&x,&c);
                update(1,1,n,x,c);
            }
        }
    }
    return 0;
}


相關推薦

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

ZOJ 3279 Ants線段更新和查詢)

比較基礎的線段樹了。。 AC程式碼: /* *********************************************** Author :yzkAccepted Cre

hdu1394(枚舉/狀數組/線段更新&區間求和)

splay nbsp one 包括 一個 hid closed 當前 初始 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 題意:給出一個循環數組,求其逆序對最少為多少; 思路:對於逆序對: 交換兩個相鄰數,逆

hdu2795(線段更新&區間最值)

ref 當前 廣告 sed gif .cn lap ostream spa 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=2795 題意:有一個 h * w 的板子,要在上面貼 n 條 1 * x 的廣告,在貼第 i 條廣

CodeForces 19D Points(離散化+線段+更新

cond clu ref console padding top ostream name consola 題目鏈接: huangjing 題意:給了三種操作 1:add(x,y)將這個點增加二維坐標系 2:remove(x,y)將這個點從二維坐標系移除。 3:fin

HDU 2795 線段更新

miss imp ember name specific ssi chan order start Billboard Time Limit: 20000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/

POJ 3468 A Simple Problem with Integers(線段 更新+區間求和 )

names || log shu 更新 can pro struct sim 題目鏈接:http://poj.org/problem?id=3468 題意:單點更新,區間求和。 題解:裸題 1 //POJ 3468 A Simple Problem with

Buy Tickets(線段更新,逆向思維)

fin eve lang sum free orz cst ssi -s 題目大意:有n個的排隊,每一個人都有一個val來對應,每一個後來人都會插入當前隊伍的某一個位置pos。要求把隊伍最後的狀態輸出。 個人心得:哈哈,用鏈表寫了下,果不其然超時了,後面轉念一想要用靜態數組

FZU 2297 Number theory【線段/更新/思維】

list algo divide val 復雜 ger wing scrip ive Given a integers x = 1, you have to apply Q (Q ≤ 100000) operations: Multiply, Divide. Input F

洛谷 P3919 【模板】可持久化數組(可持久化線段/平衡)-可持久化線段(更新查詢)

map sse 依次 數據規模 操作 str tps () 發現 P3919 【模板】可持久化數組(可持久化線段樹/平衡樹) 題目背景 UPDATE : 最後一個點時間空間已經放大 標題即題意 有了可持久化數組,便可以實現很多衍生的可持久化功能(例如:可持久化並查集

線段更新+離線

cf19d 離散化+set #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <cmath> #include &l

線段更新區間查詢

敵兵佈陣 線段樹單點更新區間查詢 C國的死對頭A國這段時間正在進行軍事演習,所以C國間諜頭子Derek和他手下Tidy又開始忙乎了。A國在海岸線沿直線佈置了N個工兵營地,Derek和Tidy的任務就是要監視這些工兵營地的活動情況。由於採取了某種先進的監測手段,所以每個工兵營地的人數C

洛谷 P3919 【模板】可持久化陣列(可持久化線段/平衡)-可持久化線段(更新查詢)

P3919 【模板】可持久化陣列(可持久化線段樹/平衡樹) 題目背景 UPDATE : 最後一個點時間空間已經放大 標題即題意 有了可持久化陣列,便可以實現很多衍生的可持久化功能(例如:可持久化並查集) 題目描述 如題,你需要維護這樣的一個長度為 NN 的陣列,支援如下幾種操作

hihoCoder week19 RMQ問題再臨-線段 更新 區間查詢

單點更新 區間查詢   #include <bits/stdc++.h> using namespace std; #define m ((l+r)/2) #define ls (rt<<1) #define rs (rt<<1|1) cons

【HDU】1394Minimum Inversion Number-(線段更新,求出逆序數)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25122 &n

線段--更新模板題

線段樹應該是資料結構中的一種吧,說白了,他就是一種工具,只要你學會了他,那麼你就可以在以後的學習中去用它。 他的大致用法,就是把一個一維陣列改變成一個樹的結構,而且這個樹還是一個完全二叉樹;  上述圖就是把一個1-8的區間變成了一個二叉樹的結構,為什麼我們要這樣幹

hdu1754 線段更新模板

程式碼: #include<iostream> #include<cstdio> #include<cstring> #include<algorithm&g

HDOJ 題目3074 Multiply game(線段更新,區間求乘積)

Multiply game Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2109    Accepted S

杭電 3308 LCIS (線段+更新+區間求和)

Given n integers.  You have two operations:  U A B: replace the Ath number by B. (index counting from 0)  Q A B: output the length of the