【SP2713 GSS4 - Can you answer these queries IV】 题解

本文提供了一种解决洛谷SP2713题目的方法,使用线段树进行区间更新与查询,通过六次开方操作简化问题。代码实现了读取输入、建立线段树、更新区间值及查询区间总和的功能。

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

题目链接:https://www.luogu.org/problemnew/show/SP2713

真暴力啊。

开方你开就是了,开上6次就都没了。

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define ll long long
using namespace std;
const int maxn = 100100;
inline ll read(){
    ll k = 0 ; char c = getchar() ;
    while (!isdigit(c)) c = getchar() ;
    while (isdigit(c)) k = k * 10 + c - 48, c = getchar() ;
    return k  ; 
}
ll n, m, a[maxn], t=0;
class Segment_Tree{
    #define lson l, mid, rt<<1
    #define rson mid+1, r, rt<<1|1
    public:
        ll tree[maxn<<2];
        void build(ll l, ll r, ll rt)
        {
            if(l == r)
            {
                tree[rt] = a[l];
                return;
            }
            ll mid = (l + r) >> 1;
            build(lson);
            build(rson);
            PushUP(rt);
        }
        
        void update(ll left, ll right, ll l, ll r, ll rt)
        {
            if(left <= l && r <= right)
            {
                if(tree[rt] <= r - l + 1) return;
                else
                {
                    if(l == r) 
                    {
                        tree[rt] = (ll)sqrt((double)tree[rt]);
                        return;
                    }
                }
            }
            ll mid = (l + r) >> 1;
            PushUP(rt);
            if(left <= mid) update(left, right, lson);
            PushUP(rt);
            if(right > mid) update(left, right, rson);
            PushUP(rt);
        }
        
        ll query(ll left, ll right, ll l, ll r, ll rt)
        {
            ll res = 0;
            if(left <= l && right >= r)
            {
                return tree[rt];
            }
            ll mid = (l + r) >> 1;
            if(left <= mid) res += query(left, right, lson);
            if(right > mid) res += query(left, right, rson);
            return res;
        }
        
    private:
        void PushUP(ll rt)
        {
            tree[rt] = tree[rt<<1] + tree[rt<<1|1];
        }
}T;
int main()
{
    while(~scanf("%lld",&n))
    {
        t++;
        printf("Case #%d:\n",t);
        memset(a, 0, sizeof(a));
        for(ll i = 1; i <= n; i++)
        a[i] = read();
        T.build(1,n,1);
        m = read();
        for(ll i = 1; i <= m; i++)
        {
            ll opt, x, y;
            opt = read();
            x = read();
            y = read();
            if(y < x) swap(x, y);
            if(opt == 0)
            T.update(x,y,1,n,1);
            else
            printf("%lld\n",T.query(x,y,1,n,1));
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/MisakaAzusa/p/9803084.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值