QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#560178 | #9250. Max GCD | duckindog | TL | 26ms | 7632kb | C++23 | 2.0kb | 2024-09-12 14:03:10 | 2024-09-12 14:03:11 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int N = 150'000 + 10,
MAX = 1'000'000;
int n, q;
int a[N];
struct Query {
int l, r, idx;
friend istream& operator >> (istream& is, auto& rhs) {
return is >> rhs.l >> rhs.r;
}
} query[N];
int answer[N];
vector<int> pos[MAX + 10];
vector<pair<int, int>> save[N];
int bit[N];
void upd(int i, int x) {
for (; i <= n; i += i & -i) bit[i] = max(bit[i], x);
}
int que(int i) {
int ret = 0;
for (; i; i -= i & -i) ret = max(ret, bit[i]);
return ret;
}
int32_t main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> q;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= q; ++i) cin >> query[i], query[i].idx = i;
for (int i = 1; i <= n; ++i) pos[a[i]].push_back(i);
{ //init
set<pair<int, int>> s;
for (int value = MAX; value >= 1; --value) {
vector<int> totalPos;
for (int multiple = value; multiple <= MAX; multiple += value) {
for (const auto& x : pos[multiple]) totalPos.push_back(x);
}
if (totalPos.size() <= 2) continue;
sort(totalPos.begin(), totalPos.end());
for (const auto& x : totalPos) {
if (x == totalPos[0] || x == totalPos.back()) continue;
int l = *(lower_bound(totalPos.begin(), totalPos.end(), x) - 1),
r = lower_bound(totalPos.begin(), totalPos.end(), 2 * x - l) - totalPos.begin();
if (r == totalPos.size()) continue;
r = totalPos[r];
if (!s.count({l, r})) {
s.insert({l, r});
save[l].push_back({r, value});
}
}
}
}
sort(query + 1, query + q + 1, [&](const auto& a, const auto& b) { return a.l < b.l; });
int it = n;
for (int i = q; i >= 1; --i) {
const auto& [l, r, idx] = query[i];
while (it >= l)
for (const auto& [r, value] : save[it--]) upd(r, value);
answer[idx] = que(r);
}
for (int i = 1; i <= q; ++i) cout << answer[i] << "\n";
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 26ms
memory: 7632kb
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...