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.assign(4 * n + 1, {inf, inf});
}
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), ma(2 * n, 2 * n - 1);
vector <vector <int>> cnt(2, vector <int> (2 * n, 0));
vector <vector <II>> open(2 * n);
Segment f(2 * n - 1);
for (int i = 1; i <= n; i ++) {
cin >> b[i];
if (!vis[b[i]]) {
ma[b[i]] = 2 * i - 1;
vis[b[i]] = i;
}
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;
if (b[i] + 1 < b[i - 1])
open[b[i] + 1].push_back({i, b[i - 1] - 1});
}
else {
cnt[1][b[i - 1]] += 2;
if (b[i - 1] + 1 < b[i])
open[b[i - 1] + 1].push_back({i, b[i] - 1});
}
}
}
vector <vector <int>> ins(2 * n);
priority_queue <II> q;
for (int i = 1; i < 2 * n; i ++) {
for (auto z : open[i])
q.push(z);
while (!q.empty() && q.top().nd < i)
q.pop();
if (!q.empty())
ins[q.top().st + 1].push_back(i);
else
ins[1].push_back(i);
}
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);
vis.assign(2 * n, 0);
for (int u : ins[1])
f.update(1, 2 * n - 1, 1, u, ma[u]);
f.update(1, 2 * n - 1, 1, b[1], 2 * n);
vis[b[1]] = 1;
for (int i = 2; i <= n; i ++) {
for (int u : ins[i])
f.update(1, 2 * n - 1, 1, u, ma[u]);
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 --;
int l = c2[v] + 1, r = c1[u] - 1;
if (r >= b[i - 1])
r = b[i - 1] - 1;
a[2 * i - 1] = f.get(1, 2 * n - 1, 1, l, r).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 ++;
l = c2[v] + 1; r = c1[u] - 1;
if (l <= b[i - 1])
l = b[i - 1] + 1;
a[2 * i - 2] = f.get(1, 2 * n - 1, 1, l, r).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]) - c1.begin(), v = lower_bound(c2.begin(), c2.end(), b[i]) - c2.begin();
if (c2[v] == b[i])
v --;
int l = c2[v] + 1, r = c1[u] - 1;
if (r >= b[i])
r = b[i] - 1;
if (vis[b[i]])
a[2 * i - 1] = f.get(1, 2 * n - 1, 1, l, r).nd;
else
a[2 * i - 1] = b[i];
f.update(1, 2 * n - 1, 1, a[2 * i - 1], 2 * n);
a[2 * i - 2] = f.get(1, 2 * n - 1, 1, l, r).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]) - c1.begin(), v = lower_bound(c2.begin(), c2.end(), b[i]) - c2.begin();
if (c1[u] == b[i])
u ++;
int l = c2[v] + 1, r = c1[u] - 1;
if (l <= b[i])
l = b[i] + 1;
if (vis[b[i]])
a[2 * i - 1] = f.get(1, 2 * n - 1, 1, l, r).nd;
else
a[2 * i - 1] = b[i];
f.update(1, 2 * n - 1, 1, a[2 * i - 1], 2 * n);
a[2 * i - 2] = f.get(1, 2 * n - 1, 1, l, r).nd;
f.update(1, 2 * n - 1, 1, a[2 * i - 2], 2 * n);
}
vis[a[2 * i - 1]] = vis[a[2 * i - 2]] = 1;
}
for (int i = 1; i < 2 * n; i ++)
cout << a[i] << ' ';
return 0;
}
Compilation message (stderr)
medians.cpp: In function 'int main()':
medians.cpp:49:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | freopen (TASKNAME".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
medians.cpp:50:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | freopen (TASKNAME".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |