Submission #1001059

# Submission time Handle Problem Language Result Execution time Memory
1001059 2024-06-18T13:54:20 Z vuavisao September (APIO24_september) C++17
75 / 100
237 ms 40376 KB
#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; tree.clear(); 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;
	}
};

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);
	}

	struct FenWickMax {
		int n_node = 0;
		vector<int> tree;

		void resize(int n) { n_node = n; tree.clear(); 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;
		}
	};

	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];
	}
}
 
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 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13912 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13912 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 3 ms 24156 KB Output is correct
9 Correct 3 ms 24156 KB Output is correct
10 Correct 3 ms 24152 KB Output is correct
11 Correct 3 ms 24156 KB Output is correct
12 Correct 3 ms 24156 KB Output is correct
13 Correct 5 ms 24156 KB Output is correct
14 Correct 3 ms 24156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13912 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 3 ms 13916 KB Output is correct
9 Correct 3 ms 13916 KB Output is correct
10 Correct 3 ms 13916 KB Output is correct
11 Correct 3 ms 14168 KB Output is correct
12 Correct 4 ms 13992 KB Output is correct
13 Correct 4 ms 13916 KB Output is correct
14 Correct 3 ms 13916 KB Output is correct
15 Correct 2 ms 13916 KB Output is correct
16 Correct 3 ms 13916 KB Output is correct
17 Correct 3 ms 13912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 17 ms 24408 KB Output is correct
5 Correct 13 ms 24412 KB Output is correct
6 Correct 12 ms 24496 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13912 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 3 ms 24156 KB Output is correct
9 Correct 3 ms 24156 KB Output is correct
10 Correct 3 ms 24152 KB Output is correct
11 Correct 3 ms 24156 KB Output is correct
12 Correct 3 ms 24156 KB Output is correct
13 Correct 5 ms 24156 KB Output is correct
14 Correct 3 ms 24156 KB Output is correct
15 Correct 3 ms 13916 KB Output is correct
16 Correct 3 ms 13916 KB Output is correct
17 Correct 3 ms 13916 KB Output is correct
18 Correct 3 ms 14168 KB Output is correct
19 Correct 4 ms 13992 KB Output is correct
20 Correct 4 ms 13916 KB Output is correct
21 Correct 3 ms 13916 KB Output is correct
22 Correct 2 ms 13916 KB Output is correct
23 Correct 3 ms 13916 KB Output is correct
24 Correct 3 ms 13912 KB Output is correct
25 Correct 17 ms 24408 KB Output is correct
26 Correct 13 ms 24412 KB Output is correct
27 Correct 12 ms 24496 KB Output is correct
28 Correct 15 ms 24412 KB Output is correct
29 Correct 14 ms 24448 KB Output is correct
30 Correct 12 ms 24300 KB Output is correct
31 Correct 13 ms 24452 KB Output is correct
32 Correct 11 ms 24224 KB Output is correct
33 Correct 12 ms 24408 KB Output is correct
34 Correct 12 ms 24296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 228 ms 32200 KB Output is correct
5 Correct 208 ms 32508 KB Output is correct
6 Correct 213 ms 31912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13912 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 3 ms 13916 KB Output is correct
9 Correct 3 ms 13916 KB Output is correct
10 Correct 3 ms 13916 KB Output is correct
11 Correct 3 ms 14168 KB Output is correct
12 Correct 4 ms 13992 KB Output is correct
13 Correct 4 ms 13916 KB Output is correct
14 Correct 3 ms 13916 KB Output is correct
15 Correct 2 ms 13916 KB Output is correct
16 Correct 3 ms 13916 KB Output is correct
17 Correct 3 ms 13912 KB Output is correct
18 Correct 228 ms 32200 KB Output is correct
19 Correct 208 ms 32508 KB Output is correct
20 Correct 213 ms 31912 KB Output is correct
21 Correct 237 ms 27668 KB Output is correct
22 Correct 232 ms 28628 KB Output is correct
23 Correct 217 ms 28292 KB Output is correct
24 Correct 213 ms 28400 KB Output is correct
25 Correct 226 ms 27772 KB Output is correct
26 Correct 223 ms 27864 KB Output is correct
27 Correct 207 ms 31456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 13916 KB Output is correct
2 Correct 3 ms 13916 KB Output is correct
3 Correct 3 ms 13916 KB Output is correct
4 Correct 17 ms 24408 KB Output is correct
5 Correct 13 ms 24412 KB Output is correct
6 Correct 12 ms 24496 KB Output is correct
7 Correct 228 ms 32200 KB Output is correct
8 Correct 208 ms 32508 KB Output is correct
9 Correct 213 ms 31912 KB Output is correct
10 Incorrect 139 ms 40376 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 13912 KB Output is correct
2 Correct 2 ms 13916 KB Output is correct
3 Correct 2 ms 13916 KB Output is correct
4 Correct 2 ms 13916 KB Output is correct
5 Correct 2 ms 13916 KB Output is correct
6 Correct 2 ms 13916 KB Output is correct
7 Correct 3 ms 13916 KB Output is correct
8 Correct 3 ms 24156 KB Output is correct
9 Correct 3 ms 24156 KB Output is correct
10 Correct 3 ms 24152 KB Output is correct
11 Correct 3 ms 24156 KB Output is correct
12 Correct 3 ms 24156 KB Output is correct
13 Correct 5 ms 24156 KB Output is correct
14 Correct 3 ms 24156 KB Output is correct
15 Correct 3 ms 13916 KB Output is correct
16 Correct 3 ms 13916 KB Output is correct
17 Correct 3 ms 13916 KB Output is correct
18 Correct 3 ms 14168 KB Output is correct
19 Correct 4 ms 13992 KB Output is correct
20 Correct 4 ms 13916 KB Output is correct
21 Correct 3 ms 13916 KB Output is correct
22 Correct 2 ms 13916 KB Output is correct
23 Correct 3 ms 13916 KB Output is correct
24 Correct 3 ms 13912 KB Output is correct
25 Correct 17 ms 24408 KB Output is correct
26 Correct 13 ms 24412 KB Output is correct
27 Correct 12 ms 24496 KB Output is correct
28 Correct 15 ms 24412 KB Output is correct
29 Correct 14 ms 24448 KB Output is correct
30 Correct 12 ms 24300 KB Output is correct
31 Correct 13 ms 24452 KB Output is correct
32 Correct 11 ms 24224 KB Output is correct
33 Correct 12 ms 24408 KB Output is correct
34 Correct 12 ms 24296 KB Output is correct
35 Correct 228 ms 32200 KB Output is correct
36 Correct 208 ms 32508 KB Output is correct
37 Correct 213 ms 31912 KB Output is correct
38 Correct 237 ms 27668 KB Output is correct
39 Correct 232 ms 28628 KB Output is correct
40 Correct 217 ms 28292 KB Output is correct
41 Correct 213 ms 28400 KB Output is correct
42 Correct 226 ms 27772 KB Output is correct
43 Correct 223 ms 27864 KB Output is correct
44 Correct 207 ms 31456 KB Output is correct
45 Incorrect 139 ms 40376 KB Output isn't correct
46 Halted 0 ms 0 KB -