QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#661465 | #9250. Max GCD | lllei# | TL | 4ms | 9808kb | C++20 | 3.1kb | 2024-10-20 16:21:50 | 2024-10-20 16:21:51 |
Judging History
answer
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,tune=native,popcnt,lzcnt,abm,bmi,bmi2,fma")
using namespace __gnu_pbds;
using namespace std;
using LL = long long;
struct MyHash {
LL operator()(pair<int, int> x) const {
return ((1LL * x.first) << 31) + x.second;
}
};
const int N = 1e6 + 5;
int n, q;
int a[N];
vector<int> P[N];
struct qr {
int x, y, v, type;
} node[N * 36], tmp[N * 36];
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 = (int)(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);
}
}
}
gp_hash_table<pair<int, int>, int, MyHash> table;
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) {
table[{P[i][j], P[i][r]}] = max(table[{P[i][j], P[i][r]}], i);
// cout<<P[i][j]<<' '<<P[i][j+1]<<' '<<i<<' '<<P[i][r]<<'\n';
// node[++m] = {P[i][j], P[i][r], i, 0};
}
}
}
for (const auto [x, y] : table) {
node[++m] = {x.first, x.second, y, 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: 9808kb
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...