zoukankan      html  css  js  c++  java
  • 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): 10249    Accepted Submission(s): 2350


    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

    之前听说过很多神级线段树的题,最后都是暴力拼循环节,今天难得见一个入门级别的,也算涨姿势了~

    题意:给定n个数(1~100000),两种操作(1~100000)0 or 1 ,0操作是把给定区间内的数都变成原来的开方(向下取整),1操作,询问区间内的数的总和;

    突破点:所有数据和的范围是2的63此方,大神们发现开方六七次以后,这些数就都变成1了~所以就不需要再向下更新了,

    那么判定不需要更新的条件就是:该节点下的区间总和与该区间长度相等,那么它们妥妥都是1了,就可以返回

    但是本题坑坑比较多~

    1坑:给定区间 Xth 与 Yth大小关系不定,记得用下swap~,不然就是RE(非法访问)。

    2坑 : 每组测试实例最后还需要多输出一个空行~

    0.0也怪不细心~

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <math.h>
      5 #include <iostream>
      6 #include <algorithm>
      7 #include <climits>
      8 #include <queue>
      9 #define ll long long
     10 
     11 using namespace std;
     12 
     13 
     14 const int MAX = 200005;
     15 ll num[MAX];
     16 
     17 struct nodes
     18 {
     19     int left,right;
     20     ll large;
     21 } tree[MAX*5];
     22 
     23 void pushup(int root)
     24 {
     25     tree[root].large = tree[root*2].large + tree[root*2+1].large;
     26 }
     27 void build(int root,int left,int right)
     28 {
     29     tree[root].left = left;
     30     tree[root].right = right;
     31     if(left == right)
     32     {
     33          tree[root].large = num[left];
     34          return;
     35     }
     36 
     37    int mid = (left+right)/2;
     38 
     39     build(2*root,left,mid);
     40     build(2*root+1,mid+1,right);
     41 
     42     pushup(root);
     43 }
     44 
     45 void update(int root,int left,int right)
     46 {
     47     if(tree[root].large == tree[root].right - tree[root].left + 1)
     48         return;
     49     if(tree[root].right == tree[root].left)
     50        {
     51            tree[root].large = (ll)sqrt(tree[root].large);
     52            return;
     53        }
     54     int mid = (tree[root].left+tree[root].right)/2;
     55     if(left <= mid && right > mid)
     56     {
     57         update(2*root,left,mid);
     58         update(2*root+1,mid+1,right);
     59     }
     60     else if(left > mid)
     61     {
     62         update(2*root+1,left,right);
     63     }
     64     else
     65     {
     66         update(2*root,left,right);
     67     }
     68     pushup(root);
     69 }
     70 
     71 
     72 ll query(int root ,int left,int right)
     73 {
     74     if(tree[root].large == tree[root].right - tree[root].left + 1)
     75     {
     76         return right - left + 1;
     77     }
     78     if(left == tree[root].left && right == tree[root].right)
     79     {
     80         return  tree[root].large;
     81     }
     82     int mid = (tree[root].left+tree[root].right)/2;
     83     ll ans = 0;
     84     if(right > mid && left <= mid)
     85     {
     86         ans += query(2*root,left,mid);
     87         ans += query(2*root+1,mid+1,right);
     88     }
     89     else if(left > mid)
     90     {
     91         ans += query(2*root+1,left,right);
     92     }
     93     else
     94     {
     95         ans += query(2*root,left,right);
     96     }
     97     return ans;
     98 }
     99 
    100 int main(void)
    101 {
    102     int i,cmd,x,y,n,q,cnt= 1;
    103     while(scanf("%d",&n) != -1)
    104     {
    105         for(i = 1; i <= n; i++)
    106             scanf("%lld",&num[i]);
    107             build(1,1,n);
    108         scanf("%d",&q);
    109         printf("Case #%d:
    ",cnt++);
    110         for(i = 0; i <q; i++)
    111         {
    112             scanf("%d %d %d",&cmd,&x,&y);
    113             if(y < x)
    114                 swap(x,y);
    115             if(cmd)
    116                 printf("%lld
    ",query(1,x,y));
    117             else
    118                 update(1,x,y);
    119         }
    120         printf("
    ");
    121     }
    122     return 0;
    123 }

     精简版

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <math.h>
      5 #include <iostream>
      6 #include <algorithm>
      7 #include <climits>
      8 #include <queue>
      9 #define ll long long
     10 
     11 using namespace std;
     12 
     13 struct Segment_T
     14 {
     15     static const int MAX = 1e5+1000;
     16     ll num[MAX];
     17     struct nodes
     18     {
     19         int lt,rt;
     20         ll add,sum;
     21     } T[MAX*5];
     22 
     23     Segment_T(){}
     24 
     25     void pushup(int R)
     26     {
     27         T[R].sum = T[R<<1].sum + T[R<<1|1].sum;
     28     }
     29     void pushdown(int R)
     30     {
     31         if(T[R].add)
     32         {
     33             T[R<<1].add += T[R].add;
     34             T[R<<1|1].add += T[R].add;
     35             T[R<<1].sum += T[R].add * (T[R<<1].rt - T[R<<1].lt + 1);
     36             T[R<<1|1].sum += T[R].add * (T[R<<1|1].rt - T[R<<1|1].lt + 1);
     37             T[R].add = 0;
     38         }
     39     }
     40     void build(int R,int lt,int rt)
     41     {
     42         T[R].lt = lt;
     43         T[R].rt = rt;
     44         if(lt == rt)
     45         {
     46             T[R].sum = num[lt];
     47             return;
     48         }
     49         int mid = (lt+rt)>>1;
     50 
     51         build(R<<1,lt,mid);
     52         build(R<<1|1,mid+1,rt);
     53 
     54         pushup(R);
     55     }
     56     void update(int R,int lt,int rt)
     57     {
     58         if(T[R].sum == T[R].rt - T[R].lt + 1)
     59             return;
     60         if(T[R].rt == T[R].lt)
     61         {
     62             T[R].sum = (ll)sqrt(T[R].sum); //返回操作处
     63             return;
     64         }
     65         int mid = (T[R].lt+T[R].rt)>>1;
     66         if(lt <= mid && rt > mid)
     67         {
     68             update(R<<1,lt,mid);
     69             update(R<<1|1,mid+1,rt);
     70         }
     71         else if(lt > mid)
     72             update(R<<1|1,lt,rt);
     73         else
     74             update(R<<1,lt,rt);
     75         pushup(R);
     76     }
     77     ll query(int R,int lt,int rt)
     78     {
     79         if(T[R].sum == T[R].rt - T[R].lt + 1)
     80         {
     81             return rt - lt + 1;
     82         }
     83         if(lt == T[R].lt && rt == T[R].rt)
     84         {
     85             return  T[R].sum;  //返回操作处
     86         }
     87         int mid = (T[R].lt+T[R].rt)>>1;
     88         ll ans = 0;
     89         if(rt > mid && lt <= mid)
     90             ans = query(R<<1,lt,mid) + query(R<<1|1,mid+1,rt);
     91         else if(lt > mid)
     92             ans += query(R<<1|1,lt,rt);
     93         else
     94             ans += query(R<<1,lt,rt);
     95         return ans;
     96     }
     97 };
     98 Segment_T Tr;
     99 int main(void)
    100 {
    101     int i,cmd,x,y,n,q,cnt= 1;
    102     while(scanf("%d",&n) != -1)
    103     {
    104         for(i = 1; i <= n; i++)
    105             scanf("%lld",&Tr.num[i]);
    106         Tr.build(1,1,n);
    107         scanf("%d",&q);
    108         printf("Case #%d:
    ",cnt++);
    109         for(i = 0; i <q; i++)
    110         {
    111             scanf("%d %d %d",&cmd,&x,&y);
    112             if(y < x)
    113                 swap(x,y);
    114             if(cmd)
    115                 printf("%lld
    ",Tr.query(1,x,y));
    116             else
    117                 Tr.update(1,x,y);
    118         }
    119         printf("
    ");
    120     }
    121     return 0;
    122 }
  • 相关阅读:
    密码学
    MD5
    计算机基础之操作系统
    python中列表之间求差集、交集、并集
    Python语言中各种进制相互转换
    计算机基础
    bzoj2705 [SDOI2012]Longge的问题
    bzoj3160 万径人踪灭
    codeforces 528D Fuzzy Search
    杜教筛 && bzoj3944 Sum
  • 原文地址:https://www.cnblogs.com/henserlinda/p/4699656.html
Copyright © 2011-2022 走看看