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:摸鱼怪的博客分享,欢迎感谢各路大牛的指点~

  • 相关阅读:
    thinkphp引入phpmailer发送邮件
    让火狐的DIV被内容自动撑开
    mysql 日期操作 增减天数、时间转换、时间戳
    [MySQL] 几句MySQL时间筛选SQL语句[进入查看]
    公钥和私钥
    SSI整合搭建Struts2+Spring+Ibatis框架
    目前 NORTON SEP 及各类产品 离线升级包下载及升级方法
    Spring 3.1.1 + Struts 2.3.1.2 + Hibernate 4.1 整合(SSH)
    IIS与asp.net3.5的问题
    SSI框架整合
  • 原文地址:https://www.cnblogs.com/MengX/p/9113773.html
Copyright © 2011-2022 走看看