Author of the problem: | Matvii Hrytsyna |
Prepared by: | Matvii Hrytsyna, Konstantyn Denysov |
Solution: | Matvii Hrytsyna |
Idea
Let us define an array — the largest among all minimal left indices of triples, where is the right element. In other words, \text{lft}[k] = i, where (i,j,k) is some valid triple and is the largest index among all valid triples that have right index . Also, for each such , we store an array \text{ans}[k] = (j,k) — a specific pair of indices that forms a valid triple together with . Among all such pairs, we keep the one with the smallest value of .
We also build an array , where
Then for a query (l,r), an answer exists if and only if \text{pref\_max}[r] \ge l.
How to compute the array
We iterate over the array from left to right. For each , we maintain the array of its prime divisors. We also maintain, for each prime , the two last indices where divides :
— the nearest previous index;
— the second nearest previous index.
\text{lst}[i][p] = -1, if such an index does not exist.
Consider two cases.
Case 1: The gcd of all three numbers is
In this case, has a common prime divisor with both and . We iterate over , and if \text{lst}[1][p] \neq -1, i.e. there are at least two numbers divisible by to the left of index , we update
Case 2: The gcd of the three numbers is
Suppose that has a common prime divisor with and a common prime divisor with . In this case, for each prime we can take and check whether the gcd of the numbers at these positions is greater than . That is, if
and
then we update
How to answer queries
For an answer to exist for a query (l,r), for some l \le k \le r the condition \text{lft}[k] \ge l must hold. In other words, an answer exists if and only if
Since for k < l we have \text{lft}[k] < l, instead of the maximum on a segment we can take the maximum on a prefix. Let idx = \text{pref\_max}[r]. If idx < l, then there is no answer; otherwise, as the triple we can take
Complexity
Using the Sieve of Eratosthenes, we can precompute all prime divisors for numbers \le A = 2 \cdot 10^5 in O(A \log A). We compute the array in O(n d^2 \log A), where is the maximum number of prime divisors of a number. Under the given constraints, d \le 6, so this easily fits within the limits. We answer each query in .
Thus, the total complexity is O(A \log A + n d^2 \log A + q).
#include <bits/stdc++.h>
using namespace std;
const int MAXA = 200010;
const int MAXN = 400000;
vector<int> primes[MAXA];
void sieve() {
for (int i = 2; i <= MAXA; i++)
if (primes[i].empty()) {
for (int j = i; j < MAXA; j += i)
primes[j].push_back(i);
}
}
int main() {
sieve();
int n;
cin >> n;
vector<int> a(n);
for (auto& ai : a)
cin >> ai;
vector<pair<int, int>> ans(n, {-1, -1});
vector<int> pref_max(n + 1, -1), lft(n, -1);
vector<vector<int>> lst(2, vector<int>(MAXA, -1));
for (int i = 0; i < n; i++) {
int x = a[i];
for (auto p : primes[x])
if (lst[1][p] != -1) {
lft[i] = max(lft[i], lst[1][p]);
if (ans[lst[1][p]].first == -1)
ans[lst[1][p]] = {lst[0][p], i};
}
for (auto p1 : primes[x])
for (auto p2 : primes[x]) {
if (p1 == p2 || lst[0][p1] == lst[0][p2])
continue;
if (lst[0][p1] >= 0 && lst[0][p2] >= 0 &&
__gcd(a[lst[0][p1]], a[lst[0][p2]]) > 1 &&
min(lst[0][p1], lst[0][p2]) > lft[i]) {
lft[i] = min(lst[0][p1], lst[0][p2]);
if (ans[min(lst[0][p1], lst[0][p2])].first == -1)
ans[min(lst[0][p1], lst[0][p2])] =
{max(lst[0][p1], lst[0][p2]), i};
}
}
for (auto p : primes[x]) {
lst[1][p] = lst[0][p];
lst[0][p] = i;
}
}
for (int i = 1; i < n; i++)
pref_max[i] = max(pref_max[i - 1], lft[i]);
int q;
cin >> q;
while (q--) {
int l, r;
cin >> l >> r;
--l, --r;
int idx = pref_max[r];
if (idx < l) {
cout << -1 << '\n';
} else {
cout << ans[idx].first + 1 << ' '
<< ans[idx].second + 1 << ' '
<< idx + 1 << '\n';
}
}
}