#include "september.h"
#include <vector>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 100'000 + 10;
const int INF = 1'000'000'000;
struct FenWick {
int n_node = 0;
vector<int> tree;
void resize(int n) { n_node = n; vector<int>().swap(tree); tree.resize(n_node + 10, 0); }
FenWick() {};
FenWick(int n) { this->resize(n); };
void Update(int idx, int val) {
for ( ; idx <= n_node; idx += (idx & - idx)) tree[idx] += val;
return;
}
int Query(int idx) {
int res = 0;
for ( ; idx > 0; idx -= (idx & - idx)) res += tree[idx];
return res;
}
int Sum_to(int l, int r) {
if (l > r) return 0;
return Query(r) - Query(l - 1);
}
int kth(int val) {
int res = 0, s = 0;
for (int mask = 30; mask >= 0; -- mask) {
int nxt = res | (1 << mask);
if (nxt < n_node && s + tree[nxt] < val) {
res = nxt;
s += tree[nxt];
}
}
++ res; return res;
}
};
struct FenWickMax {
int n_node = 0;
vector<int> tree;
void resize(int n) { n_node = n; vector<int>().swap(tree); tree.resize(n_node + 10, -INF); }
FenWickMax() {};
FenWickMax(int n) { this->resize(n); };
void Update(int idx, int val) {
for ( ; idx > 0; idx -= (idx & - idx)) tree[idx] = max(tree[idx], val);
return;
}
int Query(int idx) {
int res = -INF;
for ( ; idx <= n_node; idx += (idx & - idx)) res = max(res, tree[idx]);
return res;
}
};
int n, m;
int parent[N];
vector<int> g[N];
int perRem[10][N];
int need[10][N];
namespace Order {
int indexing[N];
void dfs(int u, int res[]) {
int idx = indexing[u];
res[idx] = idx;
for (const auto& v : g[u]) {
dfs(v, res);
res[idx] = max(res[idx], res[indexing[v]]);
}
}
void solve(int perRem[], int res[]) {
for (int i = 1; i < n; ++ i) {
indexing[perRem[i]] = i;
}
indexing[1] = 0;
dfs(1, res);
}
}
namespace One {
bool check() {
return (m == 1);
}
vector<int> open[N];
int dp[N];
int solve() {
for (int i = 0; i <= n + 1; ++ i) {
vector<int>().swap(open[i]);
dp[i] = -INF;
}
for (int i = 1; i < n; ++ i) {
open[need[1][i]].push_back(i);
}
dp[0] = 0;
FenWick bit(n);
for (int i = 1; i < n; ++ i) bit.Update(i, 1);
FenWickMax bitMax(n);
for (int i = 1; i < n; ++ i) {
for (const auto& idx : open[i]) bit.Update(idx, -1);
bitMax.Update(i, dp[i - 1]);
int cnt = bit.Query(i);
int last = bit.kth(cnt) + (cnt != 0);
if (last <= i) {
dp[i] = bitMax.Query(last) + 1;
}
}
return dp[n - 1];
}
}
namespace Brute_force {
bool check() {
return (n <= 1'000);
}
unsigned seed = chrono::system_clock::now().time_since_epoch().count();
mt19937_64 rng(seed);
ll Rand(ll a, ll b = 1ll * INF * INF) {
return a + rng() % (b - a + 1);
}
bool can[1'010][1'010];
ll base[N];
ll Hash[10][N];
vector<int> open[N];
int exist[N];
int dp[N];
int solve() {
srand(time(nullptr));
for (int i = 0; i <= n + 1; ++ i) {
dp[i] = -INF;
exist[i] = 0;
vector<int>().swap(open[i]);
}
for (int i = 0; i <= n + 1; ++ i) {
for (int j = 0; j <= n + 1; ++ j) {
can[i][j] = false;
}
}
for (int i = 1; i <= n; ++ i) base[i] = Rand(1);
for (int i = 1; i <= m; ++ i) {
for (int j = 1; j < n; ++ j) {
Hash[i][j] = base[perRem[i][j]];
open[need[i][j]].push_back(j);
}
}
for (int i = 1; i < n; ++ i) {
ll xorHash[6];
memset(xorHash, 0, sizeof(xorHash));
for (int j = i; j < n; ++ j) {
for (int k = 1; k <= m; ++ k) {
xorHash[k] ^= Hash[k][j];
}
if (*min_element(xorHash + 1, xorHash + 1 + m) == *max_element(xorHash + 1, xorHash + 1 + m)) {
can[i][j] = true;
}
}
}
dp[0] = 0;
for (int i = 1; i < n; ++ i) {
for (const auto& idx : open[i]) ++ exist[idx];
for (int j = i; j >= 1; -- j) {
if (exist[j] < m) break;
if (can[j][i]) {
// cout << j << ' ' << i << ' ' << dp[i] << ' ' << dp[j - 1] << '\n';
dp[i] = max(dp[i], dp[j - 1] + 1);
}
}
}
return dp[n - 1];
}
}
namespace Full {
int L[N], R[N];
int pred[N];
vector<int> position[N];
int indexing[N];
vector<int> open[N];
int exist[N];
FenWickMax bitMax[N];
int dp[N];
int solve() {
for (int i = 0; i <= n + 1; ++ i) {
position[i].clear();
pred[i] = 0;
L[i] = n + 1;
R[i] = 0;
dp[i] = -INF;
exist[i] = 0;
vector<int>().swap(open[i]);
}
for (int i = 1; i <= m; ++ i) {
for (int j = 1; j < n; ++ j) {
int u = perRem[i][j];
L[u] = min(L[u], j);
R[u] = max(R[u], j);
open[need[i][j]].push_back(j);
}
}
for (int u = 2; u <= n; ++ u) {
++ pred[L[u]];
-- pred[R[u]];
}
for (int i = 1; i < n; ++ i) {
pred[i] += pred[i - 1];
position[pred[i - 1]].push_back(i - 1);
indexing[i - 1] = position[pred[i - 1]].size();
}
FenWick bit(n);
for (int i = 1; i < n; ++ i) bit.Update(i, m);
for (int i = 0; i < n; ++ i) {
if (position[i].empty()) continue;
bitMax[i].resize(position[i].size());
}
dp[0] = 0;
for (int i = 1; i < n; ++ i) {
for (const auto& idx : open[i]) {
bit.Update(idx, -1);
if (++ exist[idx] == m) {
int newIndex = idx - 1;
assert(newIndex < i);
int group = pred[newIndex];
bitMax[group].Update(indexing[newIndex], dp[newIndex]);
}
}
int group = pred[i];
if (position[group].empty()) continue;
int cnt = bit.Query(i);
int last = bit.kth(cnt) - (cnt == 0);
auto ptr = lower_bound(position[group].begin(), position[group].end(), last);
if (ptr != position[group].end()) {
int idx = position[group][ptr - position[group].begin()];
dp[i] = bitMax[group].Query(indexing[idx]) + 1;
}
}
return dp[n - 1];
}
}
int solve(int N, int M, std::vector<int> F, std::vector<std::vector<int>> S) {
n = N; m = M;
for (int i = 0; i <= n + 1; ++ i) g[i].clear();
for (int v = 2; v <= n; ++ v) {
int u = F[v - 1] + 1;
g[u].push_back(v);
parent[v] = u;
}
for (int i = 0; i < m; ++ i) {
for (int j = 0; j < n - 1; ++ j) {
perRem[i + 1][j + 1] = S[i][j] + 1;
}
}
for (int i = 1; i <= m; ++ i) {
Order::solve(perRem[i], need[i]);
}
if (One::check()) {
return One::solve();
}
if (Brute_force::check()) {
return Brute_force::solve();
}
return Full::solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
24156 KB |
Output is correct |
2 |
Correct |
4 ms |
24156 KB |
Output is correct |
3 |
Correct |
4 ms |
24160 KB |
Output is correct |
4 |
Correct |
5 ms |
24156 KB |
Output is correct |
5 |
Correct |
4 ms |
24156 KB |
Output is correct |
6 |
Correct |
5 ms |
24156 KB |
Output is correct |
7 |
Correct |
4 ms |
24180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
24156 KB |
Output is correct |
2 |
Correct |
4 ms |
24156 KB |
Output is correct |
3 |
Correct |
4 ms |
24160 KB |
Output is correct |
4 |
Correct |
5 ms |
24156 KB |
Output is correct |
5 |
Correct |
4 ms |
24156 KB |
Output is correct |
6 |
Correct |
5 ms |
24156 KB |
Output is correct |
7 |
Correct |
4 ms |
24180 KB |
Output is correct |
8 |
Correct |
5 ms |
26204 KB |
Output is correct |
9 |
Correct |
5 ms |
30300 KB |
Output is correct |
10 |
Correct |
4 ms |
22136 KB |
Output is correct |
11 |
Correct |
4 ms |
24156 KB |
Output is correct |
12 |
Correct |
5 ms |
24156 KB |
Output is correct |
13 |
Correct |
5 ms |
32348 KB |
Output is correct |
14 |
Correct |
5 ms |
34396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
24160 KB |
Output is correct |
2 |
Correct |
4 ms |
24156 KB |
Output is correct |
3 |
Correct |
5 ms |
24152 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
24156 KB |
Output is correct |
2 |
Correct |
4 ms |
24156 KB |
Output is correct |
3 |
Correct |
4 ms |
24160 KB |
Output is correct |
4 |
Correct |
5 ms |
24156 KB |
Output is correct |
5 |
Correct |
4 ms |
24156 KB |
Output is correct |
6 |
Correct |
5 ms |
24156 KB |
Output is correct |
7 |
Correct |
4 ms |
24180 KB |
Output is correct |
8 |
Correct |
5 ms |
24160 KB |
Output is correct |
9 |
Correct |
4 ms |
24156 KB |
Output is correct |
10 |
Correct |
5 ms |
24152 KB |
Output is correct |
11 |
Correct |
7 ms |
24164 KB |
Output is correct |
12 |
Correct |
5 ms |
24152 KB |
Output is correct |
13 |
Correct |
5 ms |
24156 KB |
Output is correct |
14 |
Correct |
5 ms |
24156 KB |
Output is correct |
15 |
Correct |
5 ms |
24156 KB |
Output is correct |
16 |
Correct |
5 ms |
22108 KB |
Output is correct |
17 |
Correct |
5 ms |
24184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
24160 KB |
Output is correct |
2 |
Correct |
4 ms |
24156 KB |
Output is correct |
3 |
Correct |
5 ms |
24152 KB |
Output is correct |
4 |
Correct |
14 ms |
22620 KB |
Output is correct |
5 |
Correct |
14 ms |
22872 KB |
Output is correct |
6 |
Correct |
13 ms |
25180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
24156 KB |
Output is correct |
2 |
Correct |
4 ms |
24156 KB |
Output is correct |
3 |
Correct |
4 ms |
24160 KB |
Output is correct |
4 |
Correct |
5 ms |
24156 KB |
Output is correct |
5 |
Correct |
4 ms |
24156 KB |
Output is correct |
6 |
Correct |
5 ms |
24156 KB |
Output is correct |
7 |
Correct |
4 ms |
24180 KB |
Output is correct |
8 |
Correct |
5 ms |
26204 KB |
Output is correct |
9 |
Correct |
5 ms |
30300 KB |
Output is correct |
10 |
Correct |
4 ms |
22136 KB |
Output is correct |
11 |
Correct |
4 ms |
24156 KB |
Output is correct |
12 |
Correct |
5 ms |
24156 KB |
Output is correct |
13 |
Correct |
5 ms |
32348 KB |
Output is correct |
14 |
Correct |
5 ms |
34396 KB |
Output is correct |
15 |
Correct |
5 ms |
24160 KB |
Output is correct |
16 |
Correct |
4 ms |
24156 KB |
Output is correct |
17 |
Correct |
5 ms |
24152 KB |
Output is correct |
18 |
Correct |
7 ms |
24164 KB |
Output is correct |
19 |
Correct |
5 ms |
24152 KB |
Output is correct |
20 |
Correct |
5 ms |
24156 KB |
Output is correct |
21 |
Correct |
5 ms |
24156 KB |
Output is correct |
22 |
Correct |
5 ms |
24156 KB |
Output is correct |
23 |
Correct |
5 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
24184 KB |
Output is correct |
25 |
Correct |
14 ms |
22620 KB |
Output is correct |
26 |
Correct |
14 ms |
22872 KB |
Output is correct |
27 |
Correct |
13 ms |
25180 KB |
Output is correct |
28 |
Correct |
14 ms |
23132 KB |
Output is correct |
29 |
Correct |
14 ms |
34636 KB |
Output is correct |
30 |
Correct |
15 ms |
34396 KB |
Output is correct |
31 |
Correct |
13 ms |
34396 KB |
Output is correct |
32 |
Correct |
13 ms |
23132 KB |
Output is correct |
33 |
Correct |
13 ms |
34392 KB |
Output is correct |
34 |
Correct |
14 ms |
20828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
24160 KB |
Output is correct |
2 |
Correct |
4 ms |
24156 KB |
Output is correct |
3 |
Correct |
5 ms |
24152 KB |
Output is correct |
4 |
Correct |
208 ms |
38640 KB |
Output is correct |
5 |
Correct |
228 ms |
39020 KB |
Output is correct |
6 |
Correct |
222 ms |
39968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
24156 KB |
Output is correct |
2 |
Correct |
4 ms |
24156 KB |
Output is correct |
3 |
Correct |
4 ms |
24160 KB |
Output is correct |
4 |
Correct |
5 ms |
24156 KB |
Output is correct |
5 |
Correct |
4 ms |
24156 KB |
Output is correct |
6 |
Correct |
5 ms |
24156 KB |
Output is correct |
7 |
Correct |
4 ms |
24180 KB |
Output is correct |
8 |
Correct |
5 ms |
24160 KB |
Output is correct |
9 |
Correct |
4 ms |
24156 KB |
Output is correct |
10 |
Correct |
5 ms |
24152 KB |
Output is correct |
11 |
Correct |
7 ms |
24164 KB |
Output is correct |
12 |
Correct |
5 ms |
24152 KB |
Output is correct |
13 |
Correct |
5 ms |
24156 KB |
Output is correct |
14 |
Correct |
5 ms |
24156 KB |
Output is correct |
15 |
Correct |
5 ms |
24156 KB |
Output is correct |
16 |
Correct |
5 ms |
22108 KB |
Output is correct |
17 |
Correct |
5 ms |
24184 KB |
Output is correct |
18 |
Correct |
208 ms |
38640 KB |
Output is correct |
19 |
Correct |
228 ms |
39020 KB |
Output is correct |
20 |
Correct |
222 ms |
39968 KB |
Output is correct |
21 |
Correct |
248 ms |
33972 KB |
Output is correct |
22 |
Correct |
237 ms |
35060 KB |
Output is correct |
23 |
Correct |
222 ms |
34196 KB |
Output is correct |
24 |
Correct |
210 ms |
30308 KB |
Output is correct |
25 |
Correct |
240 ms |
29432 KB |
Output is correct |
26 |
Correct |
245 ms |
29660 KB |
Output is correct |
27 |
Correct |
230 ms |
33364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
24160 KB |
Output is correct |
2 |
Correct |
4 ms |
24156 KB |
Output is correct |
3 |
Correct |
5 ms |
24152 KB |
Output is correct |
4 |
Correct |
14 ms |
22620 KB |
Output is correct |
5 |
Correct |
14 ms |
22872 KB |
Output is correct |
6 |
Correct |
13 ms |
25180 KB |
Output is correct |
7 |
Correct |
208 ms |
38640 KB |
Output is correct |
8 |
Correct |
228 ms |
39020 KB |
Output is correct |
9 |
Correct |
222 ms |
39968 KB |
Output is correct |
10 |
Correct |
204 ms |
47304 KB |
Output is correct |
11 |
Correct |
188 ms |
44232 KB |
Output is correct |
12 |
Correct |
196 ms |
43332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
24156 KB |
Output is correct |
2 |
Correct |
4 ms |
24156 KB |
Output is correct |
3 |
Correct |
4 ms |
24160 KB |
Output is correct |
4 |
Correct |
5 ms |
24156 KB |
Output is correct |
5 |
Correct |
4 ms |
24156 KB |
Output is correct |
6 |
Correct |
5 ms |
24156 KB |
Output is correct |
7 |
Correct |
4 ms |
24180 KB |
Output is correct |
8 |
Correct |
5 ms |
26204 KB |
Output is correct |
9 |
Correct |
5 ms |
30300 KB |
Output is correct |
10 |
Correct |
4 ms |
22136 KB |
Output is correct |
11 |
Correct |
4 ms |
24156 KB |
Output is correct |
12 |
Correct |
5 ms |
24156 KB |
Output is correct |
13 |
Correct |
5 ms |
32348 KB |
Output is correct |
14 |
Correct |
5 ms |
34396 KB |
Output is correct |
15 |
Correct |
5 ms |
24160 KB |
Output is correct |
16 |
Correct |
4 ms |
24156 KB |
Output is correct |
17 |
Correct |
5 ms |
24152 KB |
Output is correct |
18 |
Correct |
7 ms |
24164 KB |
Output is correct |
19 |
Correct |
5 ms |
24152 KB |
Output is correct |
20 |
Correct |
5 ms |
24156 KB |
Output is correct |
21 |
Correct |
5 ms |
24156 KB |
Output is correct |
22 |
Correct |
5 ms |
24156 KB |
Output is correct |
23 |
Correct |
5 ms |
22108 KB |
Output is correct |
24 |
Correct |
5 ms |
24184 KB |
Output is correct |
25 |
Correct |
14 ms |
22620 KB |
Output is correct |
26 |
Correct |
14 ms |
22872 KB |
Output is correct |
27 |
Correct |
13 ms |
25180 KB |
Output is correct |
28 |
Correct |
14 ms |
23132 KB |
Output is correct |
29 |
Correct |
14 ms |
34636 KB |
Output is correct |
30 |
Correct |
15 ms |
34396 KB |
Output is correct |
31 |
Correct |
13 ms |
34396 KB |
Output is correct |
32 |
Correct |
13 ms |
23132 KB |
Output is correct |
33 |
Correct |
13 ms |
34392 KB |
Output is correct |
34 |
Correct |
14 ms |
20828 KB |
Output is correct |
35 |
Correct |
208 ms |
38640 KB |
Output is correct |
36 |
Correct |
228 ms |
39020 KB |
Output is correct |
37 |
Correct |
222 ms |
39968 KB |
Output is correct |
38 |
Correct |
248 ms |
33972 KB |
Output is correct |
39 |
Correct |
237 ms |
35060 KB |
Output is correct |
40 |
Correct |
222 ms |
34196 KB |
Output is correct |
41 |
Correct |
210 ms |
30308 KB |
Output is correct |
42 |
Correct |
240 ms |
29432 KB |
Output is correct |
43 |
Correct |
245 ms |
29660 KB |
Output is correct |
44 |
Correct |
230 ms |
33364 KB |
Output is correct |
45 |
Correct |
204 ms |
47304 KB |
Output is correct |
46 |
Correct |
188 ms |
44232 KB |
Output is correct |
47 |
Correct |
196 ms |
43332 KB |
Output is correct |
48 |
Correct |
202 ms |
37064 KB |
Output is correct |
49 |
Correct |
229 ms |
38728 KB |
Output is correct |
50 |
Correct |
197 ms |
38972 KB |
Output is correct |
51 |
Correct |
194 ms |
37836 KB |
Output is correct |
52 |
Correct |
212 ms |
36900 KB |
Output is correct |
53 |
Correct |
199 ms |
36812 KB |
Output is correct |
54 |
Correct |
202 ms |
37924 KB |
Output is correct |