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;
const int M = 1e5+10;
const int N = 2e5+10;
int m, n;
vector<int> C, X, Y;
int newnode, root[N], previousRoot;
int le[N + N], ri[N + N], parity[N + N];
vector<int> neighbour[M];
void build(int u, int L, int R) {
if(L == R) return;
int mid = L + R >> 1;
le[u] = ++newnode;
ri[u] = ++newnode;
build(le[u], L, mid);
build(ri[u], mid + 1, R);
}
void assign(int u, int to) {
if(parity[u] & 1) {
parity[u] ^= 1;
if(ri[u]) assign(ri[u], to);
else ri[u] = to;
}
else {
parity[u] ^= 1;
if(le[u]) assign(le[u], to);
else le[u] = to;
}
}
void process(int val) {
int sz = neighbour[val].size();
if(sz & 1) {
root[val] = ++newnode;
build(root[val], 1, (sz + 1) / 2);
for(int i = 0; i < sz; i++)
assign(root[val], -neighbour[val][i]);
}
else {
root[val] = ++newnode;
build(root[val], 1, (sz + 2) / 2);
for(int i = 0; i < sz; i++)
assign(root[val], -neighbour[val][i]);
assign(root[val], root[val]);
}
if(previousRoot) {
assign(root[val], previousRoot);
previousRoot = root[val];
}
else {
assign(root[val], 0);
previousRoot = root[val];
}
}
void create_circuit(int M, std::vector<int> A) {
n = A.size(), m = M;
int processLast = A[n - 1];
for(int i = 0; i + 1 < n; i++) {
neighbour[A[i]].push_back(A[i + 1]);
}
C.resize(m + 1);
C[0] = A[0];
for(int i = 1; i <= m; i++) if(i != processLast) {
if(neighbour[i].empty()) C[i] = 0;
else {
process(i);
C[i] = -root[i];
}
}
process(processLast);
C[processLast] = -root[processLast];
X.resize(newnode); Y.resize(newnode);
for(int i = 1; i <= newnode; i++)
X[i - 1] = -le[i], Y[i - 1] = -ri[i];
answer(C, X, Y);
}
Compilation message (stderr)
doll.cpp: In function 'void build(int, int, int)':
doll.cpp:17:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
17 | int mid = L + R >> 1;
| ~~^~~
# | 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... |