QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#696547 | #9250. Max GCD | lllei | ML | 0ms | 34556kb | C++20 | 3.0kb | 2024-10-31 23:20:42 | 2024-10-31 23:20:42 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
constexpr int N = 1e6 + 1;
vector<vector<array<int, 2>>> stk(N);
vector<int> lp(N, 0);
vector<int> pre(N, -1);
// vector<int> lst(N, -1);
vector<array<int, 3>> a;
auto handle = [&](int x, int y) {
while (lp[x] < stk[x].size() && y >= stk[x][lp[x]][0]) {
lp[x]++;
}
// if (lp[x] >= 1 && lst[x] != stk[x][lp[x] - 1][1]) {
// a.push_back({stk[x][lp[x] - 1][1], y, x});
// }
if (lp[x] >= 1) {
a.push_back({stk[x][lp[x] - 1][1], y, x});
}
if (pre[x] != -1) {
int now = 2 * y - pre[x];
if (stk[x].size() && now <= stk[x].back()[0]) {
stk[x].pop_back();
}
stk[x].push_back({now, pre[x]});
lp[x] = min(lp[x], int(stk[x].size()));
}
pre[x] = y;
};
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
for (int j = 1; j * j <= x; ++j) {
if (x % j == 0) {
handle(j, i);
if (j * j != x) {
handle(x / j, i);
}
}
}
}
int S = sqrt(n);
if (S <= 0) {
S = 1;
}
int cnt = (n + S - 1) / S;
vector<int> L(cnt), R(cnt), belong(n);
vector<int> blk(cnt), c(n);
for (int i= 0; i < n; ++i) {
belong[i] = i / S;
}
for (int i = 0; i < cnt; ++i) {
L[i] = i * S;
R[i] = min((i + 1) * S - 1, n - 1);
}
vector<array<int, 3>> b;
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
--l, --r;
b.push_back({l, r, i});
}
vector<int> ans(q);
sort(b.begin(), b.end(), [&](const auto &a, const auto &b) {
return a[1] < b[1];
});
int p = 0;
auto add = [&](int x, int y) {
c[x] = max(c[x], y);
blk[belong[x]] = max(blk[belong[x]], y);
};
auto query = [&](int l, int r) {
int ll = belong[l], rr = belong[r];
if (ll == rr) {
int ans = 0;
for (int i = l; i <= r; ++i) {
ans = max(ans, c[i]);
}
return ans;
}
int ans = 0;
for (int i = l; i <= R[ll]; ++i) {
ans = max(ans, c[i]);
}
for (int i = L[rr]; i <= r; ++i) {
ans = max(ans, c[i]);
}
for (int i = ll + 1; i <= rr - 1; ++i) {
ans = max(ans, blk[i]);
}
return ans;
};
for (int i = 0; i < q; ++i) {
auto [l, r, id] = b[i];
while (p < int(a.size()) && a[p][1] <= r) {
add(a[p][0], a[p][2]);
p++;
}
ans[id] = query(l, r);
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << '\n';
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 34556kb
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
Memory 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...
output:
997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920 997920...