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>
using namespace std;
#define MAX 602020
int l[MAX], r[MAX];
int tree[MAX];
int arr[MAX];
int s;
vector<int>ans;
void init(int x = 1) {
if (x >= s) {
l[x] = r[x] = x - s + 1;
return;
}
init(x * 2);
init(x * 2 + 1);
l[x] = l[x * 2];
r[x] = r[x * 2 + 1];
}
void update(int loc) {
if (loc == 0) return;
tree[loc]++;
update(loc / 2);
}
int sumquery(int low, int high, int loc = 1) {
if (l[loc] == low && r[loc] == high) return tree[loc];
else if (r[loc * 2] >= high) return sumquery(low, high, loc * 2);
else if (l[loc * 2 + 1] <= low) return sumquery(low, high, loc * 2 + 1);
else return sumquery(low, r[loc * 2], loc * 2) + sumquery(l[loc * 2 + 1], high, loc * 2 + 1);
}
int main(void) {
int N;
cin >> N;
s = 1 << (int)ceil(log2(2 * N - 1));
int i;
init();
int a;
cin >> a;
ans.push_back(a);
update(a);
arr[a] = 1;
int p, q;
p = 1;
q = 2 * N - 1;
for (i = 1; i <= N - 1; i++) {
cin >> a;
if (arr[a] == 0) {
ans.push_back(a);
update(a);
arr[a] = 1;
if (sumquery(1, a) < sumquery(a, 2 * N - 1)) {
while (arr[p] == 1) p++;
ans.push_back(p);
update(p + s - 1);
arr[p] = 1;
}
else {
while (arr[q] == 1) q--;
ans.push_back(q);
update(q + s - 1);
arr[q] = 1;
}
}
else {
if (sumquery(1, a) < sumquery(a, 2 * N - 1)) {
while (arr[p] == 1) p++;
ans.push_back(p);
update(p + s - 1);
arr[p] = 1;
while (arr[p] == 1) p++;
ans.push_back(p);
update(p + s - 1);
arr[p] = 1;
}
else if (sumquery(1, a) > sumquery(a, 2 * N - 1)) {
while (arr[q] == 1) q--;
ans.push_back(q);
update(q + s - 1);
arr[q] = 1;
while (arr[q] == 1) q--;
ans.push_back(q);
update(q + s - 1);
arr[q] = 1;
}
else {
while (arr[p] == 1) p++;
ans.push_back(p);
update(p + s - 1);
arr[p] = 1;
while (arr[q] == 1) q--;
ans.push_back(q);
update(q + s - 1);
arr[q] = 1;
}
}
}
for (auto i : ans) cout << i << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |