QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#661418 | #9250. Max GCD | lllei# | RE | 4ms | 9760kb | C++20 | 2.2kb | 2024-10-20 16:08:27 | 2024-10-20 16:08:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
int n,q;
int a[N];
vector<int>P[N];
struct qr
{
int x,y,v,type;
}node[N*30],tmp[N*30];
int ans[N];
void cdq(int l,int r)
{
if(l==r)return;
int mid=l+r>>1;
cdq(l,mid);
cdq(mid+1,r);
int p=l,q=mid+1;
int mx=0,t=0;
while(p<=mid&&q<=r)
{
if(node[p].x>=node[q].x)
{
if(node[p].type==0)mx=max(mx,node[p].v);
tmp[++t]=node[p];
p++;
}
else if(node[p].x<node[q].x)
{
if(node[q].type==1)ans[node[q].v]=max(ans[node[q].v],mx);
tmp[++t]=node[q];
q++;
}
}
while(p<=mid)
{
if(node[p].type==0)mx=max(mx,node[p].v);
tmp[++t]=node[p];
p++;
}
while(q<=r)
{
if(node[q].type==1)ans[node[q].v]=max(ans[node[q].v],mx);
tmp[++t]=node[q];
q++;
}
for(int i=l;i<=r;i++)node[i]=tmp[i-l+1];
}
int m;
int query(const vector<int>&v,int p)
{
int l=0,r=v.size()-1,ans=-1;
while(l<=r)
{
int mid=l+r>>1;
if(v[mid]>=p)ans=mid,r=mid-1;
else l=mid+1;
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++)cin>>a[i];
for(int i=1;i<=n;i++)
{
int lim=sqrt(a[i]);
for(int j=1;j<=lim;j++)
{
if(a[i]%j==0)
{
P[j].push_back(i);
if(j*j!=a[i])P[a[i]/j].push_back(i);
}
}
}
for(int i=1;i<=1000000;i++)
{
int lim=P[i].size();
for(int j=0;j<lim-1;j++)
{
int t=P[i][j+1]*2-P[i][j];
int r=query(P[i],t);
if(r!=-1)
{
//cout<<P[i][j]<<' '<<P[i][j+1]<<' '<<i<<' '<<P[i][r]<<'\n';
node[++m]={P[i][j],P[i][r],i,0};
}
}
}
for(int i=1;i<=q;i++)
{
int l,r;
cin>>l>>r;
node[++m]={l,r,i,1};
}
sort(node+1,node+m+1,[](const qr&a,const qr&b){return a.y==b.y?(a.x==b.x?a.type<b.type:a.x>b.x):a.y<b.y;});
cdq(1,m);
for(int i=1;i<=q;i++)cout<<ans[i]<<'\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 9760kb
input:
8 8 8 24 4 6 6 7 3 3 1 5 2 6 3 7 5 8 4 8 1 3 2 5 3 8
output:
4 2 3 1 3 4 2 3
result:
ok 8 tokens
Test #2:
score: -100
Runtime Error
input:
150000 100000 982800 554400 665280 982800 997920 720720 786240 831600 997920 982800 786240 982800 942480 831600 887040 665280 831600 960960 786240 982800 786240 942480 665280 831600 942480 665280 982800 960960 960960 997920 720720 960960 960960 665280 982800 665280 982800 942480 786240 997920 554400...