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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhangxiaoduoduo/article/details/83001501

 Can you answer these queries?

 HDU - 4027 

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

题意:对n个数进行m次操作。op,x,y,op=0,对x~y之间所有数开平方,op=1,求x~y的和。

思路:最开始以为是区间更新,但是后来发现开方的话无法用区间更新,故使用单点更新,每个数开方几次以后就成了1,所以复杂度不会太高。这里有个坑点就是,x可能比y大,此时要交换。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<utility>
#include<set>
#include<vector>
#include<map>
#include<queue>
#include<stack>
#define maxn 100010
#define INF 0x3f3f3f3f
#define LL long long
#define ULL unsigned long long
#define E 1e-8
#define mod 1000000007
#define P pair<int,int>
#define MID(l,r) (l+(r-l)/2)
#define lson(o) (o<<1)
#define rson(o) (o<<1|1)
using namespace std;

LL a[maxn];
struct node
{
    int l,r;
    LL sum;
}tree[maxn<<2];
void build(int o,int l,int r)
{
    tree[o].l = l;
    tree[o].r = r;
    if(l==r){
        tree[o].sum = a[l];
        return ;
    }
    int m = MID(l,r);
    int lc = lson(o),rc = rson(o);
    build(lc,l,m);
    build(rc,m+1,r);
    tree[o].sum  = tree[lc].sum + tree[rc].sum;
}
//区间更新
void update(int o,int l,int r)
{
    if(tree[o].l == l && tree[o].r == r && tree[o].sum==r-l+1){  //关键!!
        return ;
    }
    if(tree[o].l == tree[o].r){
        tree[o].sum = sqrt(tree[o].sum*1.0);
        return ;
    }
    int m = MID(tree[o].l,tree[o].r);
    int lc = lson(o),rc = rson(o);
    if(r<=m) update(lc,l,r);
    else if(l>m) update(rc,l,r);
    else{
        update(lc,l,m);
        update(rc,m+1,r);
    }
    tree[o].sum = tree[lc].sum + tree[rc].sum;
}
//区间查询
LL query(int o,int l,int r)
{
    if(tree[o].l>=l && tree[o].r<=r){
        return tree[o].sum ;
    }
    int m =MID(tree[o].l,tree[o].r);
    int lc = lson(o),rc = rson(o);
    if(r<=m) return query(lc,l,r);
    else if(l>m)  return query(rc,l,r);
    else return query(lc,l,m)+query(rc,m+1,r);
}
int main()
{
    int n,m,op,x,y,cases = 0;
    while(scanf("%d",&n)!=EOF){
        printf("Case #%d:\n",++cases);
        for(int i=1;i<=n;++i){
            scanf("%lld",&a[i]);
        }
        build(1,1,n);
        scanf("%d",&m);
        while(m--){
            scanf("%d %d %d",&op,&x,&y);
            if(x>y) swap(x,y);
            if(op==0){
                update(1,x,y);
            }
            else{
                LL ans = query(1,x,y);
                printf("%lld\n",ans);
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhangxiaoduoduo/article/details/83001501