HDU4027线段树

来源:互联网 发布:暂七师军乐队 知乎 编辑:程序博客网 时间:2024/04/27 03:22

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;}
0 0
原创粉丝点击