QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#661044 | #9250. Max GCD | _CHO# | TL | 4ms | 9960kb | C++20 | 2.7kb | 2024-10-20 14:27:52 | 2024-10-20 14:27:54 |
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";
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 9960kb
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...