/* https://codeforces.com/blog/entry/91608 */
#include "game.h"
#include <stdlib.h>
const int N = 300000;
int *ej[N], eo[N], *fi[N], fo[N], ll[N], rr[N], n, k;
void append(int **ej, int *eo, int i, int j) {
int o = eo[i]++;
if (o >= 2 && (o & o - 1) == 0)
ej[i] = (int *) realloc(ej[i], o * 2 * sizeof *ej[i]);
ej[i][o] = j;
}
void init(int n_, int k_) {
n = n_, k = k_;
for (int i = 0; i < n; i++) {
ej[i] = (int *) malloc(2 * sizeof *ej[i]);
fi[i] = (int *) malloc(2 * sizeof *fi[i]);
}
for (int i = 0; i < n; i++)
if (i < k)
ll[i] = rr[i] = i;
else
ll[i] = -1, rr[i] = k;
}
int add(int i, int j) {
int m, o;
if (rr[i] < ll[j])
return 0;
if (rr[j] < ll[i])
return 1;
if (ll[i] == ll[j] && rr[i] == rr[j])
return 0;
if (ll[i] <= ll[j] && rr[j] <= rr[i]) {
m = ll[i] + (rr[i] - ll[i]) / 2;
if (rr[j] > m)
return 0;
rr[i] = m;
if (i >= k && ll[i] == rr[i])
return 1;
for (o = eo[i]; o--; ) {
int v = ej[i][o];
if (add(i, v))
return 1;
}
for (o = fo[i]; o--; ) {
int u = fi[i][o];
if (add(u, i))
return 1;
}
return add(i, j);
} else {
m = ll[j] + (rr[j] - ll[j]) / 2;
if (ll[i] <= m)
return 0;
ll[j] = m + 1;
if (j >= k && ll[j] == rr[j])
return 1;
for (o = eo[j]; o--; ) {
int v = ej[j][o];
if (add(j, v))
return 1;
}
for (o = fo[j]; o--; ) {
int u = fi[j][o];
if (add(u, j))
return 1;
}
return add(i, j);
}
}
int add_teleporter(int i, int j) {
if (i == j)
return i < k;
if (add(i, j))
return 1;
append(ej, eo, i, j), append(fi, fo, j, i);
return 0;
}
Compilation message
game.cpp: In function 'void append(int**, int*, int, int)':
game.cpp:11:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
11 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
0 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
3 ms |
416 KB |
Output is correct |
26 |
Correct |
3 ms |
464 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
0 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
3 ms |
416 KB |
Output is correct |
26 |
Correct |
3 ms |
464 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
464 KB |
Output is correct |
30 |
Correct |
13 ms |
3024 KB |
Output is correct |
31 |
Correct |
5 ms |
3100 KB |
Output is correct |
32 |
Correct |
26 ms |
5440 KB |
Output is correct |
33 |
Correct |
15 ms |
3132 KB |
Output is correct |
34 |
Correct |
40 ms |
16688 KB |
Output is correct |
35 |
Correct |
34 ms |
4680 KB |
Output is correct |
36 |
Correct |
30 ms |
3132 KB |
Output is correct |
37 |
Correct |
34 ms |
3128 KB |
Output is correct |
38 |
Correct |
28 ms |
3176 KB |
Output is correct |
39 |
Correct |
36 ms |
3256 KB |
Output is correct |
40 |
Correct |
25 ms |
5440 KB |
Output is correct |
41 |
Correct |
41 ms |
5076 KB |
Output is correct |
42 |
Correct |
27 ms |
5828 KB |
Output is correct |
43 |
Correct |
33 ms |
4392 KB |
Output is correct |
44 |
Correct |
38 ms |
3268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
0 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
336 KB |
Output is correct |
5 |
Correct |
0 ms |
336 KB |
Output is correct |
6 |
Correct |
0 ms |
336 KB |
Output is correct |
7 |
Correct |
0 ms |
336 KB |
Output is correct |
8 |
Correct |
0 ms |
208 KB |
Output is correct |
9 |
Correct |
1 ms |
208 KB |
Output is correct |
10 |
Correct |
1 ms |
208 KB |
Output is correct |
11 |
Correct |
0 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
0 ms |
208 KB |
Output is correct |
14 |
Correct |
0 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
0 ms |
336 KB |
Output is correct |
17 |
Correct |
0 ms |
336 KB |
Output is correct |
18 |
Correct |
0 ms |
336 KB |
Output is correct |
19 |
Correct |
0 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
0 ms |
336 KB |
Output is correct |
22 |
Correct |
1 ms |
336 KB |
Output is correct |
23 |
Correct |
1 ms |
336 KB |
Output is correct |
24 |
Correct |
1 ms |
464 KB |
Output is correct |
25 |
Correct |
3 ms |
416 KB |
Output is correct |
26 |
Correct |
3 ms |
464 KB |
Output is correct |
27 |
Correct |
2 ms |
336 KB |
Output is correct |
28 |
Correct |
2 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
464 KB |
Output is correct |
30 |
Correct |
13 ms |
3024 KB |
Output is correct |
31 |
Correct |
5 ms |
3100 KB |
Output is correct |
32 |
Correct |
26 ms |
5440 KB |
Output is correct |
33 |
Correct |
15 ms |
3132 KB |
Output is correct |
34 |
Correct |
40 ms |
16688 KB |
Output is correct |
35 |
Correct |
34 ms |
4680 KB |
Output is correct |
36 |
Correct |
30 ms |
3132 KB |
Output is correct |
37 |
Correct |
34 ms |
3128 KB |
Output is correct |
38 |
Correct |
28 ms |
3176 KB |
Output is correct |
39 |
Correct |
36 ms |
3256 KB |
Output is correct |
40 |
Correct |
25 ms |
5440 KB |
Output is correct |
41 |
Correct |
41 ms |
5076 KB |
Output is correct |
42 |
Correct |
27 ms |
5828 KB |
Output is correct |
43 |
Correct |
33 ms |
4392 KB |
Output is correct |
44 |
Correct |
38 ms |
3268 KB |
Output is correct |
45 |
Correct |
188 ms |
28432 KB |
Output is correct |
46 |
Correct |
24 ms |
28412 KB |
Output is correct |
47 |
Correct |
23 ms |
28360 KB |
Output is correct |
48 |
Correct |
276 ms |
38592 KB |
Output is correct |
49 |
Correct |
189 ms |
28444 KB |
Output is correct |
50 |
Correct |
557 ms |
122448 KB |
Output is correct |
51 |
Correct |
767 ms |
58252 KB |
Output is correct |
52 |
Correct |
516 ms |
28620 KB |
Output is correct |
53 |
Correct |
667 ms |
28752 KB |
Output is correct |
54 |
Correct |
783 ms |
28924 KB |
Output is correct |
55 |
Correct |
971 ms |
29264 KB |
Output is correct |
56 |
Correct |
1173 ms |
29360 KB |
Output is correct |
57 |
Correct |
430 ms |
29280 KB |
Output is correct |
58 |
Correct |
460 ms |
29388 KB |
Output is correct |
59 |
Correct |
449 ms |
29316 KB |
Output is correct |
60 |
Correct |
520 ms |
29528 KB |
Output is correct |
61 |
Correct |
685 ms |
29204 KB |
Output is correct |
62 |
Correct |
437 ms |
40536 KB |
Output is correct |
63 |
Correct |
386 ms |
40604 KB |
Output is correct |
64 |
Correct |
1040 ms |
65032 KB |
Output is correct |
65 |
Correct |
529 ms |
43680 KB |
Output is correct |
66 |
Correct |
444 ms |
34680 KB |
Output is correct |
67 |
Correct |
819 ms |
58648 KB |
Output is correct |
68 |
Correct |
160 ms |
28428 KB |
Output is correct |
69 |
Correct |
58 ms |
28436 KB |
Output is correct |
70 |
Correct |
799 ms |
39992 KB |
Output is correct |
71 |
Correct |
268 ms |
29452 KB |
Output is correct |
72 |
Correct |
220 ms |
29336 KB |
Output is correct |
73 |
Correct |
628 ms |
39068 KB |
Output is correct |
74 |
Correct |
1363 ms |
29852 KB |
Output is correct |
75 |
Correct |
903 ms |
40632 KB |
Output is correct |
76 |
Correct |
823 ms |
30496 KB |
Output is correct |