QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#792451 | #9250. Max GCD | _Strelitzia | TL | 116ms | 677568kb | C++23 | 2.0kb | 2024-11-29 10:20:51 | 2024-11-29 10:20:52 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
const int M = 1e6 + 5;
int n, m, a[N], ans[N];
bool flag[M];
vector<int> fac[M];
deque<int> d[M];
priority_queue<int, vector<int> > v;
int pos[N];
struct Node {
int l, r, k;
bool operator<(Node B) {
if (pos[l] != pos[B.l]) {
return pos[l] < pos[B.l];
}
if (pos[l] & 1) {
return r > B.r;
}
return r < B.r;
}
}q[N];
void add(int x) {
for (auto y : fac[a[x]]) {
if (d[y].empty() || x > d[y].back()) {
d[y].push_back(x);
} else {
d[y].push_front(x);
}
if (flag[y] || d[y].size() < 3) {
continue;
}
if (d[y][1] - d[y].front() <= d[y].back() - d[y][1]) {
flag[y] = 1;
v.push(y);
}
}
}
void del(int x) {
for (auto y : fac[a[x]]) {
if (d[y].back() == x) {
d[y].pop_back();
} else {
d[y].pop_front();
}
}
}
void solve() {
cin>>n>>m;
int block = sqrt(n);
for (int i = 1; i <= n; i++) {
cin>>a[i];
pos[i] = (i - 1) / block + 1;
if (fac[a[i]].empty()) {
int top = sqrt(a[i]);
for (int j = 1; j <= top; j++) {
if (a[i] % j == 0) {
fac[a[i]].push_back(j);
if (j != a[i] / j) {
fac[a[i]].push_back(a[i] / j);
}
}
}
}
}
for (int i = 1; i <= m; i++) {
cin>>q[i].l>>q[i].r;
q[i].k = i;
}
sort(q + 1, q + m + 1);
int l = q[1].l, r = q[1].r;
for (int i = l; i <= r; i++) {
add(i);
}
for (int i = 1; i <= m; i++) {
while (l > q[i].l) add(--l);
while (r < q[i].r) add(++r);
while (l < q[i].l) del(l++);
while (r > q[i].r) del(r--);
while (!v.empty()) {
int t = v.top();
if (d[t].size() < 3 || d[t][1] - d[t].front() > d[t].back() - d[t][1]) {
flag[t] = 0;
v.pop();
} else {
break;
}
}
if (v.empty()) {
ans[q[i].k] = 0;
} else {
ans[q[i].k] = v.top();
}
}
for (int i = 1; i <= m; i++) {
cout<<ans[i]<<'\n';
}
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T = 1;
// cin>>T;
while (T--) {
solve();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 116ms
memory: 677568kb
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...