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

InputThe 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. 
OutputFor 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

题意:给出一串数字,每次操作将l-r中的所有数开根并向下取整.每次查询询问l-r的区间和,强制在线.

题解:好吧,我真的不会怎么区间修改....传说yyk大佬有办法,各位好奇的同志可以问他去了...于是乎点修改,但如果一个一个慢慢来肯定会超时,仔细研究发现其实不管多大的数,只要小于long long,最多7次开根就变成了1.因为2^7>63
2的2^7次方已经超过了long long的范围了
.所以对于已经被开根成1的数,我们已经没有必要去再开根算了,这样即使100000的数据最大复杂度也不高.至于怎么算一段数是否已被开根成一,只要区间和等于区间长度就可以了.

代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#define N 100010
#define lson root<<1
#define rson root<<1|1
using namespace std;

long long node[N<<2],n;

void pushup(int root)
{
    node[root]=node[lson]+node[rson];
}

void build(int l,int r,int root)
{
    if(l==r)
    {
        scanf("%lld",&node[root]);
        return;
    }
    int mid=(l+r)>>1;
    build(l,mid,lson);
    build(mid+1,r,rson);
    pushup(root);
}

void update(int left,int right,int l,int r,int root)
{
    if(l==r)
    {
        node[root]=sqrt(node[root]);
        return;
    }
    if(left<=l&&right>=r&&node[root]==r-l+1)
    {
        return;
    }
    int mid=(l+r)>>1;
    if(left<=mid)
    {
        update(left,right,l,mid,lson);
    }
    if(right>mid)
    {
        update(left,right,mid+1,r,rson);
    }
    pushup(root);
}

long long query(int left,int right,int l,int r,int root)
{
    if(left<=l&&right>=r)
    {
        return node[root];
    }
    int mid=(l+r)>>1;
    long long ans=0;
    if(left<=mid)
    {
        ans+=query(left,right,l,mid,lson);
    }
    if(right>mid)
    {
        ans+=query(left,right,mid+1,r,rson);
    }
    return ans;
}

int main()
{
    int n,m,ttt=0;
    while(scanf("%d",&n)!=EOF)
    {
        build(1,n,1);
        scanf("%d",&m);
        printf("Case #%d:\n",++ttt);
        for(int i=1;i<=m;i++)
        {
            int k,ll,rr;
            scanf("%d %d %d",&k,&ll,&rr);
            if(ll>rr)
            {
                swap(rr,ll);
            }
            if(k==0)
            {
                update(ll,rr,1,n,1);
            }
            if(k==1)
            {
                printf("%lld\n",query(ll,rr,1,n,1));
            }
        }
        printf("\n");
    }
    return 0;
}

 

 

 

 

每天刷题,身体棒棒!

posted @ 2017-10-08 15:50  Styx-ferryman  阅读(248)  评论(0编辑  收藏  举报