QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#660919 | #9250. Max GCD | _CHO# | TL | 631ms | 213684kb | C++20 | 2.1kb | 2024-10-20 13:56:19 | 2024-10-20 13:56:20 |
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];
set<int> pos[maxn];
int tagv[maxn<<2],maxv[maxn<<2];
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);
}
main(){
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];
for(int i=1;i<=Q;++i){
int l,r;cin>>l>>r;
qry[r].emplace_back(l,i);
}
for(int i=1;i<=n;++i){
for(int fac:factor[a[i]]) pos[fac].insert(i);
}
for(int i=1;i<maxn;++i) pos[i].insert(0);
for(int r=1;r<=n;++r){
for(int fac:factor[a[r]]){
auto it = --pos[fac].lower_bound(r);
while(it!=pos[fac].begin()){
auto ne = --it; ++ it;
if(ne == pos[fac].begin()) break;
if(*it - *ne <= r - *it){
seg_upd(1,1,n,1,*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: 100
Accepted
time: 631ms
memory: 213684kb
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
Time Limit Exceeded
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...