SPOJ - GSS4 Can you answer these queries IV

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq1059752567/article/details/71434380

这种题做的多了,套路一下就好,只是。。。

#include <stdio.h>
#include <string.h>
#include <ctime>
#include <stack>
#include <string>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define LL long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lowbit(x) (x&-x)
#define mem(x,d) memset(x,d,sizeof(x))

const int maxn = 1e5+5;
const int INF = 0x3f3f3f3f;

LL sum[maxn<<3];

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

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

void update(int L,int R,int l,int r,int rt)
{
    if(sum[rt]==(r-l+1))
        return;
    if(l==r)
    {
        sum[rt] = (LL)sqrt(sum[rt]+0.0);
        return;
    }
    int m = (l+r)>>1;
    if(L<=m)
        update(L,R,lson);
    if(R>m)
        update(L,R,rson);
    pushUp(rt);
}

LL query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
        return sum[rt];
    int m = (l+r)>>1;
    LL ret = 0;
    if(L<=m)
        ret += query(L,R,lson);
    if(R>m)
        ret += query(L,R,rson);
    return ret;
}

int main()
{
    int n,q,cas=1;
    while(scanf("%d",&n)!=EOF)
    {
        build(1,n,1);
        scanf("%d",&q);
        printf("Case #%d:\n",cas++);
        while(q--)
        {
            int c,l,r;
            scanf("%d%d%d",&c,&l,&r);
            if(l>r) swap(l,r);      //这里想骂出题人一万句caonima
            if(c==0)
                update(l,r,1,n,1);
            else
                printf("%lld\n",query(l,r,1,n,1));
        }
        printf("\n");
    }
	return 0;
}


猜你喜欢

转载自blog.csdn.net/qq1059752567/article/details/71434380
今日推荐