1. 程式人生 > >Can you answer these queries? (HDU

Can you answer these queries? (HDU

Can you answer these queries?

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


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
Recommend lcy
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define MAX  100000
struct s{
   int l;
   int r;
   long long  num;
   int lazy;
   long long sum;
}a[MAX*4+5];
void build(int l,int r,int k)
{
    if(l==r)
    {
        a[k].l=l;
        a[k].r=r;
        scanf("%I64d",&a[k].num);
        a[k].sum=a[k].num;
        if(a[k].num==1)
        a[k].lazy=1;
        return ;
    }
    a[k].l=l;
    a[k].r=r;
    a[k].lazy=0;
    int mid=(l+r)/2;
    build(l,mid,k*2);
    build(mid+1,r,k*2+1);
    a[k].sum=a[k*2].sum+a[k*2+1].sum;
    if(a[k*2].lazy==1&&a[k*2+1].lazy==1)
        a[k].lazy=1;
}
void gg(int l,int r,int k)
{
    if(a[k].lazy==1)
        return ;
    if(a[k].l==a[k].r)
    {
        a[k].num=sqrt(a[k].num);
        if(a[k].num==1)
        a[k].lazy=1;
        a[k].sum=a[k].num;
        return ;
    }
    int  mid=(a[k].l+a[k].r)/2;
    if(r<=mid)
        gg(l,r,k*2);
    else if(l>mid)
        gg(l,r,k*2+1);
    else
    {
        gg(l,mid,k*2);
        gg(mid+1,r,k*2+1);
    }
    a[k].sum=a[k*2].sum+a[k*2+1].sum;
     if(a[k*2].lazy==1&&a[k*2+1].lazy==1)
        a[k].lazy=1;
}
long long ans;
void q(int l,int r,int k)
{
    if(a[k].l==l&&a[k].r==r)
    {
        ans+=a[k].sum;
        return ;
    }
    int mid=(a[k].l+a[k].r)/2;
    if(r<=mid)
        q(l,r,k*2);
    else if(l>mid)
        q(l,r,k*2+1);
    else
    {
        q(l,mid,k*2);
        q(mid+1,r,k*2+1);
    }
}
int main()
{
    int  n;
    int cnt=1;
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        build(1,n,1);
        printf("Case #%d:\n",cnt);
        cnt++;
        int m;
        scanf("%d",&m);
        for(int i=0;i<m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            if(y>z)
            {
                int temp=y;
                y=z;
                z=temp;
            }
            if(x==1)
            {
               ans=0;
                q(y,z,1);
                printf("%I64d\n",ans);
            }
            else
            {
                gg(y,z,1);
            }
        }
         printf("\n");
    }
}


相關推薦

Can you answer these queries? HDU

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

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?線段樹——區間更新)思路)

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

Can you answer these queries? 線段樹)

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 s

Can you answer these queries? 【HDU

題目連結 題意:就是給你一串數,讓你對一段區間操作,要麼是詢問,要麼是把這段區間的每個點開根號。 比賽看到這道題的時候,我還真高興呢,終於有線段樹了,我推了下,發現前期單點更新就行,後期的時候判斷是否為1,也就是此時節點的權值是否等於(r-l+1),於是我興高采烈的敲完了

Can you answer these queries? 區間更新 開方操作)

H - Can you answer these queries? 題目連結 ps:十分有趣的一道題目。一直在想如何用lazy標記,來使區間更新不超時間。 題意: 有n個數(1 <= n <= 100000),數最大為2^63。有m次操作(1 <=

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

HDU 4027 Can you answer these queries?

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

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

SP1043 GSS1 - Can you answer these queries I貓樹)

log imm str 定義 getc 這就是 out for space 給出了序列A[1],A[2],…,A[N]。 (a[i]≤15007,1≤N≤50000)。查詢定義如下: 查詢(x,y)=max{a[i]+a[i+1]+...+a

SP1557 GSS2 - Can you answer these queries II線段樹)

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

2018.10.16 spoj Can you answer these queries V線段樹)

傳送門 線段樹經典題。 就是讓你求左端點在[l1,r1][l1,r1][l1,r1]之間,右端點在[l2,r2][l2,r2][l2,r2]之間且滿足l1≤l2,r1≤r2l1\le l2,r1 \le

Can you answer these queries? HDU

題意 給定n個數,分別代表每一個戰艦的耐力值。有m個操作,當 t=1時,輸出x~y的戰艦耐力值之和,當 t=0時,使x~y戰艦的每一個耐力值都變為原來的平方根(向下取整)。 思路 遍歷 x到y 的每一個葉子節點,使他們的值變為原來的平方根,結果超時了。因為 1 的平

GSS1 - Can you answer these queries I線段樹)

stdin for ref get stdlib.h 中間 you () == 前言 線段樹菜雞報告,stO ZCDHJ Orz,GSS基本上都切完了。 Solution 考慮一下用線段樹維護一段區間左邊連續的Max,右邊的連續Max,中間的連續Max還有總和,發現這些東西

另一個畫風的GSS1 - Can you answer these queries I貓樹)

前言 其實我覺得你看貓錕的解釋也看不懂(主要是還有一些不良心的講解者不講清楚,當然這裡不是針對了qwq) 貓錕連結 Solution 考慮我們的線段樹是個啥玩意? 每一層都是一堆區間疊在一起。 我們在每一個節點維護的又是什麼? 左邊的max,右邊的max,中間的max,還有sum。 那麼我們改變一下:

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,執行操