/* WHY DOES THIS CODE WORK? ? ? ? ?? ? ?*/
#pragma GCC optimize("O3,unroll-loops")
#include "september.h"
#include <string>
#include <cstdio>
#include <array>
#include <vector>
#include <algorithm>
struct max_segtree { std::vector<int> st; int n; max_segtree(int n) : st(n * 2), n(n) {} void pul(int i) { st[i] = std::max(st[i << 1], st[i << 1 | 1]); } int query(int l, int r) { int z = 0; for (l += n, r += n + 1; l < r; l /= 2, r /= 2) { if (l & 1) z = std::max(z, st[l++]); if (r & 1) z = std::max(st[--r], z); } return z; } void assign(int p, int k) { for (st[p += n] = k; p /= 2; ) pul(p); } };
const int NN = 100000;
const int MM = 5;
std::vector<int> g[NN];
int M, tin[NN], tout[NN], timer, pos[MM][NN];
using Lena = std::array<int, 5>;
void add_(Lena &a, const Lena &b) {
int i;
for (i = 0; i < M; ++i)
a[i] = std::max(a[i], b[i]);
}
Lena add(Lena a, const Lena &b) {
add_(a, b);
return a;
}
void dfs(int u) {
tin[u] = timer++;
for (auto v : g[u])
dfs(v);
tout[u] = timer - 1;
}
struct lena_segtree {
std::vector<Lena> st;
int n;
lena_segtree(int n) : st(n * 2), n(n) {}
void pul(int i) { st[i] = add(st[i << 1], st[i << 1 | 1]); }
Lena query(int l, int r) { Lena z {}; for (l += n, r += n + 1; l < r; l /= 2, r /= 2) { if (l & 1) add_(z, st[l++]); if (r & 1) add_(z, st[--r]); } return z; }
void update(int p, int q, int k) { p += n; st[p][q] = std::max(st[p][q], k); for (; p /= 2; ) pul(p); }
void update2(int p, const Lena &x) { add_(st[p += n], x); for (; p /= 2; ) pul(p); }
};
int solve(int N, int M, std::vector<int> F, std::vector<std::vector<int>> S) {
int i, j;
::M = M;
for (i = 0; i < N; ++i)
g[i].clear();
timer = 0;
/* WHY DOES REMOVING THIS BREAK IT */
if (M == 1) { auto S0 = S[0]; std::vector<int> pos(N); for (i = 0; i < N - 1; ++i) pos[S0[i]] = i; for (i = 1; i < N; ++i) g[F[i]].push_back(i); dfs(0); max_segtree ta(N), tb(N); for (i = 1; i < N; ++i) ta.assign(tin[i], pos[i]); for (i = N - 2; i >= 0; --i) { int u, last; u = S0[i]; last = ta.query(tin[u], tout[u]); last = std::max(last, tb.query(i, last)); tb.assign(i, last); } int K = 0; for (i = 0; i < N - 1;) { int until; until = tb.query(i, i); i = until + 1; ++K; } return K; }
for (i = 0; i < M; ++i)
for (j = 0; j < N - 1; ++j)
pos[i][S[i][j]] = j;
for (i = 1; i < N; ++i)
g[F[i]].push_back(i);
dfs(0);
std::vector<lena_segtree> tb;
std::vector<Lena> tc(N);
lena_segtree ta(N);
for (i = 0; i < M; ++i)
tb.emplace_back(N);
auto Put = [&](int u, Lena x) {
add_(x, tc[u]);
for (int j = 0; j < M; ++j)
add_(x, tb[j].query(0, x[j]));
for (int j = 0; j < M; ++j)
tb[j].update2(pos[j][u], x);
ta.update2(tin[u], x);
add_(tc[u], x);
};
for (i = 0; i < M; ++i)
for (j = 1; j < N; ++j)
ta.update(tin[j], i, pos[i][j]);
for (j = 0; j < M; ++j)
for (i = 1; i < N; ++i)
Put(i, ta.query(tin[i], tout[i]));
Lena at {};
int K = 0;
while (1) {
for (j = 0; j < M; ++j)
if (at[j] >= N - 1)
return K;
for (j = 0; j < M; ++j)
add_(at, tc[S[j][at[j]]]);
for (j = 0; j < M; ++j)
++at[j];
++K;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
5464 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
5464 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
9 |
Correct |
1 ms |
5396 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5208 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5720 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
5464 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5468 KB |
Output is correct |
9 |
Correct |
2 ms |
5720 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5468 KB |
Output is correct |
13 |
Correct |
2 ms |
5468 KB |
Output is correct |
14 |
Correct |
2 ms |
5468 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5720 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
4 |
Correct |
9 ms |
5724 KB |
Output is correct |
5 |
Correct |
9 ms |
5716 KB |
Output is correct |
6 |
Correct |
8 ms |
5724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
5464 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
9 |
Correct |
1 ms |
5396 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5208 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5468 KB |
Output is correct |
16 |
Correct |
2 ms |
5720 KB |
Output is correct |
17 |
Correct |
2 ms |
5468 KB |
Output is correct |
18 |
Correct |
2 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5468 KB |
Output is correct |
20 |
Correct |
2 ms |
5468 KB |
Output is correct |
21 |
Correct |
2 ms |
5468 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5468 KB |
Output is correct |
25 |
Correct |
9 ms |
5724 KB |
Output is correct |
26 |
Correct |
9 ms |
5716 KB |
Output is correct |
27 |
Correct |
8 ms |
5724 KB |
Output is correct |
28 |
Correct |
8 ms |
5724 KB |
Output is correct |
29 |
Correct |
10 ms |
5756 KB |
Output is correct |
30 |
Correct |
9 ms |
5720 KB |
Output is correct |
31 |
Correct |
8 ms |
5724 KB |
Output is correct |
32 |
Correct |
8 ms |
5648 KB |
Output is correct |
33 |
Correct |
9 ms |
5720 KB |
Output is correct |
34 |
Correct |
8 ms |
5720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5720 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
4 |
Correct |
174 ms |
20152 KB |
Output is correct |
5 |
Correct |
172 ms |
21264 KB |
Output is correct |
6 |
Correct |
178 ms |
19644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
5464 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
2 ms |
5468 KB |
Output is correct |
9 |
Correct |
2 ms |
5720 KB |
Output is correct |
10 |
Correct |
2 ms |
5468 KB |
Output is correct |
11 |
Correct |
2 ms |
5212 KB |
Output is correct |
12 |
Correct |
2 ms |
5468 KB |
Output is correct |
13 |
Correct |
2 ms |
5468 KB |
Output is correct |
14 |
Correct |
2 ms |
5468 KB |
Output is correct |
15 |
Correct |
2 ms |
5212 KB |
Output is correct |
16 |
Correct |
2 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5468 KB |
Output is correct |
18 |
Correct |
174 ms |
20152 KB |
Output is correct |
19 |
Correct |
172 ms |
21264 KB |
Output is correct |
20 |
Correct |
178 ms |
19644 KB |
Output is correct |
21 |
Correct |
232 ms |
16848 KB |
Output is correct |
22 |
Correct |
212 ms |
16472 KB |
Output is correct |
23 |
Correct |
193 ms |
19644 KB |
Output is correct |
24 |
Correct |
178 ms |
18332 KB |
Output is correct |
25 |
Correct |
226 ms |
16132 KB |
Output is correct |
26 |
Correct |
210 ms |
16312 KB |
Output is correct |
27 |
Correct |
188 ms |
14156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5468 KB |
Output is correct |
2 |
Correct |
2 ms |
5720 KB |
Output is correct |
3 |
Correct |
2 ms |
5468 KB |
Output is correct |
4 |
Correct |
9 ms |
5724 KB |
Output is correct |
5 |
Correct |
9 ms |
5716 KB |
Output is correct |
6 |
Correct |
8 ms |
5724 KB |
Output is correct |
7 |
Correct |
174 ms |
20152 KB |
Output is correct |
8 |
Correct |
172 ms |
21264 KB |
Output is correct |
9 |
Correct |
178 ms |
19644 KB |
Output is correct |
10 |
Execution timed out |
1028 ms |
43092 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
2 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
5464 KB |
Output is correct |
5 |
Correct |
2 ms |
5212 KB |
Output is correct |
6 |
Correct |
2 ms |
5212 KB |
Output is correct |
7 |
Correct |
1 ms |
5212 KB |
Output is correct |
8 |
Correct |
1 ms |
5212 KB |
Output is correct |
9 |
Correct |
1 ms |
5396 KB |
Output is correct |
10 |
Correct |
2 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5208 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
2 ms |
5212 KB |
Output is correct |
15 |
Correct |
2 ms |
5468 KB |
Output is correct |
16 |
Correct |
2 ms |
5720 KB |
Output is correct |
17 |
Correct |
2 ms |
5468 KB |
Output is correct |
18 |
Correct |
2 ms |
5212 KB |
Output is correct |
19 |
Correct |
2 ms |
5468 KB |
Output is correct |
20 |
Correct |
2 ms |
5468 KB |
Output is correct |
21 |
Correct |
2 ms |
5468 KB |
Output is correct |
22 |
Correct |
2 ms |
5212 KB |
Output is correct |
23 |
Correct |
2 ms |
5212 KB |
Output is correct |
24 |
Correct |
2 ms |
5468 KB |
Output is correct |
25 |
Correct |
9 ms |
5724 KB |
Output is correct |
26 |
Correct |
9 ms |
5716 KB |
Output is correct |
27 |
Correct |
8 ms |
5724 KB |
Output is correct |
28 |
Correct |
8 ms |
5724 KB |
Output is correct |
29 |
Correct |
10 ms |
5756 KB |
Output is correct |
30 |
Correct |
9 ms |
5720 KB |
Output is correct |
31 |
Correct |
8 ms |
5724 KB |
Output is correct |
32 |
Correct |
8 ms |
5648 KB |
Output is correct |
33 |
Correct |
9 ms |
5720 KB |
Output is correct |
34 |
Correct |
8 ms |
5720 KB |
Output is correct |
35 |
Correct |
174 ms |
20152 KB |
Output is correct |
36 |
Correct |
172 ms |
21264 KB |
Output is correct |
37 |
Correct |
178 ms |
19644 KB |
Output is correct |
38 |
Correct |
232 ms |
16848 KB |
Output is correct |
39 |
Correct |
212 ms |
16472 KB |
Output is correct |
40 |
Correct |
193 ms |
19644 KB |
Output is correct |
41 |
Correct |
178 ms |
18332 KB |
Output is correct |
42 |
Correct |
226 ms |
16132 KB |
Output is correct |
43 |
Correct |
210 ms |
16312 KB |
Output is correct |
44 |
Correct |
188 ms |
14156 KB |
Output is correct |
45 |
Execution timed out |
1028 ms |
43092 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |