• HDU 4027 Can you answer these queries?(线段树)


    Can you answer these queries?

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


    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
     
    取根看似不好想,其实对于一个 2^64 的数,log2(64)次取根号就成了1。
    1取根永远是1。。。对于整个区间都是1的话操作 0 就无效了。
    所以时间复杂度是 log2(64)*n*log(n)。
     
    题是用一个( r - l + 1 ) == sum[rt] 返回。
    其实我觉得这么做是大有问题的。 在于区间中如果飞船的护甲本来就是0的话。
    就出错了。。不过应该是没这样的数据。。所以能过。
     
    如果有这样的数据的话。
    开一个 sum[i][j]( i = 1 ~ 20,j = 1 ~ n )的数组
    表示区间下标从 1 ~ j 取了 i 次根号的数值的和。
    然后再用线段树处理 。 这样的话有0的数据也没问题了。
    但是就是写不过。 QAQ , 表示不喜欢这些范围说得不清不楚的题。
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <string>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include <map>
    #include <set>
    #include <stack>
    #include <algorithm>
    using namespace std;
    #define root 1,n,1
    #define lson l,mid,rt<<1
    #define rson mid+1,r,rt<<1|1
    #define lr rt<<1
    #define rr rt<<1|1
    typedef long long LL;
    const int oo = 1e9+7;
    const double PI = acos(-1.0);
    const double eps = 1e-6 ;
    const int N = 100010;
    LL e[N],date[N<<2];
    int n , m ;
    
    void Up( int rt ) {
        date[rt] = date[lr] + date[rr];
    }
    void build( int l , int r , int rt ) {
        if( l == r ) {
            date[rt] = e[l] ;
            return ;
        }
        int mid = (l+r)>>1;
        build(lson) , build(rson);
        Up(rt);
    }
    
    void update( int l , int r , int rt , int L , int R ) {
        if( r - l + 1 == date[rt] ) return ;
        if( l == r ){
            date[rt] = (LL)sqrt((double)date[rt] );
            return ;
        }
        int mid = (l+r)>>1;
        if( R <= mid ) update(lson,L,R);
        else if( L > mid ) update(rson,L,R);
        else update(lson,L,mid),update(rson,mid+1,R);
        Up(rt);
    
    }
    
    LL query( int l , int r , int rt , int L , int R ) {
        if( L == l && r == R )return date[rt];
        int mid = (l+r)>>1;
        if( R <= mid ) return query(lson,L,R);
        else if( L > mid ) return query(rson,L,R);
        else return query(lson,L,mid)+query(rson,mid+1,R);
    }
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
    //        freopen("out.txt","w",stdout);
        #endif // LOCAL
        int _ , x , y , c , cas = 1 ;
        while( ~scanf("%d",&n) ) {
            printf("Case #%d:
    ",cas++);
            for( int i = 1 ; i <= n ; ++i ){
                scanf("%I64d",&e[i]);
            }
            build(root);
            scanf("%d",&m);
            while( m-- ) {
                scanf("%d%d%d",&c,&x,&y);
                if( x > y ) swap( x , y );
                if( !c ) {
                    update(root,x,y);
                }
                else {
                    printf("%I64d
    ",query(root,x,y));
                }
            }
            puts("");
        }
    }
    View Code
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    nginx查看配置文件nginx.conf路径
    http://127.0.0.1/thinkphp5/public/index/teacher/delete/id/1.html 这样的URL下,页面收不到get参数
    thinkphp5访问报错 ...with message 'mkdir(): Permission denied' in... 或...failed to open stream: Permission denied' in...
    thinkphp报错No input file specified. (对于隐藏url中的index.php 是如何做到的?)
    收藏网址
    jquery 和 原生js 获取<select><option></option></select>标签的值和文本
    Macbook下MySQL卸载方法
    提示错误 Call to undefined function imagepng() in …
    Mac 安装phpmyadmin提示无法登陆 mysql服务器解决办法
    php把一个字符串分割成字符数组(可以用参数指定数组每个元素字符的长度)
  • 原文地址:https://www.cnblogs.com/hlmark/p/4249222.html
Copyright © 2020-2023  润新知