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

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=4027

    解题思路:线段树区间更新,查询。主要问题在于如何解决快速对一个区间所有数据开根号然后求和 ----- 实际上是根本不用关心这个问题。2^64 在开根号7次之后已经变成1了,继续开根号没意义。

    因此,对于每次更新,如果这个区间长度和值一样(全部为1)直接返回,否则更新到点然后更新父节点。

    查询就和普通区间查询一样即可。

    需要注意X Y的大小关系,加个if(x > y) swap(x, y)即可

    代码:

     1 const int maxn = 1e5 + 5;
     2 ll A[maxn], tree[maxn * 4];
     3 int n, m;
     4 
     5 void build(int l, int r, int k){
     6     if(l == r){
     7         tree[k] = A[l];
     8         return;
     9     }
    10     int mid = (l + r) / 2;
    11     build(l, mid, k << 1);
    12     build(mid + 1, r, k << 1 | 1);
    13     tree[k] = tree[k << 1] + tree[k << 1 | 1];
    14 }
    15 void update(int ul, int ur, int l, int r, int k){
    16     if(tree[k] == (r - l + 1)) return;
    17     if(ul > r || ur < l) return;
    18     if(l == r && tree[k] != 1) {
    19         tree[k] = floor(sqrt(tree[k]));
    20         return;
    21     }
    22     
    23     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    24     update(ul, ur, l, mid, lc);
    25     update(ul, ur, mid + 1, r, rc);
    26     tree[k] = tree[lc] + tree[rc];
    27 }
    28 ll query(int ql, int qr, int l, int r, int k){
    29     if(ql <= l && qr >= r) return tree[k];
    30     if(ql > r || qr < l) return 0;
    31     
    32     int mid = (l + r) >> 1, lc = k << 1, rc = k << 1 | 1;
    33     ll ans1 = query(ql, qr, l, mid, lc);
    34     ll ans2 = query(ql, qr, mid + 1, r, rc);
    35     return ans1 + ans2;
    36 }
    37 
    38 int main(){
    39     int t = 1;
    40     while(scanf("%d", &n) != EOF){
    41         printf("Case #%d:
    ", t++);
    42         for(int i = 1; i <= n; i++)
    43             scanf("%lld", &A[i]);
    44         build(1, n, 1);
    45         int m;
    46         scanf("%d", &m);
    47         while(m--){
    48             int a, b, c;
    49             scanf("%d %d %d", &a, &b, &c);
    50             if(b > c) swap(b, c);
    51             if(a == 0){
    52                 update(b, c, 1, n, 1);
    53             }
    54             else{
    55                 printf("%lld
    ", query(b, c, 1, n, 1));
    56             }
    57         }
    58         puts("");
    59     }
    60 }

    题目:

    Can you answer these queries?

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


    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
  • 相关阅读:
    利用递归分割(Split)字符串
    SQL Server2012 T-SQL基础教程--读书笔记(1-4章)
    kindeditor编辑器的使用
    echarts绘制四川地图
    Windows下搭建PHP开发环境(Apache+PHP+MySQL)+调试工具Xdebug的配置
    给搜索关键字添加高亮,加以颜色区分
    SQL 生成6位随机数并MD5加密输出
    微信小程序登录 .net 后端实现
    钉钉小程序http post 请求
    浅谈Web Api配合SignalR的跨域支持
  • 原文地址:https://www.cnblogs.com/bolderic/p/7295224.html
Copyright © 2011-2022 走看看