zoukankan      html  css  js  c++  java
  • 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个战舰,每个战舰有一个耐力值;现在有q次操作,每一次操作可以查询某一个区间内的战舰的耐力值的和,也可改变某一个区间内战舰的耐力值(降低为原来的根号倍 即x = 根号x)
    思路:这里我们用到了区间查询和修改所以应该很容易的想到用线段树吧,但是这里的区间修改有所不同就是是将区间内每一个数变为原来的根号级。大概就是这样吧 下面直接上代码!
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cstdio>
     5 using namespace std;
     6 const int maxn = 100005;
     7 typedef long long LL;
     8 LL sum[maxn<<2],add[maxn<<2];
     9 int n,q;
    10 void pushup(int rt){
    11     sum[rt] = sum[rt<<1]+sum[rt<<1|1];
    12 }
    13 void build(int l,int r,int rt)
    14 {
    15     if(l==r){
    16         scanf("%lld",&sum[rt]);
    17         return ;
    18     }
    19     int mid = (l+r)>>1;
    20     build(l,mid,rt<<1);
    21     build(mid+1,r,rt<<1|1);
    22     pushup(rt);
    23 }
    24 void update(int L,int R,int l,int r,int rt)
    25 {
    26     if(l==r){
    27         sum[rt] = sqrt(sum[rt]);
    28         return;
    29     }
    30     if(L<=l&&r<=R&&(sum[rt]==(r-l+1)))return;
    31     int mid = (l+r)>>1;
    32     if(L<=mid)update(L,R,l,mid,rt<<1);
    33     if(R>mid)update(L,R,mid+1,r,rt<<1|1);
    34     pushup(rt);
    35 }
    36 LL query(int L,int R,int l,int r,int rt)
    37 {
    38     if(L<=l&&r<=R)return sum[rt];
    39     LL ans = 0;
    40     int mid = (l+r)>>1;
    41     if(L<=mid) ans+= query(L,R,l,mid,rt<<1);
    42     if(R>mid) ans += query(L,R,mid+1,r,rt<<1|1);
    43     return ans;
    44 }
    45 int main()
    46 {
    47     int t = 1;
    48     while(scanf("%d",&n)!=EOF){
    49         build(1,n,1);
    50         scanf("%d",&q);
    51         int op,l,r;
    52         printf("Case #%d:
    ",t++);
    53         while(q--){
    54             scanf("%d%d%d",&op,&l,&r);
    55             if(l>r)swap(l,r);
    56             if(!op)update(l,r,1,n,1);
    57             else if(op==1)printf("%lld
    ",query(l,r,1,n,1));
    58         }
    59         printf("
    ");
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    【Nginx】跨域配置
    【Python】【Chart】图标绘制/地图生成
    【Python】操作压缩文件
    【VSCode】koroFileHeader插件自动添加文件及函数注释
    【性能】web页面性能之lighthouse使用
    【VSCode】格式化后换行
    【Python】MD5
    【IDEA】自定义/自动生成/注释/新增文件自动生成注释/自动生成方法注释
    【Java】文件下载/下载Excel/下载文件到本地
    【杂项】英语学习
  • 原文地址:https://www.cnblogs.com/wangrunhu/p/9599692.html
Copyright © 2011-2022 走看看