#P5689. [CSP-S 2019 江西] 多叉堆

[CSP-S 2019 江西] 多叉堆

Description

A d-ary heap is a tree-structured data structure. In this problem, we only consider the min-heap, which satisfies that for every node except the root, the weight of the node is not less than the weight of its parent. Except for leaf nodes, every node has at least one child.

Initially, there are nn nodes, numbered 0n10 \sim n - 1. Each node is a single-node tree rooted at itself. Then there are qq operations in order, and each operation is one of the following two types:

  • 1 x y: Choose nodes xx and yy that are not in the same tree. Attach the root of the tree containing xx directly under the root of the tree containing yy. Then the trees containing xx and yy merge into one tree.

  • 2 x: Choose a node xx, and let the number of nodes in the tree containing xx be sizesize. You need to compute how many different d-ary heaps can be produced by filling the sizesize numbers 0size10 \sim size - 1 into the nodes of the tree containing xx. Two heaps are different if and only if there exists a node whose filled value is different. Since the answer may be very large, you only need to output the result modulo 109+710^9+7.

Input Format

The first line contains two integers n,qn,q, whose meanings are described above.

In the next qq lines, each line is in one of the following formats:

  • 1 x' y': You need to compute x=(x+ans)modn,y=(y+ans)modnx=(x'+ans) \bmod n , y=(y'+ans) \bmod n to get the actual xx and yy. The input guarantees that the trees containing xx and yy are different.

  • 2 x': You need to compute x=(x+ans)modnx=(x'+ans) \bmod n to get the actual xx.

Here, ansans denotes the output of the previous type 22 operation, and initially ans=0ans=0.

Output Format

For each type 22 operation, output one line with one integer representing the answer.

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

Hint

Sample 1 Explanation

In the 1st operation, attach the root 11 of the tree containing 11 under the root 00 of the tree containing 00.

In the 2nd operation, attach the root 22 of the tree containing 22 under the root 00 of the tree containing 00.

In the 3rd operation, the tree containing 11 is shown in Figure 1. Filling 0,1,20,1,2 with [0,1,2][0,1,2] and [0,2,1][0,2,1] can produce 22 different heaps.

In the 4th operation, x=(3+2)mod5=0x=(3+2) \bmod 5=0, y=(1+2)mod5=3y=(1+2) \bmod 5=3. Attach the root 00 of the tree containing 00 under the root 33 of the tree containing 33.

In the 5th operation, x=(2+2)mod5=4x=(2+2) \bmod 5=4, y=(0+2)mod5=2y=(0+2) \bmod 5=2. Attach the root 44 of the tree containing 44 under the root 33 of the tree containing 22.

In the 6th operation, x=(4+2)mod5=1x=(4+2) \bmod 5=1. The tree containing 11 is shown in Figure 2. Filling 040\sim 4 with [1,2,3,0,4][1,2,3,0,4], [1,3,2,0,4][1,3,2,0,4], [1,2,4,0,3][1,2,4,0,3], [1,4,2,0,3][1,4,2,0,3], [1,3,4,0,2][1,3,4,0,2], [1,4,3,0,2][1,4,3,0,2], [2,4,3,0,1][2,4,3,0,1], [2,3,4,0,1][2,3,4,0,1] can produce 88 different heaps.

Constraints and Conventions

For 100%100\% of the data, 0x,y<n3×1050\le x',y' <n \le 3\times 10^5, 1q3×1051\le q\le 3\times 10^5.

For different test points, we define:

::cute-table{tuack}

Test Point ID nn\le qq\le Special Property 11 Special Property 22
11 1010 2020 Yes No
22 ^ No Yes
33 ^ No
44 ^
55 300300 600600 Yes
66 ^ No Yes
77 ^ No
88 20002000 40004000 Yes ^
99 ^ No Yes
1010 ^ No
1111 5×1045\times10^4 10510^5 Yes ^
1212 ^ No Yes
1313 ^ No
1414 ^
1515
1616 3×1053\times10^5 Yes
1717 ^ No Yes
1818 ^ No
1919 ^
2020

Special Property 11: There exists 1i<n1\le i<n , such that the first ii operations are all type 11 operations, and all the remaining operations are type 22 operations.

Special Property 22: For all inputs, xx and yy themselves are the roots of their respective trees.

Thanks to @Fairicle for providing the testdata.

Translated by ChatGPT 5