1. 程式人生 > >HDU 4027 Can you answer these queries?(線段樹單點更新)

HDU 4027 Can you answer these queries?(線段樹單點更新)

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 13517    Accepted Submission(s): 3081


Problem Description A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to eliminate the battleships. Each of the battleships can be marked a value of endurance. For every attack of our secret weapon, it could decrease the endurance of a consecutive part of battleships by make their endurance to the square root of it original value of endurance. During the series of attack of our secret weapon, the commander wants to evaluate the effect of the weapon, so he asks you for help.
You are asked to answer the queries that the sum of the endurance of a consecutive part of the battleship line.

Notice that the square root operation should be rounded down to integer.

Input The input contains several test cases, terminated by EOF.
  For each test case, the first line contains a single integer N, denoting there are N battleships of evil in a line. (1 <= N <= 100000)
  The second line contains N integers Ei, indicating the endurance value of each battleship from the beginning of the line to the end. You can assume that the sum of all endurance value is less than 263
.
  The next line contains an integer M, denoting the number of actions and queries. (1 <= M <= 100000)
  For the following M lines, each line contains three integers T, X and Y. The T=0 denoting the action of the secret weapon, which will decrease the endurance value of the battleships between the X-th and Y-th battleship, inclusive. The T=1 denoting the query of the commander which ask for the sum of the endurance value of the battleship between X-th and Y-th, inclusive.

Output For each test case, print the case number at the first line. Then print one line for each query. And remember follow a blank line after each test case.
Sample Input 10 1 2 3 4 5 6 7 8 9 10 5 0 1 10 1 1 10 1 1 5 0 5 8 1 4 8
Sample Output Case #1: 19 7 6
Source //真TMD無語 //又寫一道 單點更新的線段樹 //本來 以為 Soeasy TMD 寫出來又 TEL // 用延遲標記了啊 // 原來這道題 就是 開根號到 1 就不需要在 update下去了
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <math.h>
using namespace std;

const int L = 100000+10;
__int64 sum[L<<2],cnt[L<<2];

void add(int i)
{
    sum[i] = sum[2*i]+sum[2*i+1];
    cnt[i] = cnt[2*i]&&cnt[2*i+1];
}

void init(int l,int r,int i)
{
    if(l == r)
    {
        scanf("%I64d",&sum[i]);
        return;
    }
    int mid = (l+r)>>1;
    init(l,mid,2*i);
    init(mid+1,r,2*i+1);
    add(i);
}

void insert(int L,int R,int l,int r,int i)
{
    if(l == r)
    {
        sum[i] = sqrt(sum[i]);//向下取整
        if(sum[i]<=1)//如果為0,則標記不能再訪問了
            cnt[i] = 1;
        return ;
    }
    int mid = (l+r)>>1;
    if(L<=mid && !cnt[2*i])//孩子已經破壞了則不需再訪問
        insert(L,R,l,mid,2*i);
    if(R>mid && !cnt[2*i+1])
        insert(L,R,mid+1,r,2*i+1);
    add(i);
}

__int64 query(int L,int R,int l,int r,int i)//查詢區間和
{
    if(L<=l && r<=R)
        return sum[i];
    int mid = (l+r)>>1;
    __int64 ans = 0;
    if(L<=mid)
        ans+=query(L,R,l,mid,2*i);
    if(R>mid)
        ans+=query(L,R,mid+1,r,2*i+1);
    return ans;
}

int main()
{
    int n,m,i,j,x,l,r,cas = 1,flag;
    while(~scanf("%d",&n))
    {
        memset(sum,0,sizeof(sum));
        memset(cnt,0,sizeof(cnt));
        init(1,n,1);
        printf("Case #%d:\n",cas++);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&flag,&l,&r);
            if(l>r)//這裡要注意
                swap(l,r);
            if(flag)
                printf("%I64d\n",query(l,r,1,n,1));
            else
                insert(l,r,1,n,1);
        }
        printf("\n");
    }

    return 0;
}


相關推薦

HDU 4027 Can you answer these queries?(線段更新)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 13

HDU 4027 Can you answer these queries?(線段 區間不等更新)

題意  輸入n個數  然後有兩種操作   輸入0時將給定區間所有數都變為自己的開方   輸入1輸出給定區間所有數的和 雖然是區間更新  但每個點更新的不一樣  因此只能對單點進行更新  其實一個點最多被更新7次  2^64開平方7次後就變為1了  如果某個區間的數都變為了1

HDU 4027 Can you answer these queries? [線段]

題意:給你長度為n的數列,m個操作,有兩種操作型別: ①區間[L,R]每個數開根號 ②詢問區間[L,R]的和 題解:由於每個數能開根號的次數很少,當區間和為區間長度的時候,說明該區間全為1了,所以不必要更新下去,所以我們可以減少更新線段樹的複雜度。 注意:輸入的L不一定小於

HDU - 4027 Can you answer these queries? (線段)

tar main 區間求和 hup target 代碼 name upd memset 題目傳送門:HDU - 4027 Can you answer these queries? 題目大意: 存在n艘敵軍戰艦,每艘戰艦都有能量值,我軍存在一種秘密武器,每次能夠將在該

HDOJ 4027 Can you answer these queries?(線段+區間標記)

The input contains several test cases, terminated by EOF.    For each test case, the first line contains a single integer N, denoting there are N battleshi

Can you answer these queries?(線段,區間更新更新到點)

題目 連結:傳送門 A lot of battleships of evil are arranged in a line before the battle. Our commander decides to use our secret weapon to

HDU 4027 Can you answer these queries?(線段區間開方)

sizeof sqrt .cn swap %d nes nts following clr Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 6576

HDU 4027 Can you answer these queries?(線段/區間不等更新

push battle mark put action light blog acc lang 傳送門 Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limi

HDU 4027 Can you answer these queries? (線段+暴力)

題意: 給出一段序列和兩種操作,第一種操作,將x,y區間的數均開平方,第二種操作,對x,y區間進行求和。 分析: 一開始還不敢用線段樹做,因為純線段樹下來根暴力列舉複雜度差不了多少,但由於開方,所以在很少次的迴圈裡就能達到1,所以就可以直接這麼做了。但題目沒有說名忍耐值的範圍,要是0太多

hdu 4027 Can you answer these queries?[線段]

題目 題意: 輸入一個 :n  。(1<=n<<100000) 輸入n個數    (num<2^63) 輸入一個m :代表m個操作    (1<=m<<100000;) 接下來m行,每行三個數a,b,c,如果a==0,執行操

hdu 4027 Can you answer these queries?(線段——區間更新)(思路)

Can you answer these queries? Problem Description A lot of battleships of evil are arranged in a line before the battle. Our comm

hdu 4027 Can you answer these queries?(線段)(更新 但需要標記記錄剪枝)

題目:http://acm.hdu.edu.cn/showproblem.php?pid=4027 題目大意:給定100000個數,兩種操作,0 i j表示將i j這段的數字都開根號(向下取整),1

HDU 4027 Can you answer these queries?

date 全部 swe shanghai man nts less mina query 題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=4027 解題思路:線段樹區間更新,查詢。主要問題在於如何解決快速對一個區間所有數據開根號

hdoj 4027 Can you answer these queries? 【線段 區間減為平方 + 區間求和】

The input contains several test cases, terminated by EOF.   For each test case, the first line contains a single integer N, denoting there are N battleshi

HDU4027Can you answer these queries?(線段區間和,坑)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 8674

Can you answer these queries? HDU - 4027線段+技巧)

fin 題意 ios fff PE sqrt += 長度 scan 題意:給一個數組序列, 數組長度為100000 兩種操作: 一種操作是將某一個固定區間所有數開方(向下取整) 另一種操作是詢問某個區間的所有數字之和。 由於數不超過263,因此開個七八次就變成1,由於只有

Can you answer these queries? HDU 4027線段+延遲標記+開根號的速度)

H - Can you answer these queries? Time Limit:2000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64u Submit Status Practice HDU 40

SP1043 GSS1 - Can you answer these queries I(線段,區間最大子段和(靜態))

有一種 nbsp 不用 端點 合並 表示 格式 space iostream 題目描述 給出了序列A[1],A[2],…,A[N]。 (a[i]≤15007,1≤N≤50000)。查詢定義如下: 查詢(x,y)=max{a[i]+a[i+1

SP1557 GSS2 - Can you answer these queries II(線段

傳送門   線段樹好題 因為題目中相同的只算一次,我們可以聯想到HH的項鍊,於是考慮離線的做法 先把所有的詢問按$r$排序,然後每一次不斷將$a[r]$加入線段樹 線段樹上維護四個值,$sum,hix,sumtag,hixtag$,分別代表當前節點的值,節點歷史上的最大值,當前的增加標記,

SP2713 GSS4 - Can you answer these queries IV(線段)

def for turn put spa main 是否 case 每次 傳送門 解題思路   大概就是一個數很少次數的開方會開到\(1\),而\(1\)開方還是\(1\),所以維護一個和,維護一個開方標記,維護一個區間是否全部為\(1/0\)的標記。然後每次修改時先看是否