zoukankan      html  css  js  c++  java
  • HDU-4027 Can you answer these queries?

    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

    题意:n个数据,m组查询,每组查询由a l r 组成 a==0是求[l~r]内的值开根号 a==1求和[l~r]
    思路:当一个值为1是用sqrt 还是1,这里我们就不用再往下面算了,我们搞个f数组记录是否出现过1了。其他就是模板了,这里不使用lazy也能过,但是不标记1,盲目开根号会T(因为我T了
    #include<bits/stdc++.h>
    #define ll long long
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    const int maxn = 100000+5;
    ll sum[maxn<<2],f[maxn<<2];
    void pushup(ll rt)
    {
        sum[rt]=sum[rt<<1]+sum[rt<<1|1];
        f[rt]=f[rt<<1]&f[rt<<1|1];
    }
    void build(ll l,ll r,ll rt)
    {
        if(l==r)
        {
            scanf("%lld",&sum[rt]);
            return;
        }
        ll m=(l+r)>>1;
        build(lson);
        build(rson);
        pushup(rt);
    }
    void updata(ll L,ll R,ll l,ll r,ll rt)
    {
        if(l==r)
        {
            sum[rt]=sqrt(sum[rt]);
            if(sum[rt]<=1) f[rt]=1;
            return;
        }
        ll m=(l+r)>>1;
        if(L<=m&&!f[rt<<1]) updata(L,R,lson);
        if(R>m&&!f[rt<<1|1]) updata(L,R,rson);
        pushup(rt);
    }
    ll query(ll L,ll R,ll l,ll r,ll rt)
    {
        if(L<=l&&r<=R)
        {
            return sum[rt];
        }
        ll ans=0;
        ll m=(l+r)>>1;
        if(L<=m) ans+=query(L,R,lson);
        if(R>m) ans+=query(L,R,rson);
        return ans;
    
    }
    int main()
    {
        ll n,m,p=1;
        while(~scanf("%lld",&n))
        {
            memset(sum,0,sizeof(sum));
            memset(f,0,sizeof(f));
            build(1,n,1);
            ll a,b,c;
            int o[10];
            scanf("%lld",&m);
            printf("Case #%lld:
    ",p++);
            while(m--)
            {
                scanf("%lld %lld %lld",&a,&b,&c);
                ll bb=max(b,c);
                ll cc=min(b,c);
    
                if(a)
                    printf("%lld
    ",query(cc,bb,1,n,1));
                else
                    updata(cc,bb,1,n,1);
            }
              printf("
    ");
        }
    }
    View Code

    PS:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

  • 相关阅读:
    利用API将软件嵌入网页中
    TensorFlow:tf.train.Saver()模型保存与恢复
    TensorFlow:tf.reduce_mean(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None)
    TensoFlow的tf.reshape()
    Python:查看矩阵大小,查看列表大小
    TensorFlow:tf.contrib.layers.xavier_initializer
    python:list
    TensorFlow-Python:创建空列表list与append的用法
    MATLAB:读取mat文件中物体的三维坐标,显示三维模型
    查看已装TensorFlow的版本和路径
  • 原文地址:https://www.cnblogs.com/MengX/p/9113773.html
Copyright © 2011-2022 走看看