zoukankan      html  css  js  c++  java
  • 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.
     

    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:
    ", 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
    ", 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;
    } 
    

      

  • 相关阅读:
    定时任务:crontab: installing new crontab
    报错:Sqoop Failing this attempt. Failing the application.
    数据库优化
    vscode ,保存失败提示:关于Failed to save 'package.json': The content of the file is newer. Please..解决办法
    php -1 month 的问题
    sql 中convert和cast区别
    [SQL case when的两种用法]
    Android编译Lame库(Mp3编解码库)
    AndroidStudio使用Cmake编译armeabi-v7a,arm64-v8a的so库
    图片压缩原理
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4696734.html
Copyright © 2011-2022 走看看