先考虑对于一个确定 ,怎么快速求出 。显然的,如果 ,那么 和 的颜色相同不会使答案变劣,因此考虑 中元素两两不同。
这里是一些关于 的小知识:
-
对于任意正整数 ,。
-
对于任意正整数 ,要么 要么 。
基于以上事实,我们认为:存在一个最优方案,使得只将一个数(即所有数值等于某个数的元素)涂成蓝色,其余涂成红色(认为拥有该颜色的元素数量较少的颜色为蓝色)。
证明:考虑先估一个较优值:只将最大值 涂蓝,此时答案为最大值与剩余数 之和。考虑如果最优解需要将不少于两个数涂蓝,那么有如下可能情况:
- 涂蓝的数的 为涂蓝数的最小值 :那么由第一个知识可以得出 ,那么涂红数的 应该大于 。但涂红数的 大于 当且仅当只有 被涂红,与红色元素数量多于蓝色元素数量矛盾。
- 涂蓝的数的 小于涂蓝数最小值 :由性质二可以证明在涂蓝数的数量较涂红数少且不少于两个时,涂蓝数和涂红数的 都不大于 ,和自然也不会大于 。
因此取不少于两个数不会更优,故命题得证。
因此现在只需要找到选取哪个数涂蓝可以最大化答案了:贪心地,我们想选最大值涂蓝,但此时剩余数的 不一定最大。那不妨尝试次大值,次次大值……直接枚举一定会 TLE,考虑分析枚举次数上界:如果当前枚举值小于 :此时一定不优于直接选 ,证明参上;那么枚举的值都是互质的,容易发现无法找到两个数互质且 不为 ,因此只需要枚举最大值和次大值即可。
用可持久化线段树可以简单维护出区间删除一个数后的 ,时间复杂度 。
/*
因为枚举次数上界是我写题解时才强化到现在这个情况的
所以这份代码在找最大值和次大值时有些多余的操作
但我懒得改了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;
}