QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#618642 | #9250. Max GCD | ucup-team1766 | TL | 3ms | 6892kb | C++20 | 2.5kb | 2024-10-07 01:46:51 | 2024-10-07 01:46:52 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const int MAXV = 1000005;
vector<int> inds[MAXV];
vector<int> d_prev(MAXV, -1);
struct Tree {
typedef int T;
static constexpr T unit = 0;
T f(T a, T b) { return max(a, b); } // (any associative fn)
vector<T> s; int n;
Tree(int n = 0, T def = unit) : s(2*n, def), n(n) {}
void update(int pos, T val) {
pos += n;
for (s[pos] = f(s[pos], val); pos /= 2;)
s[pos] = f(s[pos * 2], s[pos * 2 + 1]);
}
T query(int b, int e) { // query [b, e)
T ra = unit, rb = unit;
for (b += n, e += n; b < e; b /= 2, e /= 2) {
if (b % 2) ra = f(ra, s[b++]);
if (e % 2) rb = f(s[--e], rb);
}
return f(ra, rb);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, q;
cin >> n >> q;
vector<int> a(n);
vector<vector<int>> divisors(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
for (int j = 1; j * j <= a[i]; j++) {
if (a[i] % j == 0) {
divisors[i].push_back(j);
inds[j].push_back(i);
if (j * j < a[i]) {
int other = a[i] / j;
divisors[i].push_back(other);
inds[other].push_back(i);
}
}
}
}
vector<vector<pair<int, int>>> intervals(n);
for (int i = 0; i < n; i++) {
for (int divisor : divisors[i]) {
if (d_prev[divisor] != -1) {
auto next_ind = lower_bound(inds[divisor].begin(), inds[divisor].end(), i + (i - d_prev[divisor]));
if (next_ind != inds[divisor].end()) {
intervals[*next_ind].emplace_back(d_prev[divisor], divisor);
}
}
d_prev[divisor] = i;
}
}
vector<tuple<int, int, int>> queries(q);
for (int i = 0; i < q; i++) {
int l, r;
cin >> l >> r;
queries[i] = {r - 1, l - 1, i};
}
sort(queries.begin(), queries.end());
Tree st(n);
vector<int> res(n);
int p = 0;
for (int i = 0; i < n; i++) {
for (auto [l, div] : intervals[i]) {
st.update(l, div);
}
while (p < n && get<0>(queries[p]) == i) {
res[get<2>(queries[p])] = st.query(get<1>(queries[p]), i + 1);
p++;
}
}
for (int i = 0; i < n; i++) {
cout << res[i] << "\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 6892kb
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...