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

题意:可以对区间进行两种操作,令某个区间的所有数开方,二求某个区间的区间和。 任何一个数开方最多8次,如果开到1就不允许再开了,即区间内没一个数都是1就不用再更新了。只需要往上更新 sum[rt]==rr[rt]-ll[rt]+1

#include 
#include 
#include 
#include 
using namespace std;
const int N = 100010;
long long ll[N*20],rr[N*20];
long long sum[N*20],add[N*20],arr[N*20];
void pushup(int rt)
{
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt){
    ll[rt]=l;
    rr[rt]=r;
    sum[rt]=0;
    if(l==r)
    {
        sum[rt]=arr[l];
        return ;
    }
    int m=(l+r)>>1;
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    pushup(rt);
}

void update(int L,int R,int rt)
{
    if(ll[rt]==rr[rt])
    {
        sum[rt]=sqrt(1.0*sum[rt]);
        return;
    }
    if(L<=ll[rt]&&rr[rt]<=R&&sum[rt]==rr[rt]-ll[rt]+1)
    {
        return ;
    }
     int m=( ll[rt]+rr[rt])>>1;
     if(R<=m)
     {
        update(L,R,rt<<1);
     }
     else if(m1|1);
     else 
     {
        update(L,m,rt<<1);
        update(m+1,R,rt<<1|1);
     }
     pushup(rt);
}
long long query(int L,int R,int rt)
{
    if(L<=ll[rt]&&rr[rt]<=R)
    {
        return sum[rt];
    }
    int m=(ll[rt]+rr[rt])>>1;
    // long long ans=0;
    // if(L<=m) ans+=query(L,R,rt<<1);
    // if(m
    // return ans;
    if(R<=m)
    {
        return query(L,R,rt<<1);
    }
    else if(L>m)
    {
        return query(L,R,rt<<1|1);
    }
    else
    {
        long long v=query(L,m,rt<<1);
        long long v1=query(m+1,R,rt<<1|1);
        return v+v1;
    }

}
int main()
{
    int n;
    int tt=1;
    while(scanf("%d",&n)!=EOF)
    {
        printf("Case #%d:\n",tt++);
        for(int i=1;i<=n;i++)
            scanf("%lld",&arr[i]);
        build(1,n,1);
        int m;
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(cif(a==1)
            {
                printf("%lld\n",query(b,c,1));
            }
            else
            {
                update(b,c,1);
            }
        }
        puts("");
    }

}

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