From 39f9b49f08e07dd2b8a3dfe269cb82d729cc2813 Mon Sep 17 00:00:00 2001 From: Baoshuo Date: Sat, 10 Dec 2022 20:39:58 +0800 Subject: [PATCH] =?UTF-8?q?P5283=20[=E5=8D=81=E4=BA=8C=E7=9C=81=E8=81=94?= =?UTF-8?q?=E8=80=83=202019]=20=E5=BC=82=E6=88=96=E7=B2=BD=E5=AD=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://www.luogu.com.cn/record/97037195 --- Luogu/P5283/P5283.cpp | 125 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 Luogu/P5283/P5283.cpp diff --git a/Luogu/P5283/P5283.cpp b/Luogu/P5283/P5283.cpp new file mode 100644 index 00000000..7a6a8d1e --- /dev/null +++ b/Luogu/P5283/P5283.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include + +using std::cin; +using std::cout; +const char endl = '\n'; + +const int N = 5e5 + 5; + +class Trie { + private: + const int len = 30; + + struct node { + int cnt; + node* child[2]; + + node() + : cnt(0), child{0, 0} {} + + ~node() { + if (child[0]) delete child[0]; + if (child[1]) delete child[1]; + } + }; + + node* root; + + public: + Trie() + : root(new node()) {} + + ~Trie() { + delete root; + } + + void insert(unsigned long long val) { + node* cur = root; + + cur->cnt++; + + for (int i = 32; ~i; i--) { + bool x = (val >> i) & 1; + + if (cur->child[x] == nullptr) cur->child[x] = new node(); + + cur = cur->child[x]; + cur->cnt++; + } + } + + unsigned long long query(unsigned long long val, int k) { + node* cur = root; + unsigned long long res = 0; + + for (int i = 32; ~i; i--) { + bool x = (val >> i) & 1; + + if (cur->child[x] && cur->child[x]->cnt >= k) { + cur = cur->child[x]; + } else { + if (cur->child[x]) k -= cur->child[x]->cnt; + cur = cur->child[x ^ 1]; + res |= 1ll << i; + } + } + + return res; + } +}; + +int n, k; +unsigned long long s[N], ans; +std::priority_queue< + std::tuple, + std::vector>, + auto(*)(std::tuple, std::tuple)->bool> + q{[](std::tuple lhs, std::tuple rhs) -> bool { + return std::get<2>(lhs) < std::get<2>(rhs); + }}; +Trie trie; + +int main() { + std::ios::sync_with_stdio(false); + cin.tie(nullptr); + + cin >> n >> k; + + k <<= 1; + trie.insert(0); + + for (int i = 1; i <= n; i++) { + unsigned long long x; + + cin >> x; + + s[i] = s[i - 1] ^ x; + trie.insert(s[i]); + } + + for (int i = 0; i <= n; i++) { + q.emplace(i, n + 1, trie.query(s[i], n + 1)); + } + + while (k--) { + auto t = q.top(); + q.pop(); + + ans += std::get<2>(t); + + if (std::get<1>(t)) { + q.emplace( + std::get<0>(t), + std::get<1>(t) - 1, + trie.query(s[std::get<0>(t)], std::get<1>(t) - 1)); + } + } + + cout << (ans >> 1) << endl; + + return 0; +}