码迷,mamicode.com
首页 > 其他好文 > 详细

HDU4027——线段树成段更新——Can you answer these queries?

时间:2015-08-02 21:29:51      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:

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
 
/*
线段树成段更新开根号
记得如果ans==r-l+1,就不用更新了,否则TLE

*/
#include <bits/stdc++.h>
using namespace std;

const int MAX = 1e6 + 10;
long long sum[MAX << 2];
long long a[MAX];
int n;

void build(long long rt, long long l, long long r)
{
    sum[rt] = 0;
    if(l == r){
        sum[rt] = a[l];
        return;
    }
    long long mid = l + r >> 1;
    build(rt*2, l , mid);
    build(rt*2+1, mid + 1, r);
    sum[rt] = sum[rt*2] + sum[rt*2+1];
}

void update(long long rt, long long l, long long r, long long L, long long R)
{
    if(l == r){
        sum[rt] = sqrt(sum[rt]);
        return;
    }
    long long mid = l + r >> 1;
    if(L <= mid) update(rt*2, l, mid, L, R);
    if(R > mid) update(rt*2+1, mid + 1, r, L, R);
    sum[rt] = sum[rt*2] + sum[rt*2+1];
}



long long  query(long long rt, long long l, long long r, long long L, long long R)
{
    if(L <= l && R >= r) return sum[rt];
    long long mid = l + r >> 1;
    long long ret = 0;
    if(L <= mid) ret += query(rt*2, l, mid, L, R);
    if(R > mid) ret += query(rt*2+1, mid+1, r, L, R);
    return ret;
}

int main()
{
    int cas = 1;
    while(~scanf("%d", &n)){
            printf("Case #%d:\n", cas++);
        for(int i = 1; i <= n; i++)
            scanf("%I64d", &a[i]);
        build(1, 1, n);
        int m;
        scanf("%d", &m);
        int flag;
        long long  l, r;
        while(m--){
            scanf("%d%I64d%I64d", &flag, &l, &r);
            if(l >= r) swap(l, r);
            if(flag){
                long long ans = query(1, 1, n, l, r);
                printf("%I64d\n", ans);
            }
            else {
                long long ans = query(1, 1, n, l, r);
                if(ans != r - l + 1) 
                    update(1, 1, n, l, r);
            }

        }

        puts("");
    }
    return 0;
} 

  

HDU4027——线段树成段更新——Can you answer these queries?

标签:

原文地址:http://www.cnblogs.com/zero-begin/p/4696734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!