zoukankan      html  css  js  c++  java
  • hdu4027Can you answer these queries?【线段树】

    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 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. 

    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

    因为更新的时候是进行开方 所以常规的更新就不行了

    但是因为是开方 对64位的数来说 开方的次数是有限制的

    而且当一个数开方成都是1以后再开方也还是1 所以一个区间都是1的时候就不需要继续更新了

    刚开始query用的是之前的写法 结果T了

    看题解改成了这样就过了

    
    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define inf 1e18
    using namespace std;
    
    int m, n;
    const int maxn = 100005;
    int weapon[maxn];
    long long tree[maxn << 2], lazy[maxn<<2];
    
    void pushup(int rt)//更新
    {
        tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
    }
    
    void build(int l, int r, int rt)
    {
        if(l == r){
            scanf("%lld", &tree[rt]);
            return;
        }
        int m = (l + r) >> 1;
        build(l, m, rt << 1);
        build(m + 1, r, rt << 1 | 1);
        pushup(rt);
    }
    
    
    /*void update(int L, int C, int l, int r, int rt)
    {
        if(l == r){
            tree[rt] += C;
            return;
        }
        int m = (l + r) >>1;
        if(L <= m) update(L, C, l, m, rt << 1);
        else update(L, C, m + 1, r, rt << 1 | 1);
        pushup(rt);
    }*/
    
    void update(int L, int R, int l, int r, int rt)
    {
        if(tree[rt] == r - l + 1){//已经全部是1了就不用更新了
            return;
        }
        if(l == r){
            tree[rt] = sqrt(tree[rt]);
            return;
        }
        int m = (l + r) >> 1;
        if(L <= m) update(L, R, l, m, rt << 1);
        if(R > m) update(L, R, m + 1, r, rt << 1 | 1);
        pushup(rt);
    }
    
    long long query(int L, int R, int l, int r, int rt)
    {
        if(l == L && r == R){
            return tree[rt];
        }
        int m = (l + r) >> 1;
        //pushdown(rt, m - l + 1, r - m);
    
        long long ans = 0;
        if(R <= m) return query(L, R, l, m, rt << 1);
        if(L > m) return query(L, R, m + 1, r, rt << 1 | 1);
        return query(L, m, l, m, rt << 1) + query(m + 1, R, m + 1, r, rt<<1|1);
    }
    
    
    int main()
    {
        int cas = 1;
        while(scanf("%d", &n) != EOF){
            build(1, n, 1);
            scanf("%d", &m);
            printf("Case #%d:
    ", cas++);
            while(m--){
                int a, b, c;
                scanf("%d%d%d", &a, &b, &c);
                if(b > c){
                    swap(b, c);
                }
                if(a == 0){
                    update(b, c, 1, n, 1);
                }
                else{
                    printf("%lld
    ", query(b, c, 1, n, 1));
                }
            }
            printf("
    ");
        }
        return 0;
    }
    
  • 相关阅读:
    互斥锁Mutex与信号量Semaphore的区别
    c/c++强制类型转换
    c++中的隐藏、重载、覆盖(重写)
    运算符重载详解
    类的大小
    C++ static、const和static const 以及它们的初始化
    一种隐蔽性较高的Java ConcurrentModificationException异常场景
    Java编码常见的Log日志打印问题
    Java编程常见缺陷汇总(一)
    Java字符串连接的多种实现方法及效率对比
  • 原文地址:https://www.cnblogs.com/wyboooo/p/9643395.html
Copyright © 2011-2022 走看看