hdu 4027 Can you answer these queries?

http://acm.hdu.edu.cn/showproblem.php?pid=4027

Can you answer these queries?
Time Limit:2000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64u

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次操作,每次操作有t,a,b ,  t = 0时第a个数到第b个数都变成其本身的平方根[sqrt(al[i])](取整),t = 1时查找第a个数到第b个数的和
"You can assume that the sum of all endurance value is less than 2 63" 注意用long long
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define N 100010
#define Lson root<<1, L, tree[root].Mid()
#define Rson root<<1|1, tree[root].Mid() + 1, R

using namespace std;

struct Tree
{
    int L, R;
    long long sum;
    int Mid()
    {
        return (L + R) / 2;
    }
    int Len()
    {
        return (R - L + 1);
    }
} tree[N * 4];

long long al[N];

void Build(int root, int L, int R)
{
    tree[root].L = L, tree[root].R = R;
    if(L == R)
    {
        tree[root].sum = al[L];
        return ;
    }

    Build(Lson);
    Build(Rson);

    tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
}

void Update(int root, int L, int R)
{
    if(tree[root].Len() == tree[root].sum)
        return ;//注意这儿如果省了会TLE
    if(tree[root].L == tree[root].R)
    {
        tree[root].sum = (long long)sqrt(tree[root].sum);
        return ;
    }
    if(R <= tree[root].Mid())
        Update(root<<1, L, R);
    else if(L > tree[root].Mid())
        Update(root<<1|1, L, R);
    else
    {
        Update(Lson);
        Update(Rson);
    }
    tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
}

long long Query(int root, int L, int R)
{
    if(tree[root].L == L && tree[root].R == R)
        return tree[root].sum;
    if(R <= tree[root].Mid())
        return Query(root<<1, L, R);
    else if(L > tree[root].Mid())
        return Query(root<<1|1, L, R);
    else
        return Query(Lson) + Query(Rson);
}

int main()
{
    int n, m, i, t, a, b, x = 0;
    while(~scanf("%d", &n))
    {
        x++;
        for(i = 1 ; i <= n ; i++)
            scanf("%I64d", &al[i]);
        Build(1, 1, n);
        scanf("%d", &m);
        printf("Case #%d:\n", x);
        while(m--)
        {
            scanf("%d%d%d", &t, &a, &b);
            if(a > b)
                swap(a, b);//注意,如果所给的a比b大,两者就需要调换位置
            if(t == 0)
                Update(1, a, b);
            else
                printf("%I64d\n", Query(1, a, b));
        }
        printf("\n");
    }
    return 0;
}

 

 

 
posted @ 2015-08-03 15:09  午夜阳光~  阅读(118)  评论(0编辑  收藏  举报