• 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 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
     
     
    题意:给出一个n的数组,支持2种操作:
    1.0 a b  :把a到b之间的数都开平方
    2.1 a b  :查询区间a~b的数之和
     
     
    注意:这道题求出的平方根要下取整。
       然后,每个样例之后有空行。
     
    有个技巧,写在了注释。
     
     
     
      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #include<cmath>
      5 
      6 using namespace std;
      7 
      8 #define lson l,m,rt<<1
      9 #define rson m+1,r,rt<<1|1
     10 #define LL long long
     11 const int maxn=100000+5;
     12 
     13 LL sum[maxn<<2];
     14 
     15 //因为2^63总和最多2^63
     16 //如果只有1个数,也就最多开7次根号
     17 //即开着开着就恒为1
     18 //所以,当该区间的和等于区间长度(r-l)+1的时候,
     19 //该区间的值都不会再更新
     20 
     21 //1表示这个区间不需要更新了
     22 
     23 void pushup(int rt)
     24 {
     25     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
     26 
     27 }
     28 
     29 void build(int l,int r,int rt)
     30 {
     31     if(l==r)
     32     {
     33         scanf("%lld",&sum[rt]);
     34         return ;
     35     }
     36 
     37     int m=(l+r)>>1;
     38 
     39     build(lson);
     40     build(rson);
     41     pushup(rt);
     42 }
     43 
     44 void update(int L,int R,int l,int r,int rt)
     45 {
     46     if(sum[rt]==r-l+1)
     47         return ;
     48 
     49     if(l==r)
     50     {
     51         sum[rt]=(LL)(sqrt((long double)sum[rt]));
     52         //printf("%lld ",sum[rt]);
     53         return ;
     54     }
     55 
     56     int m=(l+r)>>1;
     57 
     58     if(L<=m)
     59         update(L,R,lson);
     60     if(R>m)
     61         update(L,R,rson);
     62 
     63     pushup(rt);
     64 }
     65 
     66 LL query(int L,int R,int l,int r,int rt)
     67 {
     68     if(L<=l&&R>=r)
     69     {
     70         return sum[rt];
     71     }
     72 
     73     int m=(l+r)>>1;
     74 
     75     LL ret=0;
     76 
     77     if(L<=m)
     78         ret+=query(L,R,lson);
     79     if(R>m)
     80         ret+=query(L,R,rson);
     81 
     82     return ret;
     83 }
     84 
     85 int main()
     86 {
     87     int cas=1;
     88 
     89     int n;
     90 
     91     while(scanf("%d",&n)!=EOF)
     92     {
     93         //memset(cal,0,sizeof(cal));
     94 
     95         build(1,n,1);
     96 
     97         int m;
     98         scanf("%d",&m);
     99 
    100         int T,x,y;
    101 
    102         printf("Case #%d:
    ",cas++);
    103 
    104         for(int i=1;i<=m;i++)
    105         {
    106             scanf("%d %d %d",&T,&x,&y);
    107 
    108             if(x>y)
    109                 swap(x,y);
    110 
    111             if(T==0)
    112             {
    113                 update(x,y,1,n,1);
    114             }
    115             else
    116             {
    117                 printf("%lld
    ",query(x,y,1,n,1));
    118             }
    119         }
    120 
    121         printf("
    ");
    122     }
    123     return 0;
    124 }
    View Code
     
     
     
     
     
     
  • 相关阅读:
    Winform中用了皮肤控件之后,报错:容量超出了最大容量 参数名:capacity
    C# 生成二维码
    T-sql语句修改数据库逻辑名、数据库名、物理名
    ASP.NET MVC中使用jQuery时的浏览器缓存问题
    关于asp.net页面缓存
    关于VS 工具箱灰色,不可用的解决方案
    Android
    如何让一个DIV水平,垂直方向都居中于浏览器?
    cookie.setPath()的用法
    CSS选择器
  • 原文地址:https://www.cnblogs.com/-maybe/p/4479090.html
Copyright © 2020-2023  润新知