hdoj 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): 10892    Accepted Submission(s): 2535


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

1,若op为0,表示区间[x, y]里面的数全变为原数的平方,四舍五入;

2,若op为1,表示查询区间[x, y]里面所有数的和。



分析:对一个2^64次方的数,8次操作变为1,以后操作都不会改变数值。当然若一个数变为0,则操作后数值不变。



思路:我们可以标记区间里面非0非1的数的个数。显然,若个数为0,则说明该区间不需要操作即update时不再进行;对于个数不为0的区间,则更新到底。




AC代码:


#include 
#include 
#include 
#include 
#define MAXN 100000
#define ll o<<1
#define rr o<<1|1
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define LL long long
using namespace std;
struct Tree{
    int l, r;
    LL sum;
    int need;
};
Tree tree[MAXN<<2];
void PushUp(int o){
    tree[o].sum = tree[ll].sum + tree[rr].sum;
    tree[o].need = tree[ll].need + tree[rr].need;
}
void build(int o, int l, int r)
{
    tree[o].l = l, tree[o].r = r;
    if(l == r)
    {
        scanf("%lld", &tree[o].sum);
        tree[o].need = tree[o].sum >= 1 ? 1 : 0;
        return ;
    }
    int mid = (l + r) >> 1;
    build(lson);
    build(rson);
    PushUp(o);
}
void update(int o, int L, int R)
{
    if(!tree[o].need)//没有非0非1的数 不再修改
        return ;
    if(tree[o].l == tree[o].r)
    {
        tree[o].sum = sqrt(tree[o].sum);
        if(tree[o].sum <= 1)
            tree[o].need = 0;
        return ;
    }
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(R <= mid)
        update(ll, L, R);
    else if(L > mid)
        update(rr, L, R);
    else
    {
        update(ll, L, mid);
        update(rr, mid+1, R);
    }
    PushUp(o);
}
LL Query(int o, int L, int R)
{
    if(L <= tree[o].l && R >= tree[o].r)
        return tree[o].sum;
    int mid = (tree[o].l + tree[o].r) >> 1;
    if(R <= mid)
        return Query(ll, L, R);
    else if(L > mid)
        return Query(rr, L, R);
    else
        return Query(ll, L, mid) + Query(rr, mid+1, R);
}
int main()
{
    int n, k = 1;
    while(scanf("%d", &n) != EOF)
    {
        build(1, 1, n);
        printf("Case #%d:\n", k++);
        int m;
        scanf("%d", &m);
        while(m--)
        {
            int op, x, y;
            scanf("%d%d%d", &op, &x, &y);
            int p;
            if(x > y)//注意x y的大小
            {
                p = x;
                x = y;
                y = p;
            }
            if(op == 0)
                update(1, x, y);
            else
                printf("%lld\n", Query(1, x, y));
        }
        printf("\n");
    }
    return 0;
}


你可能感兴趣的:(线段树)