题解 SP2713 【GSS4 - Can you answer these queries IV】

· · 题解

题解 SP2713 【GSS4 - Can you answer these queries IV】

题目传送门:

https://www.luogu.org/problemnew/show/SP2713

=========================================

(先瞎扯两句= =)

1.原本以为这题很水的,其实不然,还是比较脑洞的

2.我说这题是个重题你信么= =

双倍经验:

https://www.luogu.org/problemnew/show/P4145

p4145 上帝造题的七分钟2/花神游历各国

(虽然说这题的数据比较强,不算重题吧= =)

==========================================

对于这个题,我看见题目之后就去搜了一下= =

发现GSS4-Can you answer these queries竟然是一套完整的数据结构(毒瘤)题,考虑一下先从这题开始做起,以后可能会陆续做完其他的题吧

好了不扯了,开始讲解:

既然是个数据结构题,那么一定要向数据结构上想

首先可以想到一个很容易懂的东西:

对于一个数(这里指1e9以内的数),经过连续开方6次就会变成1

(不信的小伙伴可以自己手动试一下)

以后无论怎么操作都是1 !!!

那么有了这个认识,我们不妨这样考虑:

如果一个区间的最大值都为1,那么是不是对于整个区间都不用操作呢

正确性显然,因为如果整个区间最大值是1,那么整个区间显然都是1,那么1怎么开方都是1

有了这个想法,再加上不怕WA死的精神,我们不妨把它推广到本题上来

(不就是数据变成了1e18么,不是太大吧)(逃

做法如下:

建立一颗线段树,维护区间最大值和区间和,

在进行区间开方操作时,只需要判断一下区间最大值是否为1,如果为1那么不用操作,否则给区间和及区间最大值开方即可

时间复杂度很正确,即使是数据大了一些

下面奉上代码code

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define l_son p<<1
#define r_son p<<1|1
#define maxn 100003
using namespace std;
typedef long long ll;
const int inf=1e9+7;
inline ll read()
{
    ll p=0,f=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){p=p*10+c-'0';c=getchar();}
    return 1ll*f*p;}
struct Line_Segment_Tree  //定义线段树
{
    ll mx,value_;
}T[maxn<<2];
int n,m,L[maxn<<2],R[maxn<<2];
ll A[maxn];
inline ll max(ll a,ll b)
{
    return a>b?a:b;
}
inline void maintain(int p)//维护线段树信息 
{
    T[p].value_=T[l_son].value_+T[r_son].value_;
    T[p].mx=max(T[l_son].mx,T[r_son].mx);
}
inline void Build_(int p,int l,int r)//建树 
{
    L[p]=l,R[p]=r;
    if(l==r)
       {
            T[p].value_=T[p].mx=A[l];
            return ;
       }
    int mid=(l+r)>>1;
    Build_(l_son,l,mid),Build_(r_son,mid+1,r);
    maintain(p);
}
inline void change_(int p,int l,int r)//区间开方 
{
    if(L[p]==R[p])
        {
            T[p].mx=(ll)sqrt(T[p].mx);
            T[p].value_=(ll)sqrt(T[p].value_);
            return ;
        }
    int mid=(L[p]+R[p])>>1;
    if(l<=mid&&T[l_son].mx>1)change_(l_son,l,r);
    if(r>mid&&T[r_son].mx>1)change_(r_son,l,r);
    maintain(p);
}
inline ll ask_(int p,int l,int r)//询问区间和 
{
    if(l<=L[p]&&r>=R[p])return T[p].value_;
    int mid=(L[p]+R[p])>>1;
    ll ans=0;
    if(l<=mid)ans+=ask_(l_son,l,r);
    if(r>mid)ans+=ask_(r_son,l,r);
    return 1ll*ans;
}
inline void Clean()//多组数据注意清空 
{
    memset(T,0,sizeof(T));
    memset(A,0,sizeof(A));
    memset(L,0,sizeof(L));
    memset(R,0,sizeof(R));
}
int main()
{
    int cas=0;
    while(scanf("%d",&n)!=EOF)
        {
            printf("Case #%d:\n",++cas);
            for(int i=1;i<=n;i++)A[i]=read();
            Build_(1,1,n);//建树
            m=read();
            while(m--)
                {
                    int key_=read(),x=read(),y=read();
                  if(x>y)swap(x,y);//题目要求 
                    if(!key_)change_(1,x,y);//区间开方 
                    else if(key_==1)//询问区间和 
                        printf("%lld\n",ask_(1,x,y));
                }
            printf("\n");//注意输出格式坑点,数据之间有回车 
            Clean();
        }
    return 0;
}

好了,到这里这题就结束了

小总结:线段树可以维护一些神奇的量,能使得一些看似困难的操作变得很简单,的确需要十分熟练地使用

最后推广一下我的blog:

https://www.luogu.org/blog/new2zy/

感谢阅读,拜拜~~~