hdu 4027 线段树 区间变换 区间求值

Can you answer these queries?

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 12591    Accepted Submission(s): 2991


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

题意:给一个数组序列, 数组长度为100000,现在有两种操作, 一种操作是将某一个固定区间所有数开方(向下取整),另一种操作是询问某个区间的所有数字之和。
数组元素和不超过2^63所以,所以每个数最多才能开方八次, 所以对于长度为n的数组,开方的次数最多为8*n。这时间节约时间的方法主要是处理被开方数为1的情况。在结构体中 设参量a[i].f=1时,区间值都为1 不用再递归...

#include 
#include 
#include 
#include 
#include 
typedef long long ll;
using namespace std;
const int INF=0xffffff0;
#define Maxn 100010

struct node
{
    int l,r,f;
    int mid()
    {
        return (l+r)/2;
    }
    ll sum;
}a[Maxn<<2];

ll num[Maxn];

void Build(int l,int r,int root)  ///建树
{
    a[root].l=l;
    a[root].r=r;
    a[root].f=0;
    if(l==r)
    {
        a[root].sum=num[l];
        if(num[l]<=1)    //如果数组值小于等于1时,a[root].f赋值为1;
            a[root].f=1;
        return ;
    }
    int mid=(l+r)/2;
    Build(l,mid,root*2);
    Build(mid+1,r,root*2+1);
    a[root].sum=a[root*2].sum+a[root*2+1].sum;
    if(a[root*2].f && a[root*2+1].f)      ///左右儿子节点都为1 父亲结点也要为1
        a[root].f=1;          
}

void update(int l,int r,int root)
{
    if(a[root].f==1)   ///当a[root].f=1表示该结点所包含区间值都<=1,不用再递归,省时间
        return ;
    if(a[root].r==r && a[root].l==l && l==r)  ///注意l==r 必须递归到叶子结点 
    {
        a[root].sum=(ll)sqrt(1.0*a[root].sum);  
        if(a[root].sum<=1)
            a[root].f=1;
        return ;
    }
    if(r<=a[root].mid())
        update(l,r,root*2);
    else if(l>a[root].mid())
        update(l,r,root*2+1);
    else
    {
        update(l,a[root].mid(),root*2);
        update(a[root].mid()+1,r,root*2+1);
    }
    a[root].sum=a[root*2].sum+a[root*2+1].sum;
    if(a[root*2].f && a[root*2+1].f)
        a[root].f=1;
}

ll Query(int l,int r,int root)
{
     ll ans=0;
     if(a[root].l==l && a[root].r==r)
        return a[root].sum;
     if(r<=a[root].mid())
        ans=ans+Query(l,r,root*2);
     else if(l>a[root].mid())
        ans=ans+Query(l,r,root*2+1);
     else
     {
         ans=ans+Query(l,a[root].mid(),root*2);
         ans=ans+Query(a[root].mid()+1,r,root*2+1);
     }
     return ans;
}

int main()
{
    int n,M,x,y,z;
    int cas=1;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%I64d",&num[i]);

        Build(1,n,1);
        scanf("%d",&M);
        printf("Case #%d:\n",cas++);
        while(M--)
        {
            scanf("%d%d%d",&z,&x,&y);
            if(x>y)
            {
                int t=x;
                x=y;
                y=t;
            }
            if(z==0)
                update(x,y,1);
            else
                printf("%I64d\n",Query(x,y,1));
        }
        printf("\n");  //格式
    }
    return 0;
}


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