#include <iostream>
#include <fstream>
#include <algorithm>
#include <cstring>
#include <climits>
#include <cassert>
#include <tuple>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <unordered_set>
#include <unordered_map>
using namespace std;
typedef long long LL;
typedef vector<int> Vi;
typedef vector<bool> Vb;
typedef vector<vector<int>> Vii;
#define forR(i, n) for (int i = 0; i < (n); i++)
void answer(Vi C, Vi X, Vi Y);
Vi C, X, Y;
Vi swState;
int getLevelSW(int sz) {
int level = 1, maxSz = 2;
while (sz > maxSz) {
level++, maxSz *= 2;
}
return level;
}
int getSW() {
int swNo = -1 - X.size();
X.push_back(0); Y.push_back(0); swState.push_back(0);
return swNo;
}
void setSWX(int sw, int t) {
X[-1 - sw] = t;
}
void setSWY(int sw, int t) {
Y[-1 - sw] = t;
}
int getStateSW(int sw) {
int state = swState[sw];
swState[sw] = 1 - state;
return state;
}
int& goAlong(int node) {
int* nodePtr = NULL;
for (int i = 0; node < 0; i++) {
int sw = -1 - node;
if (getStateSW(sw) == 0) {
nodePtr = &X[sw];
} else {
nodePtr = &Y[sw];
}
node = *nodePtr;
}
return *nodePtr;
}
int buildLevelSW(int lvl, int nSkip = 0) {
int root = getSW();
Vi sws(1, root);
for (int i = 1; i < lvl; i++) {
Vi sw_lvl;
int lvl_skip = 1 << (lvl - i);
for (int sw : sws) {
if (nSkip >= lvl_skip) {
int sw1 = getSW();
sw_lvl.push_back(sw1);
setSWX(sw, root);
setSWY(sw, sw1);
nSkip -= lvl_skip;
} else {
int sw0 = getSW(), sw1 = getSW();
sw_lvl.push_back(sw0);
sw_lvl.push_back(sw1);
setSWX(sw, sw0);
setSWY(sw, sw1);
}
}
sws = sw_lvl;
}
if (nSkip > 0) {
setSWX(sws[0], root);
}
return root;
}
void build_answer(int node, const Vi& nexts) {
if (nexts.size() == 0) {
C[node] = 0;
return;
} else if (nexts.size() == 1) {
C[node] = nexts[0];
return;
}
int lvlSW = getLevelSW(nexts.size());
int root = buildLevelSW(lvlSW);
forR(i, ((1 << lvlSW) - nexts.size())) {
goAlong(root) = root;
}
for (int next : nexts) {
goAlong(root) = next;
}
C[node] = root;
}
void create_circuit(int M, Vi A) {
int n = A.size();
X = Y = swState = Vi();
C = Vi(M + 1);
C[0] = A[0];
Vii nexts(M + 1);
A.push_back(0);
forR(i, n) {
nexts[A[i]].push_back(A[i + 1]);
}
int lvl = getLevelSW(n);
int skips = (1 << lvl) - n;
int root = buildLevelSW(lvl, skips);
forR(i, n) {
goAlong(root) = A[i + 1];
}
forR(i, M) {
C[i + 1] = root;
}
//forR(i, M) {
// build_answer(i + 1, nexts[i + 1]);
//}
answer(C, X, Y);
}
Compilation message
doll.cpp: In function 'void build_answer(int, const Vi&)':
doll.cpp:24:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | #define forR(i, n) for (int i = 0; i < (n); i++)
| ^
doll.cpp:115:2: note: in expansion of macro 'forR'
115 | forR(i, ((1 << lvlSW) - nexts.size())) {
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
60 ms |
8552 KB |
Output is correct |
3 |
Correct |
56 ms |
6864 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
14 ms |
3788 KB |
Output is correct |
6 |
Correct |
87 ms |
10632 KB |
Output is correct |
7 |
Correct |
2 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
60 ms |
8552 KB |
Output is correct |
3 |
Correct |
56 ms |
6864 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
14 ms |
3788 KB |
Output is correct |
6 |
Correct |
87 ms |
10632 KB |
Output is correct |
7 |
Correct |
2 ms |
204 KB |
Output is correct |
8 |
Correct |
118 ms |
9532 KB |
Output is correct |
9 |
Correct |
116 ms |
11816 KB |
Output is correct |
10 |
Correct |
168 ms |
14444 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
60 ms |
8552 KB |
Output is correct |
3 |
Correct |
56 ms |
6864 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
14 ms |
3788 KB |
Output is correct |
6 |
Correct |
87 ms |
10632 KB |
Output is correct |
7 |
Correct |
2 ms |
204 KB |
Output is correct |
8 |
Correct |
118 ms |
9532 KB |
Output is correct |
9 |
Correct |
116 ms |
11816 KB |
Output is correct |
10 |
Correct |
168 ms |
14444 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
166 ms |
12696 KB |
Output is correct |
15 |
Correct |
97 ms |
8112 KB |
Output is correct |
16 |
Correct |
151 ms |
11796 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
252 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
160 ms |
13372 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
2 ms |
204 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
104 ms |
6944 KB |
Output is correct |
3 |
Correct |
87 ms |
6196 KB |
Output is correct |
4 |
Correct |
124 ms |
8876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
104 ms |
6944 KB |
Output is correct |
3 |
Correct |
87 ms |
6196 KB |
Output is correct |
4 |
Correct |
124 ms |
8876 KB |
Output is correct |
5 |
Correct |
173 ms |
12360 KB |
Output is correct |
6 |
Correct |
150 ms |
11800 KB |
Output is correct |
7 |
Correct |
151 ms |
11920 KB |
Output is correct |
8 |
Correct |
148 ms |
11492 KB |
Output is correct |
9 |
Correct |
101 ms |
6296 KB |
Output is correct |
10 |
Correct |
174 ms |
11120 KB |
Output is correct |
11 |
Correct |
137 ms |
11084 KB |
Output is correct |
12 |
Correct |
121 ms |
7060 KB |
Output is correct |
13 |
Correct |
89 ms |
8128 KB |
Output is correct |
14 |
Correct |
115 ms |
8160 KB |
Output is correct |
15 |
Correct |
99 ms |
8504 KB |
Output is correct |
16 |
Correct |
3 ms |
460 KB |
Output is correct |
17 |
Correct |
84 ms |
7556 KB |
Output is correct |
18 |
Correct |
81 ms |
7532 KB |
Output is correct |
19 |
Correct |
100 ms |
7356 KB |
Output is correct |
20 |
Correct |
147 ms |
10840 KB |
Output is correct |
21 |
Correct |
138 ms |
10752 KB |
Output is correct |
22 |
Correct |
136 ms |
10396 KB |
Output is correct |