zoukankan      html  css  js  c++  java
  • Can you answer these queries? HDU

    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.InputThe 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 2 63.
      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.
    OutputFor 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

    注意:更新的时候是区间内每个数开方,当区间内的数都是1时,不更新。
     1 #include<cmath>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<iostream>
     5 #include<algorithm>
     6 #define INF 1e8
     7 #define lson l , m , rt << 1
     8 #define rson m+1,r,  rt << 1 | 1
     9 using namespace std;
    10 typedef long long ll;
    11 
    12 const int maxn=400000;
    13 
    14 int n,q;
    15 ll sum[maxn];
    16 
    17 void Pushup(int rt){ sum[rt]=sum[rt<<1]+sum[(rt<<1)|1]; }
    18 
    19 void Build(int l,int r,int rt){
    20     if(l==r){ scanf("%lld",&sum[rt]); return; }
    21     int m=(l+r)>>1;
    22     Build(lson);
    23     Build(rson);
    24     Pushup(rt); 
    25 }
    26 
    27 void Update(int L,int R,int l,int r,int rt){
    28     if(l==r){ sum[rt]=sqrt(1.0*sum[rt]); return; }
    29     if(L<=l&&r<=R&&sum[rt]==r-l+1) return;
    30     int m=(l+r)>>1;
    31     if(L<=m) Update(L,R,lson);
    32     if(R>m)  Update(L,R,rson);
    33     Pushup(rt);
    34 }
    35 
    36 ll Query(int L,int R,int l,int r,int rt){
    37     if(L<=l&&r<=R) return sum[rt];
    38     int m=(l+r)>>1;
    39     ll temp=0;
    40     if(L<=m) temp=temp+Query(L,R,lson);
    41     if(R>m)  temp=temp+Query(L,R,rson);
    42     return temp; 
    43 }
    44 
    45 int main()
    46 {   int kase=1;
    47     while(~scanf("%d",&n)){
    48         Build(1,n,1);
    49         printf("Case #%d:
    ",kase++);
    50         cin>>q;
    51         while(q--){
    52             int a,b,c;
    53             scanf("%d%d%d",&a,&b,&c);
    54             int x=min(b,c),y=max(b,c);                          //key point!!!!
    55             if(a==0) Update(x,y,1,n,1);
    56             if(a==1) printf("%lld
    ",Query(x,y,1,n,1));
    57         }
    58         cout<<endl;
    59     }
    60     return 0;
    61 }
  • 相关阅读:
    数据库中Schema和Database有什么区别
    VS2015智能提示由英文改为中文
    分配数据库角色权限
    【转载】使用局部标准差实现图像的局部对比度增强算法
    RS485的常用电路设计
    c++对txt文件的读取与写入 【转载】
    OpencV使用fitEllipse拟合椭圆后,获取椭圆参数 【转载】
    C++指定编译代码语句(模块)
    C++自动创建文件夹
    vs2015中复制C++ DLL 和.pdb文件到C#工程中bin目录的设置方法
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7365301.html
Copyright © 2011-2022 走看看