HDU4027:Can you answer these queries?(线段树单点更新)

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
 


 

有n艘战舰,每艘战舰的防护罩都有一定的能量,我军有秘密武器,每轰一次,一个区间内的战舰防护罩将为原来能量的开方,查询的则是这个区间内战舰防护罩的能量之和。

要注意的是,区间的两边的边界值输入时大小是不确定的

 

#include 
#include 
#include 
#include 
using namespace std;

const int L = 100000+10;
__int64 sum[L<<2],cnt[L<<2];

void add(int i)
{
    sum[i] = sum[2*i]+sum[2*i+1];
    cnt[i] = cnt[2*i]&&cnt[2*i+1];
}

void init(int l,int r,int i)
{
    if(l == r)
    {
        scanf("%I64d",&sum[i]);
        return;
    }
    int mid = (l+r)>>1;
    init(l,mid,2*i);
    init(mid+1,r,2*i+1);
    add(i);
}

void insert(int L,int R,int l,int r,int i)
{
    if(l == r)
    {
        sum[i] = sqrt(sum[i]);//向下取整
        if(sum[i]<=1)//如果为0,则标记不能再访问了
            cnt[i] = 1;
        return ;
    }
    int mid = (l+r)>>1;
    if(L<=mid && !cnt[2*i])//孩子已经破坏了则不需再访问
        insert(L,R,l,mid,2*i);
    if(R>mid && !cnt[2*i+1])
        insert(L,R,mid+1,r,2*i+1);
    add(i);
}

__int64 query(int L,int R,int l,int r,int i)//查询区间和
{
    if(L<=l && r<=R)
        return sum[i];
    int mid = (l+r)>>1;
    __int64 ans = 0;
    if(L<=mid)
        ans+=query(L,R,l,mid,2*i);
    if(R>mid)
        ans+=query(L,R,mid+1,r,2*i+1);
    return ans;
}

int main()
{
    int n,m,i,j,x,l,r,cas = 1,flag;
    while(~scanf("%d",&n))
    {
        memset(sum,0,sizeof(sum));
        memset(cnt,0,sizeof(cnt));
        init(1,n,1);
        printf("Case #%d:\n",cas++);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d%d",&flag,&l,&r);
            if(l>r)//这里要注意
                swap(l,r);
            if(flag)
                printf("%I64d\n",query(l,r,1,n,1));
            else
                insert(l,r,1,n,1);
        }
        printf("\n");
    }

    return 0;
}


 

 

 

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