QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#551931 | #9250. Max GCD | ucup-team3519# | TL | 1ms | 7880kb | C++20 | 2.8kb | 2024-09-07 19:11:22 | 2024-09-07 19:11:22 |
Judging History
answer
#include <bits/stdc++.h>
constexpr int MAXS1 = 250 * 1.5e5 + 1e6 * 23;
constexpr int MAXS2 = 250 * 1.5e5;
constexpr int MAXN = 1.5e5 + 19;
constexpr int MAXA = 1e6 + 19;
struct Edge {
int to, next;
} edge[MAXS1];
int edge_tot;
void insert(int &h, int x) {
++edge_tot;
edge[edge_tot].to = x;
edge[edge_tot].next = h;
h = edge_tot;
}
int divs[MAXA], pos[MAXA];
struct Page {
int r, v, next;
} page[MAXS2];
int page_tot;
int interv[MAXN];
void insert(int &h, int r, int v) {
++page_tot;
page[page_tot].r = r;
page[page_tot].v = v;
page[page_tot].next = h;
h = page_tot;
}
int n, q;
int a[MAXN];
struct BIT {
int tr[MAXN];
void update(int x, int k) {
for (; x <= n; x += x & -x) {
tr[x] = std::max(tr[x], k);
}
}
int query(int x) {
int res = 0;
for (; x; x -= x & -x) {
res = std::max(res, tr[x]);
}
return res;
}
} bit;
struct Query {
int l, r, id;
} query[MAXN];
int ans[MAXN];
int main() {
std::cin >> n >> q;
int L = 0;
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
L = std::max(L, a[i]);
}
for (int d = 1; d <= L; ++d) {
for (int i = d; i <= L; i += d) {
insert(divs[i], d);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = divs[a[i]]; j; j = edge[j].next) {
int d = edge[j].to;
insert(pos[d], i);
}
}
for (int i = 1; i <= L; ++i) {
// std::cout << "pos[" << i << "]: ";
std::vector<int> p;
for (int j = pos[i]; j; j = edge[j].next) {
p.push_back(edge[j].to);
}
std::reverse(p.begin(), p.end());
int last = p.size() - 1;
for (int j = p.size() - 3; j >= 0; --j) {
int x = p[j], y = p[j + 1];
while (last - 1 > j + 1 && y - x <= p[last - 1] - y) {
--last;
}
if (y - x <= p[last] - y) {
insert(interv[x], p[last], i);
}
}
}
if (n > 1e4) {
exit(-1);
}
for (int i = 1; i <= q; ++i) {
std::cin >> query[i].l >> query[i].r;
query[i].id = i;
}
std::sort(query + 1, query + 1 + q, [](const auto &x, const auto &y) {
return x.l > y.l;
});
int now = n + 1;
for (int _ = 1; _ <= q; ++_) {
auto [l, r, id] = query[_];
while (now > l) {
--now;
for (int i = interv[now]; i; i = page[i].next) {
int r = page[i].r, v = page[i].v;
bit.update(r, v);
}
}
ans[id] = bit.query(r);
}
for (int i = 1; i <= q; ++i) {
std::cout << ans[i] << '\n';
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7880kb
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...