题目链接:https://loj.ac/problem/6281

思路:这里有一个定理:一个int范围内的数,最多开5次方(向小取整)。那么就是说:一个块在几次操作后很容易全部变成1,这个时候再操作开方,就不用修改了。

当然没有变为0的时候只能暴力修改了, 复杂度不会太高。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
//由块号寻找第一个块元素的下标
#define LL(x) ((x-1)*Len+1)
const LL maxn=5e5+5;
LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

LL a[50050], b[50050], flag[505], s[505];//b[i]:i的块号
LL Len, n;

void Sqrt_slove(LL x)
{
    if(flag[x])//块全部为1
    {
        return;
    }
    flag[x]=1;
    s[x]=0;

    for(int i=LL(x); i<LL(x+1); i++)//维护这个块
    {
        a[i]=sqrt(a[i]);
        s[x]+=a[i];
        if(a[i]>1)
        {
            flag[x]=0;
        }
    }
}

void Sqrt(LL L,LL R)
{
    LL bL=b[L], bR=b[R];

    if(bL==bR)//同一块
    {
        for(LL i=L;i<=R;i++)
        {
            s[bL]-=a[i];
            a[i]=(int)sqrt(a[i]);
            s[bL]+=a[i];
        }
    }
    else
    {
        for(LL i=L;i<LL(bL+1);i++)//维护边块
        {
            s[bL]-=a[i];
            a[i]=(int)sqrt(a[i]);
            s[bL]+=a[i];
        }

        for(LL i=bL+1;i<bR;i++)
        {
            Sqrt_slove(i);
        }

        for(LL i=LL(bR);i<=R;i++)//维护边块
        {
            s[bR]-=a[i];
            a[i]=(int)sqrt(a[i]);
            s[bR]+=a[i];
        }
    }

}

LL query(LL L, LL R)
{
    LL ans=0;
    LL bL=b[L], bR=b[R];

    if(bL==bR)
    {
        for(LL i=L;i<=R;i++)
        {
            ans+=a[i];
        }
    }
    else
    {
        for(LL i=L;i<LL(bL+1);i++)
        {
            ans+=a[i];
        }

        for(LL i=bL+1;i<bR;i++)
        {
            ans+=s[i];
        }

        for(LL i=LL(bR);i<=R;i++)
        {
            ans+=a[i];
        }
    }

    return ans;
}

void build(LL n)
{
    Len=n/sqrt(n);
    for(LL i=1;i<=n;i++)
    {
        a[i]=read();
        s[(i-1)/Len+1]=0;
        flag[(i-1)/Len+1]=0;
    }
    for(LL i=1;i<=n;i++)
    {
        b[i]=(i-1)/Len+1;
        s[b[i]]+=a[i];
    }
}

int main()
{
    n=read();
    build(n);

    for(LL i=1;i<=n;i++)
    {
        LL op=read(), L=read(), R=read(), c=read();
        if(op==0)
        {
            Sqrt(L, R);
        }
        else
        {
            printf("%lld\n", query(L, R));
        }
    }

    return 0;
}