---
id: 1092
lang: en
translation_of: index.md
source_sha256: 85a3fcfdcbe2c3699586e01b557a40b6f7def5cb7101909a139fda78dbbf7d2d
title: "NOI 2019 Provincial Team Selection: Twelve-Province Joint Contest — Problem-Solving Report"
description: "Solutions to two Twelve-Province Joint Contest 2019 problems: XOR Zongzi with a trie and a heap, and Twelve Chimes by merging heaps along chains."
tags: ["OI","Provincial Team Selection","2019","Twelve-Province Joint Contest"]
translated: 2026-11-27
translator: Claude Opus 5.5
translator_model: claude-opus-5-5
translation_notes:
  - "Problem names 异或粽子 and 春节十二响 translated as “XOR Zongzi” and “Twelve Chimes of the Spring Festival”."
  - "The unclosed parenthesis in the last paragraph of B is kept as in the original."
---

## A - XOR Zongzi

“A <span class="slang" data-zh="傻逼题" data-gloss="shǎbī tí, “idiot problem”. Vulgar: a problem so easy that it is insulting.">stupid-easy problem</span>.” — [XG_Zepto](https://zepto.page)

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$.

<!--more-->

```cpp
// 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 <span class="slang" data-zh="傻逼题" data-gloss="shǎbī tí, “idiot problem”. Vulgar: a problem so easy that it is insulting.">stupid-easy problem</span>.” — [XG_Zepto](https://zepto.page)

This problem is quite interesting. Consider a node with two child chains:

![graph - Twelve Chimes of the Spring Festival](media/graph-1.png)

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.

```cpp
// 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;
}
```
