#P10220. [省选联考 2024] 迷宫守卫

[省选联考 2024] 迷宫守卫

Problem Description

Alice owns a maze. The maze can be modeled as a perfect binary tree with 2n2^n leaf nodes, and the total number of nodes is (2n+11)(2^{n+1} - 1). The nodes are numbered in order from 11 to (2n+11)(2^{n+1} - 1). Nodes numbered 2n(2n+11)2^n \sim (2^{n+1} - 1) are leaf nodes, and nodes numbered 1(2n1)1 \sim (2^n - 1) are non-leaf nodes. For each non-leaf node 1u(2n1)1 \le u \le (2^n - 1), its left child is numbered 2u2u, and its right child is numbered (2u+1)(2u + 1).

Each non-leaf node has a stone statue guard. Initially, all stone statue guards are asleep. Waking up the stone statue guard at node uu costs wuw_u mana.

Each leaf node has a rune. The rune at leaf node vv is denoted by qvq_v. It is guaranteed that q2n,q2n+1,,q2n+11q_{2^n}, q_{2^n+1},\cdots, q_{2^{n+1}-1} form a permutation of 12n1 \sim 2^n.

An explorer initially holds an empty sequence QQ and starts from node 11, acting according to the following rules:

  • When arriving at a leaf node vv, append the rune qvq_v to the end of sequence QQ, then return to its parent node.
  • When arriving at a non-leaf node uu:
    • If the stone statue guard at this node has been awakened, the explorer must go to the left child first, then (after returning from the left child) go to the right child, then (after returning from the right child) finally return to the parent node.
    • If the stone statue guard at this node is asleep, the explorer may choose either of the following:
      • Go to the left child first, then the right child, and finally return to the parent node.
      • Go to the right child first, then the left child, and finally return to the parent node.

When the explorer returns to node 11, the exploration ends. It can be proven that the explorer will visit each leaf node exactly once, so at this time the length of QQ is 2n2^n.

Explorer Bob is about to enter the maze. He wants the final sequence QQ to be as lexicographically small as possible. In contrast, Alice wants QQ to be as lexicographically large as possible.

Before Bob sets off, Alice may choose some stone statue guards whose total mana cost does not exceed KK and wake them up. When Bob sets off, he knows exactly which statues Alice has awakened. If both sides play optimally, find the final value of sequence QQ.

For two sequences Q1,Q2Q_1, Q_2 of length 2n2^n, we say Q1Q_1 is lexicographically smaller than Q2Q_2 if and only if the following holds:

  • i[1,2n]\exist i \in [1, 2^n] such that:
    • 1j<iQ1,j=Q2,j\forall 1 \le j < i,Q_{1,j} = Q_{2,j}
    • Q1,i<Q2,iQ_{1,i} < Q_{2,i}

Input Format

This problem has multiple test cases. The first line contains a positive integer TT, indicating the number of test cases.

Then follow TT test cases. For each test case:

  • The first line contains two integers n,Kn, K, representing the maze size and the upper limit of mana Alice can use to wake stone statue guards.
  • The second line contains (2n1)(2^n - 1) integers w1,w2,,w2n1w_1, w_2, \cdots, w_{2^n-1}, representing the mana cost to wake each stone statue guard.
  • The third line contains 2n2^n integers q2n,q2n+1,,q2n+11q_{2^n}, q_{2^n+1},\cdots, q_{2^{n+1}-1}, representing the runes on each leaf node.

Output Format

For each test case, output one line containing 2n2^n integers Q1,Q2,,Q2nQ_1, Q_2, \cdots, Q_{2^n}, representing the final value of sequence QQ when both sides play optimally.

3
1 0
1
2 1
1 1
1
2 1
3 3
3 2 1 2 1 2 1
4 2 6 3 7 1 5 8
1 2
2 1
2 4 6 3 5 8 7 1

Hint

[Sample 1 Explanation]

  • In the first test case, Alice cannot wake any stone statue guard. Bob can choose to visit leaf node 33 first and then leaf node 22, obtaining Q={1,2}Q = \{1, 2\}.
  • In the second test case, Alice can wake the stone statue guard at node 11. Bob can only visit leaf node 22 first and then leaf node 33, obtaining Q={2,1}Q = \{2, 1\}.
  • In the third test case, Alice’s optimal strategy is to wake the stone statue guards at nodes 5,65, 6.

[Sample 2]

See the attached maze2.in/ans.

This test case satisfies special property A.

[Sample 3]

See the attached maze3.in/ans.

This test case satisfies special property B.

[Sample 4]

See the attached maze4.in/ans.

[Sample 5]

See the attached maze5.in/ans.

[Subtasks]

Let 2n\sum 2^n denote the sum of 2n2^n over all test cases within a single test point. For all test cases, it is guaranteed that:

  • 1T1001\le T \le 100
  • 1n161\le n \le 1612n1051 \le \sum 2^n \le 10^5
  • 0K10120\le K \le 10^{12}
  • 1u(2n1)\forall 1 \le u \le (2^n-1)0wu10120 \le w_u \le 10^{12}
  • q2n,q2n+1,,q2n+11q_{2^n},q_{2^n+1},\cdots,q_{2^{n+1}-1} form a permutation of 12n1\sim 2^n.
Test Point ID nn\le 2n\sum 2^n \le Special Property
151\sim 5 44 8080 None
66 66 200200 A
787\sim 8 B
9109\sim 10 None
1111 1111 40004000 A
121312\sim 13 B
141514\sim 15 None
1616 1616 10510^5 A
171817\sim 18 B
192019\sim 20 None

Special property A: 2nv(2n+11)\forall 2^n \le v \le (2^{n+1}-1)qv=(2n+1v)q_v = (2^{n+1}-v)

Special property B: 1u(2n1)\forall 1 \le u \le (2^n-1)wu=1w_u = 1

Translated by ChatGPT 5