HDU 4027 Can you answer these queries?(线段树暴力修改+剪枝)

博客给出HDU 4027题目的链接,介绍解题思路。使用一个数组维护区间和,另一个维护区间是否都小于等于1,若全小于等于1则无需修改。因大数开根号几次就会等于1,可采用暴力解法,还提醒每组测试数据后加换行,并给出代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4027

解题思路:

一个数组维护区间和。

另一个维护区间是否都小于等于1,如果全是小于等于1就不用往下修改了。

因为一个很大的数(2^63)开根号开不了几次就会等于1,所以就是这么暴力。

注意每组测试数据后面一个换行。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define lson rt<<1,l,m
#define rson rt<<1|1,m+1,r
#define ll long long
#define mid int m=l+r>>1
#define al allone[rt<<1]
#define ar allone[rt<<1|1]
#define tl tree[rt<<1]
#define tr tree[rt<<1|1]
#define INF 0x3f3f3f3f
using namespace std;

const int N = 1e5+5;

ll tree[N<<2];
bool allone[N<<2];

void push_up(int rt)
{
    allone[rt] = al && ar;
    tree[rt] = tl + tr;
}

void update(int L,int R,int rt,int l,int r)
{
    if (allone[rt]){
        return ;
    }
    if (l==r){
        tree[rt] = (ll)(sqrt(tree[rt]));
        if (tree[rt]<=1) allone[rt] = true;
        return ;
    }
    mid;
    if (L<=m) update(L,R,lson);
    if (R>m)  update(L,R,rson);
    push_up(rt);
}

ll query(int L,int R,int rt,int l,int r)
{
    if (L<=l && r<=R){
        return tree[rt];
    }
    ll ans = 0;
    mid;
    if (L<=m) ans += query(L,R,lson);
    if (R>m)  ans += query(L,R,rson);
    return ans;
}

void build(int rt,int l,int r)
{
    if (l==r){
        scanf("%lld",&tree[rt]);
        if (tree[rt]<=1) allone[rt] = true;
        return ;
    }
    mid;
    build(lson);
    build(rson);
    push_up(rt);
}

int main()
{
    int n,q;
    for (int kca=1;~scanf("%d",&n);kca++){
        printf("Case #%d:\n",kca);
        memset(allone,false,sizeof allone);
        build(1,1,n);
        scanf("%d",&q);
        while (q--){
            int op,l,r;
            scanf("%d %d %d",&op,&l,&r);
            if (l>r) swap(l,r);
            if (op==0) update(l,r,1,1,n);
            else printf("%lld\n",query(l,r,1,1,n));
        }
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值