线段树区间更新+求和

对一个区间两种操作:

c x  y

1、使区间[x,y]的数等于其开方数(四舍五入)。

2、查询区间[x,y]的和。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
#define root 1 , N , 1
#define LL long long
#define SZ 100000
int N ;
int M ;

struct segment{
    int l,r;
    LL sum,maxn;
}tree[SZ<<2];
LL num[SZ];
void PushUp(int rt)
{
    tree[rt].sum = tree[rt<<1].sum + tree[rt<<1|1].sum;
    tree[rt].maxn = max( tree[rt<<1].maxn , tree[rt<<1|1].maxn );
}

void build(int l,int r,int rt)
{
    tree[rt].l = l;
    tree[rt].r = r;
    if( l == r )
    {
        LL x;
        scanf("%lld",&x);
        tree[rt].sum = tree[rt].maxn = x;
        return ;
    }
    int m = ( l + r ) >> 1;
    build(lson);
    build(rson);
    PushUp(rt);
}

void update(int l,int r,int rt)
{
    if( tree[rt].l == tree[rt].r )
    {
        tree[rt].sum = (LL)sqrt(tree[rt].sum);//...
        tree[rt].maxn = (LL)sqrt(tree[rt].maxn);
        return ;
    }
    int m = ( tree[rt].l  + tree[rt].r ) >> 1;
    if( l <= m && tree[rt<<1].maxn > 1 )
        update(l,r,rt<<1);
    if( m < r && tree[rt<<1|1].maxn > 1 )
        update(l,r,rt<<1|1);
    PushUp(rt);
}

LL query(int l,int r,int rt)
{
    if( l <= tree[rt].l && tree[rt].r <= r ){
        return tree[rt].sum;
    }

    LL res = 0;
    int m = (tree[rt].l + tree[rt].r) >> 1;
    if( l <= m )res += query(l,r,rt<<1);
    if( m < r )res += query(l,r,rt<<1|1);
    return res;
}

int main()
{
    while( cin >> N )
    {
        build(root);
        cin >> M;
        while( M -- )
        {
            int a, b , c;
            scanf("%d %d %d", &a,&b,&c);
            if( b > c )swap(b,c);
            if( a )
                cout << query( b, c, 1 ) << endl ;
            else
                update( b, c, 1 );
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值