NOI 2019 Provincial Team Selection: Twelve-Province Joint Contest — Problem-Solving Report
A - XOR Zongzi
“A stupid-easy problem.” — XG_Zepto
This problem is still pretty easy to do. First put all the prefix XOR sums into a trie, then in \(O(n)\) enumerate the right endpoint \(r\), look up in the trie the maximum XOR with the prefix XOR sum \([1-r]\) and put it into the heap (when putting it into the heap, mark its rank as \(1\)). After that, take items out of the heap and keep putting in the XOR query values with gradually increasing ranks; just collect the first \(k\).
// P5283.cpp
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX_N = 5e5 * 35;
struct npair
{
int point, rk;
ll val;
bool operator<(const npair &np) const { return val < np.val; }
};
int trie[MAX_N][2], size[MAX_N], treetot = 1, current_rank;
ll n, k, arr[MAX_N];
priority_queue<npair> pq;
void insert(ll num)
{
int p = 1;
for (int i = 31; i >= 0; i--)
{
int bit = (num >> i) & 1;
size[p]++;
if (trie[p][bit] == 0)
trie[p][bit] = ++treetot;
p = trie[p][bit];
}
size[p]++;
}
ll atRank(ll num, int rk)
{
int p = 1;
ll ans = 0;
for (int i = 31; i >= 0; i--)
{
int bit = ((num >> i) & 1);
if (trie[p][bit ^ 1] == 0)
p = trie[p][bit];
else if (rk <= size[trie[p][bit ^ 1]])
p = trie[p][bit ^ 1], ans |= 1LL << i;
else
rk -= size[trie[p][bit ^ 1]], p = trie[p][bit];
}
return ans;
}
int main()
{
scanf("%lld%lld", &n, &k), k *= 2;
for (int i = 1; i <= n; i++)
scanf("%lld", &arr[i]), arr[i] = arr[i - 1] ^ arr[i];
for (int i = 0; i <= n; i++)
insert(arr[i]);
for (int i = 0; i <= n; i++)
pq.push(npair{i, 1, atRank(arr[i], 1)});
ll ans = 1;
for (int i = 1; i <= k; i++)
{
npair np = pq.top();
ans += np.val, pq.pop();
if (np.rk < n)
np.rk++, np.val = atRank(arr[np.point], np.rk), pq.push(np);
}
printf("%lld", ans >> 1);
return 0;
}
B - Twelve Chimes of the Spring Festival
“A stupid-easy problem.” — XG_Zepto
This problem is quite interesting. Consider a node with two child chains:

We can consider putting the chains into their corresponding queues, then taking out the maximums and merging them one by one: it just so happens that the nodes on the two chains are never ancestors of one another, so keeping the maximum is enough to do the merge, filtering out a pile of useless information. So, in the case of \(n\) chains we only need to merge them one at a time (remember, pick the short chain to merge in; if the chain's length is larger than the current number of elements in the heap, you can consider an O(1) heap swap to get a better complexity.
// P5290.cpp
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 2000;
int fa[MAX_N], head[MAX_N], current, weight[MAX_N], n, size[MAX_N];
int dfn[MAX_N], tot, tmp[MAX_N];
priority_queue<int> queues[MAX_N];
struct edge
{
int to, nxt;
} edges[MAX_N << 1];
void addpath(int src, int dst)
{
edges[current].to = dst, edges[current].nxt = head[src];
head[src] = current++;
}
void dfs(int u)
{
dfn[u] = ++tot;
for (int i = head[u]; i != -1; i = edges[i].nxt)
if (edges[i].to != fa[u])
{
dfs(edges[i].to);
if (queues[dfn[edges[i].to]].size() > queues[dfn[u]].size())
swap(dfn[edges[i].to], dfn[u]);
int m = queues[dfn[edges[i].to]].size();
for (int pt = 1; pt <= m; pt++)
{
tmp[pt] = max(queues[dfn[u]].top(),
queues[dfn[edges[i].to]].top());
queues[dfn[edges[i].to]].pop();
queues[dfn[u]].pop();
}
for (int pt = 1; pt <= m; pt++)
queues[dfn[u]].push(tmp[pt]);
}
queues[dfn[u]].push(weight[u]);
}
int main()
{
memset(head, -1, sizeof(head));
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &weight[i]);
for (int i = 2; i <= n; i++)
scanf("%d", &fa[i]), addpath(fa[i], i), addpath(i, fa[i]);
dfs(1);
long long ans = 0;
while (!queues[dfn[1]].empty())
ans += queues[dfn[1]].top(), queues[dfn[1]].pop();
printf("%lld", ans);
return 0;
}