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>
using namespace std;
#define len(a) ((int)(a).size())
#define pii pair<int, int>
#define fr first
#define sc second
int n, avl, logn;
vector<int> X, Y;
vector<pii> HRT;
int CNTR = -1, S = 0;
bool dfs(int v, int x, int y, int h){
if(h >= logn - 1){
HRT[y] = {v, x};
HRT[y + (1 << (logn - 1))] = {v, x + 1};
if(x + 1 < avl) return false;
if(x < avl) X[-v - 1] = -1;
S++;
return true;
}
int rch = --CNTR;
if(!dfs(rch, x + (1 << (logn - h - 1)), y + (1 << h), h + 1)) return false;
Y[-v - 1] = rch;
int lch = --CNTR;
if(!dfs(lch, x, y, h + 1)){
X[-v - 1] = -1;
S++;
return true;
}
X[-v - 1] = lch;
S++;
return true;
}
void create_circuit(int M, vector<int> A) {
n = A.size();
vector<int> C(M + 1);
C[0] = A[0];
for (int i = 1; i <= M; ++i) C[i] = -1;
logn = ceil(log2(n));
avl = (1 << logn) - n;
X.resize(n + logn);
Y.resize(n + logn);
HRT.resize(2 * (n + logn));
dfs(-1, 0, 0, 0);
X.resize(S);
Y.resize(S);
if(n == 1){
C[A[0]] = 0;
answer(C, X, Y);
return;
}
// cout << avl << "\n";
int avlcnt = 0;
for(int i = 0, v, x, ind; i < len(HRT); i++){
// cout << "(" << HRT[i].fr << ", " << HRT[i].sc << ") ";
v = -HRT[i].fr - 1;
x = HRT[i].sc;
if(x < avl){
avlcnt++;
continue;
}
ind = i - avlcnt + 1;
if(x % 2) Y[v] = (ind == n) ? 0 : A[ind];
else X[v] = (ind == n) ? 0 : A[ind];
}
// cout << endl;
// for(auto h : C) cout << h << " ";
// cout << endl << endl;
// for(auto h : X) cout << h << " ";
// cout << endl;
// for(auto h : Y) cout << h << " ";
// cout << endl;
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... |