【第22期】观点:IT 行业加班,到底有没有价值?

HDU 4027 Can you answer these queries? 线段树

原创 2016年05月30日 21:16:55

题意:

给你一个区间

有两种操作 i)把x~y区间数据减小至原来的值的平方根 ii)查询区间x~y的值

分析:

简单的线段树更新,强烈剪枝  如果区间和sum【st】=r-l+1说明这个区间值全为一不需要继续操作 tle->ac

ACcode:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define maxn 100002
#define ll long long
#define inf 0x3f3f3f3f
#define mid ((l+r)>>1)
#define tmp (st<<1)
#define len (r-l+1)
#define lson l,mid,tmp
#define rson mid+1,r,tmp|1
#define push_up(x) sum[x]=sum[tmp]+sum[tmp|1]
using namespace std;
ll sum[maxn<<2];
inline void build(int l,int r,int st){
    sum[st]=0;
    if(l==r){
        scanf("%I64d",&sum[st]);
        return ;
    }
    build(lson);
    build(rson);
    push_up(st);
}
inline void updata(int L,int R,int l,int r,int st){
    if(sum[st]==r-l+1)return;
    if(l==r){
        sum[st]=sqrt(double(sum[st]));
        return ;
    }
    if(L<=mid)updata(L,R,lson);
    if(R>mid)updata(L,R,rson);
    push_up(st);
}
inline ll query(int L,int R,int l,int r,int st){
    if(L<=l&&r<=R)return sum[st];
    ll ret=0;
    if(L<=mid)ret+=query(L,R,lson);
    if(R>mid)ret+=query(L,R,rson);
    return ret;
}
int main(){
    int cnt=1,n,q;
    while(~scanf("%d",&n)){
        build(1,n,1);
        scanf("%d",&q);
        printf("Case #%d:\n",cnt++);
        while(q--){
            int k,x,y;
            scanf("%d%d%d",&k,&x,&y);
            if(x>y)x^=y^=x^=y;
            if(k)printf("%I64d\n",query(x,y,1,n,1));
            else updata(x,y,1,n,1);
        }
        putchar('\n');
    }
    return 0;
}


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

HDU 4027 Can you answer these queries?(线段树单点更新)

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K...

[HDU][线段树]1754.I Hate It

思路: 利用线段树实现,具体参考:[算法系列之二十三]线段树(Interval Tree) 代码 <pre class="prettyprint"

Can you answer these queries? - HDU 4027 - 线段树

Can you answer these queries? - HDU 4027 - 线段树题目:  A lot of battleships of evil are arranged in a li...

hdu4027 Can you answer these queries? 线段树

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)Memory Limit: 65768/65768 K (Java/Oth

HDU 4027 Can you answer these queries? 线段树 区间修改 区间查询

Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K...
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)