This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>
using namespace std;
const int N = 5 + 1e5;
const int inf = 7 + 1e9;
struct Segment {
int n;
vector <II> tree;
Segment (int _n = 0) : n(_n) {
tree.resize(4 * n + 1);
}
void build(int l, int r, int node) {
if (l == r) {
tree[node] = {n, l};
return;
}
int mid = (l + r) / 2;
build(l, mid, 2 * node);
build(mid + 1, r, 2 * node + 1);
tree[node] = min(tree[2 * node], tree[2 * node + 1]);
}
void update(int l, int r, int node, int u, int val) {
if (r < u || u < l)
return;
if (l == r) {
tree[node] = {val, l};
return;
}
int mid = (l + r) / 2;
if (u <= mid)
update(l, mid, 2 * node, u, val);
else
update(mid + 1, r, 2 * node + 1, u, val);
tree[node] = min(tree[2 * node], tree[2 * node + 1]);
}
II get(int l, int r, int node, int u, int v) {
if (r < u || v < l)
return {inf, 0};
if (u <= l && r <= v)
return tree[node];
int mid = (l + r) / 2;
return min(get(l, mid, 2 * node, u, v), get(mid + 1, r, 2 * node + 1, u, v));
}
};
int main() {
#define TASKNAME ""
ios_base :: sync_with_stdio (0);
cin.tie (0);
if ( fopen( TASKNAME".inp", "r" ) ) {
freopen (TASKNAME".inp", "r", stdin);
freopen (TASKNAME".out", "w", stdout);
}
int n;
cin >> n;
vector <int> b(n + 1, 0), a(2 * n, 0), vis(2 * n, 0);
vector <vector <int>> cnt(2, vector <int> (2 * n, 0));
Segment f(2 * n - 1);
f.build(1, 2 * n - 1, 1);
for (int i = 1; i <= n; i ++) {
cin >> b[i];
if (!vis[b[i]]) {
f.update(1, 2 * n - 1, 1, b[i], 2 * i - 1);
vis[b[i]] = 1;
}
if (i == 1)
a[i] = b[i];
else {
if (b[i] == b[i - 1]) {
cnt[0][b[i - 1]] ++;
cnt[1][b[i - 1]] ++;
}
else if (b[i] < b[i - 1])
cnt[0][b[i - 1]] += 2;
else
cnt[1][b[i - 1]] += 2;
}
}
vector <int> c1, c2(1, 0);
for (int i = 2; i + 1 < 2 * n; i ++) {
if (cnt[0][i] == i - 1)
c1.push_back(i);
if (cnt[1][i] == 2 * n - i)
c2.push_back(i);
}
c1.push_back(2 * n);
f.update(1, 2 * n - 1, 1, b[1], 2 * n);
for (int i = 2; i <= n; i ++) {
if (b[i] == b[i - 1]) {
int u = lower_bound(c1.begin(), c1.end(), b[i - 1]) - c1.begin(), v = lower_bound(c2.begin(), c2.end(), b[i - 1]) - c2.begin();
if (c2[v] == b[i - 1])
v --;
a[2 * i - 1] = f.get(1, 2 * n - 1, 1, c2[v] + 1, c1[u] - 1).nd;
f.update(1, 2 * n - 1, 1, a[2 * i - 1], 2 * n);
u = lower_bound(c1.begin(), c1.end(), b[i - 1]) - c1.begin(); v = lower_bound(c2.begin(), c2.end(), b[i - 1]) - c2.begin();
if (c1[u] == b[i - 1])
u ++;
a[2 * i - 2] = f.get(1, 2 * n - 1, 1, c2[v] + 1, c1[u] - 1).nd;
f.update(1, 2 * n - 1, 1, a[2 * i - 2], 2 * n);
}
else if (b[i] < b[i - 1]) {
int u = lower_bound(c1.begin(), c1.end(), b[i - 1]) - c1.begin(), v = lower_bound(c2.begin(), c2.end(), b[i - 1]) - c2.begin();
if (c2[v] == b[i - 1])
v --;
a[2 * i - 1] = f.get(1, 2 * n - 1, 1, c2[v] + 1, c1[u] - 1).nd;
f.update(1, 2 * n - 1, 1, a[2 * i - 1], 2 * n);
a[2 * i - 2] = f.get(1, 2 * n - 1, 1, c2[v] + 1, c1[u] - 1).nd;
f.update(1, 2 * n - 1, 1, a[2 * i - 2], 2 * n);
}
else {
int u = lower_bound(c1.begin(), c1.end(), b[i - 1]) - c1.begin(), v = lower_bound(c2.begin(), c2.end(), b[i - 1]) - c2.begin();
if (c1[u] == b[i - 1])
u ++;
a[2 * i - 1] = f.get(1, 2 * n - 1, 1, c2[v] + 1, c1[u] - 1).nd;
f.update(1, 2 * n - 1, 1, a[2 * i - 1], 2 * n);
a[2 * i - 2] = f.get(1, 2 * n - 1, 1, c2[v] + 1, c1[u] - 1).nd;
f.update(1, 2 * n - 1, 1, a[2 * i - 2], 2 * n);
}
}
for (int i = 1; i < 2 * n; i ++)
cout << a[i] << ' ';
return 0;
}
Compilation message (stderr)
medians.cpp: In function 'int main()':
medians.cpp:59:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | freopen (TASKNAME".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
medians.cpp:60:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen (TASKNAME".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |