Can you answer these queries? HDU - 4027 有点坑

Posted qingyuyyyyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Can you answer these queries? HDU - 4027 有点坑相关的知识,希望对你有一定的参考价值。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=1e5+10;
ll a[N];
int n,m;
int op,l,r;
struct node
{
    ll l,r;
    ll sum;
}tr[N*4];
void build(int root,int l, int r)
{
    tr[root]={l,r};
    tr[root].sum=0;
    if(l==r)
    {
        tr[root].sum=a[l];
        return;
    }
    int mid=l+r>>1;
    build(root<<1,l,mid);
    build(root<<1|1,mid+1,r);
    tr[root].sum=tr[root<<1].sum+tr[root<<1|1].sum;
}
void update(int root,int l,int r)
{
    if(l>tr[root].r||tr[root].l>r)
        return;
    if (tr[root].sum==(tr[root].r-tr[root].l + 1))
        return;
    if (tr[root].l==tr[root].r)
    {
        tr[root].sum=sqrt(tr[root].sum);
        return;
    }
    int mid=tr[root].l+tr[root].r>>1;
    update(root<<1,l,r);
    update(root<<1|1,l,r);
    tr[root].sum=tr[root<<1].sum+tr[root<<1|1].sum;
}
ll Query(int root,int l,int r)
{
    if (l>tr[root].r||tr[root].l>r)
        return 0;
    if (l<=tr[root].l&&tr[root].r<=r)
        return tr[root].sum;
    int mid=tr[root].l+tr[root].r>>1;
    ll res=0;
    res+=Query(root<<1,l,r);
    res+=Query(root<<1|1,l,r);
    return res;
}
int main()
{
    int cnt=0;
    while(~scanf("%d",&n))
    {
        printf("Case #%d:
",++cnt);
        for (int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        scanf("%d",&m);
        build(1,1,n);
        while (m--)
        {
            scanf("%d%d%d",&op,&l,&r);
            if(l>r)
                swap(l,r);
            if(op==0)
                update(1,l,r);
            else
                printf("%lld
",Query(1,l,r));
        }
        printf("
");
    }
    return 0;
}

 

以上是关于Can you answer these queries? HDU - 4027 有点坑的主要内容,如果未能解决你的问题,请参考以下文章

HDU4027 Can you answer these queries? —— 线段树 区间修改

HDU-Can you answer these queries? (线段树+区间修改)

HDU4027 Can you answer these queries?

HDU 1027 G - Can you answer these queries?

hdu 4027 Can you answer these queries?

SPOJ GSS2 Can you answer these queries II