HDU4027线段树

http://acm.hdu.edu.cn/showproblem.php?pid=4027
这道题很玄学呀~我用宏定义输入数据会超时,用scanf(“%d”,&)会超时(原先是%I64)这个坑先留着,以后看看能不能想到其他方法。
因为数据最大2^64,所以最多开方7次数据就变成1了。这时不必再进行更新操作,直接结束递归。树中的判断方法是总和是否等于区间长度。
下面是代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define N 200000+5
#define R (rt<<1|1)
#define L (rt<<1)
#define lson l,mid,L
#define rson mid+1,r,R
#define mid (l+r)/2
#define INF 0x3f3f3f3f
#define mem(arr,a) memset(arr,a,sizeof(arr))
#define LL long long int
#define sd1(a) scanf("%d",&a)
#define sd2(a,b) scanf("%d%d",&a,&b)
#define sd3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd1(a) printf("%d",a)
#define pd2(a,b) printf("%d%d",a,b)
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
using namespace std;
const int maxn = 100010;

LL sum[maxn * 4];
int n, m;

void PushUp(int rt)
{
    sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
    return;
}

void build(int l, int r, int rt)
{


    if (l == r)
    {
        scanf("%I64d", &sum[rt]);
        return;
    }
    build(lson);
    build(rson);
    PushUp(rt);
}

void update(int a, int b, int l, int r, int rt)
{
    if (l == r)
    {
        sum[rt] = sqrt(1.0 * sum[rt]);
        return;
    }
    if (a <= l && r <= b && sum[rt] == r - l + 1)
    {
        return;
    }
    if (a <= mid) update(a, b, lson);
    if (mid < b) update(a, b, rson);
    PushUp(rt);
}

LL query(int a, int b, int l, int r, int rt)
{

    if (a <= l && r <= b)
    {
        return sum[rt];
    }
    LL ans = 0;
    if (a <= mid)   ans += query(a, b, lson);
    if (mid < b)    ans += query(a, b, rson);
    return ans;
}

int main()
{
    int test = 0;
    while (scanf("%d", &n) != EOF)
    {
        build(1, n, 1);
        int a, b, c;
        int x, y;
        scanf("%d", &m);
        printf("Case #%d:\n", ++test);
        while (m--)
        {
            scanf("%d%d%d", &a, &b, &c);
            x = min(b, c);
            y = max(b, c);
            if (a == 0)
            {
                update(x, y, 1, n, 1);
            }
            else
            {
                printf("%I64d\n", query(x, y, 1, n, 1));
            }
        }
        puts("");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值