# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
647101 | georgievskiy | popa (BOI18_popa) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #define LOCAL
#ifndef LOCAL
#include "popa.h"
#endif
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
int ARR[100], LEFT[100], RIGHT[100];
int f_gcd(int a, int b) {
int g = 0;
for (int i = a; i <= b; i++)
g = __gcd(g, ARR[i]);
return g;
}
int query(int a, int b, int c, int d) {
return f_gcd(a, b) == f_gcd(c, d);
}
#endif
int rec(int l, int r, int* left, int* right, vector<int>& dl, vector<int>& dr) {
if (l >= r)
return -1;
if (l + 1 == r)
return l;
int root = -1;
for (int i = l; i < r; i++) {
if (dl[i] <= l && dr[i] >= r - 1) {
root = i;
break;
}
}
assert(root >= l && root < r);
left[root] = rec(l, root, left, right);
right[root] = rec(root + 1, r, left, right);
return root;
}
int solve(int n, int* left, int* right) {
fill(left, left + n, -1), fill(right, right + n, -1);
vector<int> dl(n), dr(n);
for (int i = 0; i < n; i++) {
{
int l = -1, r = i;
while (r - l > 1) {
int c = (l + r) / 2;
if (query(c, i, i, i))
r = c;
else
l = c;
}
dl[i] = r;
}
{
int l = i, r = n;
while (r - l > 1) {
int c = (l + r) / 2;
if (query(i, c, i, i))
l = c;
else
r = c;
}
dr[i] = l;
}
}
return rec(0, n, left, right);
}
#ifdef LOCAL
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) cin >> ARR[i];
solve(n, LEFT, RIGHT);
for (int i = 0; i < n; i++)
cout << LEFT[i] << " ";
cout << "\n";
for (int i = 0; i < n; i++)
cout << RIGHT[i] << " ";
cout << "\n";
return 0;
}
#endif