码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-01-26 01:18:56      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:

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:\n",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\n",query(root,x,y));
            }
        }
        puts("");
    }
}
View Code

 

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

标签:

原文地址:http://www.cnblogs.com/hlmark/p/4249222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!