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

题目链接 :点击打开链接


题目要求开根那么再大的一个数不超过十次开根就会变成1,那么此时就不需要再更新了,所以用线段树只需特判这个点就可以了。


题解:

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long ll;
const int mx=100010;
int n,m;
ll sum[mx<<2];
void push_up(int rt){
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt){
    if(l==r){
        scanf("%lld",sum+rt);
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(rt);
}
void update(int l,int r,int rt,int L,int R){
    if(sum[rt]==r-l+1)  return ;
    if(l==r){
        sum[rt]=sqrt(sum[rt]);
        return ;
    }
    int m=(l+r)>>1;
    if(L<=m) update(lson,L,R);
    if(R>m) update(rson,L,R);
    push_up(rt);
}
ll query(int l,int r,int rt,int L,int R){
    if(l>=L&&R>=r)  return sum[rt];
    int m=(l+r)>>1;
    ll ans=0;
    if(L<=m) ans+=query(lson,L,R);
    if(R>m) ans+=query(rson,L,R);  
    return ans;
}
int main(){
    int cases=1;
    while(cin>>n){
        build(1,n,1);
        int q,x,y,t;
        scanf("%d",&q);
        printf("Case #%d:\n",cases++);
        while(q--){
            scanf("%d%d%d",&t,&x,&y);
            if(x>y) swap(x,y);
            if(t){
                printf("%lld\n",query(1,n,1,x,y));
            }else{
                update(1,n,1,x,y);
            }
        }
        puts("");
    }
    return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值