HDU 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): 4570    Accepted Submission(s): 1087

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  

 

Source
The 36th ACM/ICPC Asia Regional Shanghai Site —— Online Contest  

 

Recommend lcy     关键就是一个数开根号几次后很快就可以变成1了。 如果都是1就不需要往下更新了。   还有就是输入的X,Y大小是不一定的,,这个坑了好久  

/*
HDU 4027 Can you answer these queries?

*/
#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
const int MAXN=5000100;
struct Node
{
    int l,r;
    long long sum;
}segTree[MAXN*3];
void Build(int i,int l,int r)
{
    segTree[i].l=l;
    segTree[i].r=r;
    if(l==r)
    {
        scanf("%I64d",&segTree[i].sum);
        return;
    }
    int mid=((l+r)>>1);
    Build(i<<1,l,mid);
    Build((i<<1)|1,mid+1,r);
    segTree[i].sum=segTree[i<<1].sum+segTree[(i<<1)|1].sum;
}
void action(int i,int l,int r)
{
    if(segTree[i].l==l && segTree[i].r==r && segTree[i].sum==r-l+1) return;
    if(segTree[i].l==segTree[i].r)
    {
        segTree[i].sum=sqrt(segTree[i].sum*1.0);
        return;
    }
    int mid=((segTree[i].l+segTree[i].r)>>1);
    if(r<=mid) action(i<<1,l,r);
    else if(l>mid)  action((i<<1)|1,l,r);
    else
    {
        action(i<<1,l,mid);
        action((i<<1)|1,mid+1,r);
    }
    segTree[i].sum=segTree[i<<1].sum+segTree[(i<<1)|1].sum;
}
long long SUM(int i,int l,int r)
{
    if(l==segTree[i].l && r==segTree[i].r) return segTree[i].sum;

    int mid=((segTree[i].l+segTree[i].r)/2);
    long long ans=0;

    if(r<=mid) ans=SUM(i<<1,l,r);
    else if(l>mid) ans=SUM((i<<1)|1,l,r);
    else
    {
        ans+=SUM(i<<1,l,mid);
        ans+=SUM((i<<1)|1,mid+1,r);
    }
    return ans;
}
int main()
{
  //  freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    int n;
    int T,X,Y;
    int iCase=0;
    int M;
    while(scanf("%d",&n)!=EOF)
    {
        iCase++;
        Build(1,1,n);
        scanf("%d",&M);
        printf("Case #%d:\n",iCase);
        while(M--)
        {
            scanf("%d%d%d",&T,&X,&Y);
            if(X>Y)swap(X,Y);//这个很重要
            if(T==0) action(1,X,Y);
            //else printf("%I64d\n",sum(1,X,Y));
            else printf("%I64d\n",SUM(1,X,Y));
        }
        printf("\n");
    }
    return 0;
}

 

    原文作者:算法小白
    原文地址: https://www.cnblogs.com/kuangbin/archive/2012/08/11/2633348.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞