QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#661039 | #9250. Max GCD | _CHO# | WA | 0ms | 9796kb | C++20 | 2.7kb | 2024-10-20 14:26:31 | 2024-10-20 14:26:31 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using pii = pair<int,int>;
const int maxn = 1e6+100;
vector<int> factor[maxn];
void Sieve(){
for(int i=1;i<maxn;++i){
for(int j=i;j<maxn;j+=i) factor[j].push_back(i);
}
}
int n,Q;
int a[maxn];
vector<pii> qry[maxn];
int ans[maxn];
vector<int> pos[maxn];
int ptr[maxn];
int tagv[maxn],maxv[maxn];
void pushtag(int o,int val){
maxv[o] = max(maxv[o],val);
tagv[o] = max(tagv[o],val);
}
void pushdown(int o){
pushtag(o<<1,tagv[o]);
pushtag(o<<1|1,tagv[o]);
tagv[o] = 0;
}
void seg_upd(int o,int l,int r,int ql,int qr,int val){
if(ql<=l && r<=qr){
pushtag(o,val);
return ;
}
pushdown(o);
int mid=l+r>>1;
if(ql<=mid) seg_upd(o<<1,l,mid,ql,qr,val);
if(qr>mid) seg_upd(o<<1|1,mid+1,r,ql,qr,val);
maxv[o] = max(maxv[o<<1],maxv[o<<1|1]);
}
int seg_max(int o,int l,int r,int q){
if(l==r) return maxv[o];
pushdown(o);
int mid= l+r>>1,res=0;
if(q<=mid) return seg_max(o<<1,l,mid,q);
else return seg_max(o<<1|1,mid+1,r,q);
}
mt19937 rnd(time(0));
main(){
srand(time(0));
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(0);
// Sieve();
cin>>n>>Q;
for(int i=1;i<=n;++i){
cin >> a[i];
// a[i] = rnd()%1000000+1;
if(factor[a[i]].empty())for(int d=1;d*d<=a[i];++d){
if(a[i]%d == 0){
factor[a[i]].push_back(d);
if(d*d!=a[i]) factor[a[i]].push_back(a[i]/d);
}
}
// a[i] = rand()+1;
}
for(int i=1;i<=Q;++i){
int l,r;
cin>>l>>r;
// l=rnd()%n+1,r=rnd()%n+1; if(l>r) swap(l,r);
qry[r].emplace_back(l,i);
}
// for(int i=1;i<maxn;++i) pos[i].push_back(0);
for(int i=1;i<=n;++i){
for(int fac:factor[a[i]]){
if(pos[fac].empty()) pos[fac].push_back(0);
pos[fac].push_back(i);
}
}
for(int r=1;r<=n;++r){
for(int fac:factor[a[r]]){
// auto it = --pos[fac].lower_bound(r);
// auto it = lower_bound(pos[fac].begin(),pos[fac].end(),r) - 1;
int it = ++ptr[fac];
while(it>0){
auto ne = it-1;
if(ne == 0) break;
if(pos[fac][it] - pos[fac][ne] <= r - pos[fac][it]){
seg_upd(1,1,n,1,pos[fac][ne],fac);
break;
}
--it;
}
}
for(auto[l,idx]:qry[r]){
ans[idx]=seg_max(1,1,n,l);
}
}
// for(int i=1;i<=Q;++i) cout<<ans[i]<<'\n';
// cerr<<"!!!\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 9796kb
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:
result:
wrong answer Unexpected EOF in the participants output