#P17144. [NOI 2026] 木棉

[NOI 2026] 木棉

Background

The statement and sample attachments come from QOJ。

When submitting to Luogu, there is no need to include the header #include "kapok.h"。Just copy

std::vector<bool> kapok(
    int c, int n, int m, std::vector<int> a, std::vector<int> l, std::vector<int> r, std::vector<int> x, std::vector<int> y
);

to the beginning of your program, and compile with a C++17 or higher compiler.

Problem Description

Those kapok trees in the old home are still growing in Little NN's gradually blurred memories. Little NN's memories of the old home can be represented by a sequence of length nn, [a0,a1,…,an−1][a_0,a_1,\ldots,a_{n-1}]。

Each kapok tree in the old home is an unrooted tree with labeled nodes. Little NN's impression of a kapok tree can be described by an interval [l,r)[l,r) of her old-home memory:

  • This tree has k=r−l+2k=r-l+2 nodes, with node labels 0∼k−10\sim k-1。
  • $[\min(a_l,k-1),\min(a_{l+1},k-1),\ldots,\min(a_{r-1},k-1)]$ is the Prüfer sequence of this tree, where the definition of Prüfer sequence is given in the [Hint] section.

While recalling the past, Little NN also asked you mm queries. The ii-th query (0≤i<m0\le i<m) is:

  • On the kapok tree corresponding to the interval [li,ri)[l_i,r_i) in the old-home memory, are nodes xi,yix_i,y_i adjacent?

Implementation Details

Contestants do not need to, and should not, implement the main function.

Contestants need to ensure that the submitted source file includes the header kapok.h, i.e. add the following code at the beginning:

#include "kapok.h"

Contestants need to implement the following function in the submitted source file kapok.cpp:

std::vector<bool> kapok(
    int c, int n, int m, std::vector<int> a, std::vector<int> l, std::vector<int> r, std::vector<int> x, std::vector<int> y
);
  • c,n,mc,n,m represent the test point index, the length of the old-home memory sequence, and the number of queries, respectively. c=0c=0 means this test point is a sample.
  • aa is the old-home memory sequence.
  • l,rl,r are the two endpoints of the interval given in each query.
  • x,yx,y are the labels of the two nodes given in each query.
  • This function needs to return a sequence of length exactly mm, f0,f1,…,fm−1f_0,f_1,\ldots,f_{m-1}, where fif_i (0≤i<m0\le i<m) is the answer to the ii-th query.
  • For each test point, this function will be called exactly once by the judge. template_kapok.cpp in this problem directory is the provided sample code. Contestants may refer to it and implement their own code.

Input Format

Test Program Method

Contestants can compile an executable in this problem directory using the following command:

g++ grader.cpp kapok.cpp -o kapok -O2 -std=c++14 -static

For the compiled executable kapok:

  • The executable will read input from standard input in the following format:
    • The first line contains three non-negative integers c,n,mc,n,m。
    • The second line contains nn non-negative integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1}。
    • The (i+3)(i+3)-th line (0≤i<m0\le i<m) contains four non-negative integers li,ri,xi,yil_i,r_i,x_i,y_i。
  • The executable will output to standard output in the following format:
    • The (i+1)(i+1)-th line (0≤i<m0\le i<m) contains one non-negative integer, where 00 means fif_i is false, and 11 means fif_i is true。

Output Format

Hint

Sample 11 Explanation

  • The tree corresponding to interval [0,3)[0,3) has 55 nodes, and its Prüfer sequence is [2,0,2][2,0,2]. The edge set is {(1,2),(0,3),(0,2),(2,4)}\{(1,2),(0,3),(0,2),(2,4)\}. Therefore, nodes 0,20,2 are adjacent.
  • The tree corresponding to interval [3,5)[3,5) has 44 nodes, and its Prüfer sequence is [3,0][3,0]. The edge set is {(1,3),(0,2),(0,3)}\{(1,3),(0,2),(0,3)\}. Therefore, nodes 1,21,2 are not adjacent.
  • The tree corresponding to interval [0,0)[0,0) has 22 nodes, and its Prüfer sequence is empty. The only edge is (0,1)(0,1). Therefore, nodes 0,10,1 are adjacent.

Sample 22

See kapok/kapok2.in and kapok/kapok2.ans in the contestants' directory.

This sample satisfies the constraints of test points 3∼53\sim5。

Sample 33

See kapok/kapok3.in and kapok/kapok3.ans in the contestants' directory.

This sample satisfies the constraints of test points 3∼53\sim5。

Constraints

For all testdata:

  • 1≤n,m≤2×1051\le n,m\le2\times10^5。
  • For all 0≤i<n0\le i<n, 0≤ai<n+20\le a_i<n+2。
  • For all 0≤i<m0\le i<m, 0≤li≤ri≤n0\le l_i\le r_i\le n and 0≤xi,yi<ri−li+20\le x_i,y_i<r_i-l_i+2。

::cute-table{tuack} | Test point index | n,m≤n,m\le | Special property | |:-:|:-:|:-:| | 1,21,2 | 500500 | None | | 3∼53\sim5 | 50005000 | ^ | | 6,76,7 | 2×1052\times10^5 | For all 0≤i<n0\le i<n, ai<10a_i<10 | | 8,98,9 | ^ | For all 0≤i<n0\le i<n, ai<103a_i<10^3 | | 10,1110,11 | ^ | For all 0≤i<m0\le i<m, xi,yi<10x_i,y_i<10 | | 12,1312,13 | ^ | For all 0≤i<m0\le i<m, xi,yi<103x_i,y_i<10^3 | | 14∼1614\sim16 | ^ | AA | | 17∼1917\sim19 | ^ | For all 0≤i<m0\le i<m, ri=nr_i=n | | 20∼2220\sim22 | 10510^5 | None | | 23∼2523\sim25 | 2×1052\times10^5 | ^ |

Special property AA: For all 0≤i<m0\le i<m, after li,ril_i,r_i are given, (xi,yi)(x_i,y_i) are generated independently and uniformly at random from all ordered pairs of nodes that satisfy the limits.

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

Hint

Sample 11 Explanation

  • The tree corresponding to interval [0,3)[0,3) has 55 nodes, and its Prüfer sequence is [2,0,2][2,0,2]. The edge set is {(1,2),(0,3),(0,2),(2,4)}\{(1,2),(0,3),(0,2),(2,4)\}. Therefore, nodes 0,20,2 are adjacent.
  • The tree corresponding to interval [3,5)[3,5) has 44 nodes, and its Prüfer sequence is [3,0][3,0]. The edge set is {(1,3),(0,2),(0,3)}\{(1,3),(0,2),(0,3)\}. Therefore, nodes 1,21,2 are not adjacent.
  • The tree corresponding to interval [0,0)[0,0) has 22 nodes, and its Prüfer sequence is empty. The only edge is (0,1)(0,1). Therefore, nodes 0,10,1 are adjacent.

Sample 22

See kapok/kapok2.in and kapok/kapok2.ans in the contestants' directory.

This sample satisfies the constraints of test points 3∼53\sim5。

Sample 33

See kapok/kapok3.in and kapok/kapok3.ans in the contestants' directory.

This sample satisfies the constraints of test points 3∼53\sim5。

Constraints

For all testdata:

  • 1≤n,m≤2×1051\le n,m\le2\times10^5。
  • For all 0≤i<n0\le i<n, 0≤ai<n+20\le a_i<n+2。
  • For all 0≤i<m0\le i<m, 0≤li≤ri≤n0\le l_i\le r_i\le n and 0≤xi,yi<ri−li+20\le x_i,y_i<r_i-l_i+2。

::cute-table{tuack} | Test point index | n,m≤n,m\le | Special property | |:-:|:-:|:-:| | 1,21,2 | 500500 | None | | 3∼53\sim5 | 50005000 | ^ | | 6,76,7 | 2×1052\times10^5 | For all 0≤i<n0\le i<n, ai<10a_i<10 | | 8,98,9 | ^ | For all 0≤i<n0\le i<n, ai<103a_i<10^3 | | 10,1110,11 | ^ | For all 0≤i<m0\le i<m, xi,yi<10x_i,y_i<10 | | 12,1312,13 | ^ | For all 0≤i<m0\le i<m, xi,yi<103x_i,y_i<10^3 | | 14∼1614\sim16 | ^ | AA | | 17∼1917\sim19 | ^ | For all 0≤i<m0\le i<m, ri=nr_i=n | | 20∼2220\sim22 | 10510^5 | None | | 23∼2523\sim25 | 2×1052\times10^5 | ^ |

Special property AA: For all 0≤i<m0\le i<m, after li,ril_i,r_i are given, (xi,yi)(x_i,y_i) are generated independently and uniformly at random from all ordered pairs of nodes that satisfy the limits.

Hint

For an unrooted tree TT whose nodes are labeled 0∼c−10\sim c-1 (c≥2c\ge2):

  • Perform c−2c-2 operations in order, where the ii-th operation (0≤i<c−20\le i<c-2) is:
    • Find the current leaf with the smallest label viv_i。
    • Record the label of its unique adjacent node pip_i。
    • Then delete viv_i and its only incident edge from TT。
  • The resulting sequence of length c−2c-2, [p0,p1,…,pc−3][p_0,p_1,\ldots,p_{c-3}], is the Prüfer sequence of TT。

Translated by ChatGPT 5