H - Can you answer these queries?

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

这题一个值最大为2^63,实际上开

#include<bits/stdc++.h>

using namespace std;

const int MAXN = 100000 + 10;
struct f
{
    int len;
    long long val;
}a[MAXN * 4];
/*
void pushdown(int x)
{
    if(a[x].lazy != 0)
    {
        if(a[x << 1].val != a[x << 1].len)
            a[x << 1].lazy = 1;
        if(a[x << 1 | 1].val != a[x << 1 | 1].len)
            a[x << 1 | 1].lazy = 1;
        a[x].lazy = 0;
    }
}
*/
void build(int left , int right , int rt)
{
    a[rt].len = right - left + 1;
    if(left == right)
    {
        scanf("%lld" , &a[rt].val);
        return;
    }
    int mid = (left + right) >> 1;
    build(left , mid , rt << 1);
    build(mid + 1 , right , rt << 1 | 1);
    a[rt].val = a[rt << 1].val + a[rt << 1 | 1].val;
}

void update(int x , int y , int left , int right , int rt)
{
    if(a[rt].val == a[rt].len)
        return;
    if(left == right)
    {
        a[rt].val = sqrt(a[rt].val);
        return;
    }
    int mid = (left + right) >> 1;
    //if(a[rt].lazy)
    //    pushdown(rt);
    if(mid >= x && a[rt << 1].len != a[rt << 1].val)
        update(x , y , left , mid , rt << 1);
    if(mid < y && a[rt << 1 | 1].len != a[rt << 1 | 1].val)
        update(x , y , mid + 1, right , rt << 1 | 1);
    a[rt].val = a[rt << 1].val + a[rt << 1 | 1].val;
}

long long query(int x , int y, int left , int right ,int rt)
{
    if(left >= x && right <= y)
        return a[rt].val;
    int mid = (left + right) >> 1;
    long long sum = 0;
    if(mid >= x)
        sum += query(x , y , left , mid , rt << 1);
    if(mid < y)
        sum += query(x , y , mid + 1 , right , rt << 1 | 1 );
    return sum;
}
int main()
{
    int n ;
    int cas = 0;
    while(~scanf("%d" , &n))
    {
        cas ++;
        memset(a,0,sizeof(a));
        build(1 ,n ,1);
        int Q;
        scanf("%d" , &Q);
        int k , b , c;
        printf("Case #%d:\n" , cas);
        while(Q --)
        {
            scanf("%d %d %d" , &k , &b , &c);
            if(b > c)
                swap(b , c);
            if(k == 0)
                update(b , c , 1 , n , 1);
            else
            {
                printf("%lld\n", query(b ,c , 1 , n ,1 ));
            }
        }
        cout << endl;
    }
    return 0;
}

6次根号就到1了,而原本就是1的就不用往下走了,这样可以卡住世间复杂度,知道这里就可以大胆得写了

猜你喜欢

转载自blog.csdn.net/ant_e_zz/article/details/81173915