From de66dfbc030ab606693a3739486a5cb72ea34167 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Thu, 2 Feb 2023 21:04:21 +0800 Subject: [PATCH] P8511 [Ynoi Easy Round 2021] TEST_68 https://www.luogu.com.cn/record/101246432 --- Luogu/P8511/P8511.cpp | 135 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 Luogu/P8511/P8511.cpp diff --git a/Luogu/P8511/P8511.cpp b/Luogu/P8511/P8511.cpp new file mode 100644 index 00000000..e83ab96d --- /dev/null +++ b/Luogu/P8511/P8511.cpp @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 5e5 + 5; + +int n, fa[N]; +long long a[N], max, ans[N]; +std::vector g[N]; +std::unordered_map lst; +bool vis[N]; +int cnt = 1, tr[N * 60][2]; + +void insert(long long x) { + int p = 1; + + for (int i = 60; i >= 0; i--) { + bool ch = (x >> i) & 1; + + if (!tr[p][ch]) { + tr[p][ch] = ++cnt; + } + + p = tr[p][ch]; + } +} + +long long query(long long x) { + int p = 1; + long long res = 0; + + for (int i = 60; i >= 0; i--) { + int ch = (x >> i) & 1; + + if (!p) break; + + if (tr[p][ch ^ 1]) { + res |= 1ll << i; + p = tr[p][ch ^ 1]; + } else { + p = tr[p][ch]; + } + } + + return res; +} + +void solve(int x) { + max = 0; + cnt = 1; + memset(tr, 0x00, sizeof(tr)); + std::vector vec; + std::vector vis2(n + 1); + + for (int i = x; i; i = fa[i]) { + vec.emplace_back(i); + vis[i] = true; + vis2[i] = true; + } + + std::reverse(vec.begin(), vec.end()); + + std::function get = [&](int u) -> void { + insert(a[u]); + max = std::max(max, query(a[u])); + + for (int v : g[u]) { + get(v); + } + }; + + for (int u : vec) { + ans[u] = max; + + for (int v : g[u]) { + if (vis2[v]) continue; + + get(v); + } + + insert(a[u]); + max = std::max(max, query(a[u])); + } +} + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n; + + for (int i = 2; i <= n; i++) { + cin >> fa[i]; + + g[fa[i]].emplace_back(i); + } + + for (int i = 1; i <= n; i++) { + cin >> a[i]; + } + + for (int i = 1; i <= n; i++) { + max = std::max(max, query(a[i])); + insert(a[i]); + } + + int x = 1, y = 1; + + for (int i = 1; i <= n; i++) { + lst[a[i]] = i; + + if (lst.count(a[i] ^ max)) { + x = i; + y = lst[a[i] ^ max]; + + break; + } + } + + solve(x); + solve(y); + + for (int i = 1; i <= n; i++) { + cout << (vis[i] ? ans[i] : max) << endl; + } + + return 0; +}