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 "doll.h"
#include <bits/stdc++.h>
#define LOG2(n) (31 - __builtin_clz(n))
#define pii pair<int, int>
using namespace std;
int N, M;
int A[200005];
int tmp[200005];
int order[200005];
int to[200005];
int id[200005];
int X[200005], Y[200005];
int tot;
int cnt = 2;
void gen_order(int l, int r) {
if(l == r) return;
int mid = ((l + r) >> 1) + 1;
for(int i = l; i <= r; i += 2) {
tmp[l + (i - l) / 2] = order[i];
tmp[mid + (i - l) / 2] = order[i + 1];
}
for(int i = l; i <= r; i++) order[i] = tmp[i];
gen_order(l, mid - 1), gen_order(mid, r);
}
int DFS(int l, int r) {
int sz = 0, now;
if(l + 1 == r) {
sz = (to[l] != -1) + (to[r] != -1);
if(sz == 0) return -1;
if(l == 1 && r == tot) now = 1;
else now = cnt++;
X[now] = to[l], Y[now] = to[r];
// cout << -now << " -> " << X[now] << " " << Y[now] << "\n";
return -now;
}
int mid = (l + r) >> 1;
int le = DFS(l, mid), ri = DFS(mid + 1, r);
if(le == -1 && ri == -1) return -1;
if(l == 1 && r == tot) now = 1;
else now = cnt++;
X[now] = le, Y[now] = ri;
// cout << -now << " -> " << X[now] << " " << Y[now] << "\n";
return -now;
}
void solve() {
tot = LOG2(N);
if(N % (1 << tot) != 0) tot++;
// cout << "tot -> " << tot << "\n";
tot = (1 << tot);
for(int i = 1; i <= tot; i++) {
order[i] = i;
}
gen_order(1, tot);
// for(int i = 1; i <= tot; i++) cout << order[i] << " "; cout << "\n";
memset(to, -1, sizeof(to));
memset(tmp, -1, sizeof(tmp));
for(int i = tot; i > tot - N; i--) tmp[order[i]] = 1;
int p = 1;
for(int i = 1; i <= N; i++) {
while(tmp[p] == -1) p++;
tmp[p] = A[i];
// cout << p << " " << A[i] << "\n";
p++;
}
for(int i = 1; i <= tot; i++) {
to[order[i]] = tmp[i];
}
DFS(1, tot);
}
void create_circuit(int m, vector<int> a) {
a.emplace_back(0);
M = m, N = a.size();
for(int i = 1; i <= N; i++) A[i] = a[i - 1];
vector<int> c(M + 1);
for(int i = 0; i <= M; i++) c[i] = -1;
solve();
vector<int> x(cnt - 1), y(cnt - 1);
for(int i = 1; i < cnt; i++) {
x[i - 1] = X[i];
y[i - 1] = Y[i];
}
// system("pause");
answer(c, x, y);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |