rt,只过了样例。求大佬来调。
#include <bits/stdc++.h>
#define BAR cout<<"*******\n";
#define fst first
#define snd second
#define INFILE(x) freopen(x,"r",stdin)
#define OUTFILE(x) freopen(x,"w",stdout)
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
using namespace std;
using ll = long long;
using ul = unsigned long long;
using ui = unsigned int;
using db = double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pll = pair<ll, ll>;
using puu = pair<unsigned, unsigned>;
const int max_n = 5e5 + 10;
unsigned xor_sum[max_n];
struct {
struct node {
int next[2]{}, cnt = 0;
} nodes[max_n * 2 * 32];
int t = 0;
void insert(unsigned x) {
unsigned p = 0;
for (unsigned mask = 1 << 31; mask; mask >>= 1) {
++nodes[p].cnt;
bool c = x & mask;
if (!nodes[p].next[c]) nodes[p].next[c] = ++t;
p = nodes[p].next[c];
}
++nodes[p].cnt;
}
unsigned nth_xor(unsigned x, unsigned n) {
unsigned p = 0, ans = 0, gt_cnt = 0;
--n;
for (unsigned mask = 1 << 31; mask; mask >>= 1) {
bool c = x & mask;
if (!nodes[p].next[c]) {
p = nodes[p].next[!c];
ans |= mask;
continue;
}
if (!nodes[p].next[!c]) {
p = nodes[p].next[c];
continue;
}
if (gt_cnt + nodes[p].next[!c] > n) {
p = nodes[p].next[!c];
ans |= mask;
} else {
gt_cnt += nodes[p].next[!c];
p = nodes[p].next[c];
}
}
return ans;
}
} trie;
int calc_cnt[max_n];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k;
cin >> n >> k;
xor_sum[0] = 0;
for (int i = 1; i <= n; ++i) {
int a;
cin >> a;
xor_sum[i] = xor_sum[i - 1] ^ a;
trie.insert(xor_sum[i]);
}
priority_queue<puu> q;
ll ans = 0;
for (int i = 1; i <= n; ++i) {
++calc_cnt[i];
q.emplace(trie.nth_xor(xor_sum[i], 1), i);
}
for (int i = 1; i <= k << 1; ++i) {
auto t = q.top();
ans += t.fst;
++calc_cnt[t.snd];
q.emplace(trie.nth_xor(xor_sum[t.snd], calc_cnt[t.snd]), t.snd);
}
cout << (ans >> 1) << '\n';
return 0;
}