题解:P15741 [JAG 2024 Summer Camp #2] Max Sum of GCD

· · 题解

先考虑对于一个确定 X,怎么快速求出 f(X)。

这里是一些关于 \gcd 的性质:

显然的,如果 a_i=a_j,且区间存在不相同的数,那么 a_i 和 a_j 的颜色相同不会使答案变劣。感性理解就是如果 a_i 涂成另一种颜色,那么原颜色的 \gcd 不会变大,新颜色的 \gcd 也不会变小,因此只考虑 X 中元素两两不同。

基于以上分析,我们认为:存在一个最优方案,使得只将一个数(即所有数值等于某个数的元素)涂成蓝色,其余涂成红色(认为拥有该颜色的元素数量较少的颜色为蓝色)。

证明:考虑先估一个较优值:只将最大值 mx 涂蓝,此时答案为最大值与剩余数 \gcd 之和。考虑如果最优解需要将不少于两个数涂蓝,那么由性质可以得知此时涂蓝数的 \gcd 不大于 \dfrac{mx}{2},这意味着涂红数的 \gcd 要大于 \dfrac{mx}{2},但此时涂红数也有至少两个,因此这不可能。

因此取不少于两个数不会更优,故命题得证。

因此现在只需要找到选取哪个数涂蓝可以最大化答案了:贪心地,我们想选最大值涂蓝,但此时剩余数的 \gcd 不一定最大。那不妨尝试次大值,次次大值……直接枚举一定会 TLE,考虑分析枚举次数上界:首先,如果一个数小于 \dfrac{mx}{2},那么它一定不优,理由参上。那么只需要考虑大于 \dfrac{mx}{2} 的数,记第 i 大的值为 v_i,那么记 g 为 X 去掉最大的三个数的 \gcd。那么考虑涂蓝 v_3 时的答案:v_3+\gcd(v_1,v_2,g)\le v_3+v_1-v_2 \le v_1,故只需要枚举最大值和次小值即可。

用可持久化线段树可以简单维护出区间删除一个数后的 \gcd,时间复杂度 O(n\log n\log V)。

/*
因为枚举次数上界是我写题解时才强化到现在这个情况的
所以这份代码在找最大值和次大值时有些多余的操作
但我懒得改了qwq
*/
#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define Inf (1ll<<60)
#define For(i,s,t) for(int i=s;i<=t;++i)
#define Down(i,s,t) for(int i=s;i>=t;--i)
#define ls ch[i][0]
#define rs ch[i][1]
#define lowbit(x) ((x)&(-(x)))
#define End {printf("NO\n");exit(0);}
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
inline void ckmx(int &x,int y){x=(x>y)?x:y;}
inline void ckmn(int &x,int y){x=(x<y)?x:y;}
inline void ckmx(ll &x,ll y){x=(x>y)?x:y;}
inline void ckmn(ll &x,ll y){x=(x<y)?x:y;}
inline int min(int x,int y){return x<y?x:y;}
inline int max(int x,int y){return x>y?x:y;}
inline ll min(ll x,ll y){return x<y?x:y;}
inline ll max(ll x,ll y){return x>y?x:y;}
char buf[1<<20],*p1,*p2;
#define gc() (p1 == p2 ? (p2 = buf + fread(p1 = buf, 1, 1 << 20, stdin), p1 == p2 ? EOF : *p1++) : *p1++)
#define read() ({\
    ll x = 0, f = 1;\
    char c = gc();\
    while(c < '0' || c > '9') f = (c == '-') ? -1 : 1, c = gc();\
    while(c >= '0' && c <= '9') x = x * 10 + (c & 15), c = gc();\
    f * x;\
})
const int N=2e5+5,M=70*N;
int n,m,cnt;
ll a[N],b[N];
vector<int> p[N];
ll Mygcd(ll x,ll y){return y ? Mygcd(y,x%y) : x;}
inline ll Gcd(ll x,ll y){return x==1 || y==1 || x==y ? min(x,y) : Mygcd(x,y);}
inline ll F(ll x,ll y){return x!=-1 && y!=-1 ? Gcd(x,y) : max(x,y);}
ll ans[N];
int rt[N],rt1[N],rt2[N],tot,ch[M][2],c[M];
ll g[M];
inline int newnode(int x){
    ++tot;
    ch[tot][0]=ch[x][0],ch[tot][1]=ch[x][1];
    g[tot]=g[x],c[tot]=c[x];
    return tot;
}
void upd(int &i,int ii,int l,int r,int x){
    i=newnode(ii);
    g[i]=F(g[i],b[a[x]]);
    if(l==r) return;
    int mid=(l+r)>>1;
    if(x<=mid) 
        upd(ls,ch[ii][0],l,mid,x);
    else
        upd(rs,ch[ii][1],mid+1,r,x);
}
void upd1(int &i,int ii,int l,int r,int x){
    i=newnode(ii),++c[i];
    if(l==r) return;
    int mid=(l+r)>>1;
    if(x<=mid) 
        upd1(ls,ch[ii][0],l,mid,x);
    else
        upd1(rs,ch[ii][1],mid+1,r,x);
}
ll query_res=-1;
void query(int i,int l,int r,int L,int R){
    if(!i) return;
    if(L<=l && r<=R){
        query_res=F(query_res,g[i]);
        return;
    }
    int mid=(l+r)>>1;
    if(L<=mid) query(ls,l,mid,L,R);
    if(mid<R) query(rs,mid+1,r,L,R);
}
int query1(int i,int ii,int l,int r,int R){
    if(!i || !(c[i]-c[ii])) return -1;
    if(l==r) return l;
    int mid=(l+r)>>1,res=-1;
    if(mid<R) res=query1(rs,ch[ii][1],mid+1,r,R);
    if(res==-1) res=query1(ls,ch[ii][0],l,mid,R);
    return res;
}
void Init(){
    For(i,1,n) b[i]=a[i];
    sort(b+1,b+n+1,less<ll>());
    cnt=unique(b+1,b+n+1)-b-1;
    For(i,1,n) a[i]=lower_bound(b+1,b+cnt+1,a[i])-b,p[a[i]].push_back(i);
    For(i,1,cnt){
        rt[i]=rt[i-1];
        for(int u : p[i])
            upd(rt[i],rt[i],1,n,u);
    }
    Down(i,cnt,1){
        rt1[i]=rt1[i+1];
        for(int u : p[i])
            upd(rt1[i],rt1[i],1,n,u);
    }
    For(i,1,n)
        upd1(rt2[i],rt2[i-1],1,cnt,a[i]);
}
ll calc(int l,int r,int x){
    query_res=-1;
    query(rt[x-1],1,n,l,r);
    query(rt1[x+1],1,n,l,r);
    ll res=query_res;
    return b[x]+(res==-1 ? b[x] : res);
}
int main()
{
#if !ONLINE_JUDGE
    freopen("test.in","r",stdin);
    freopen("test.out","w",stdout);
#endif 
    n=read();
    g[0]=-1;
    For(i,1,n) a[i]=read();
    Init();
    cerr<<tot<<'\n';
    m=read();
    int l,r,x,mx=0;
    ll res=0,G=0;
    For(i,1,m){
        l=read(),r=read();
        res=0;
        x=cnt;
        mx=G=0;
        For(j,1,2){
            x=query1(rt2[r],rt2[l-1],1,cnt,x);
            if(x==-1 || (b[x]<<1)<b[mx] || G==1) break;
            ckmx(res,calc(l,r,x));
            G=(!mx ? b[x] : Gcd(G,b[x]));
            mx=(!mx ? x : mx);
            if(x==1) break;
            --x;
        }
        printf("%lld\n",res);
    }
    return 0;
}