#P10305. [THUWC 2020] 序排泡冒
[THUWC 2020] 序排泡冒
Background
You are given a peach tree .
You don't need to think of the peach.
Because I'm going to talk about the tree.
Problem Description
Bubble sort is a well-known sorting algorithm. Its idea is to repeatedly swap two adjacent elements that are in the wrong order. Since the total number of inversions keeps decreasing, bubble sort must terminate. The bubble sort algorithm that sorts in nondecreasing order can be described by the following pseudocode.
for (int i = 1; i <= k; i++)
for (int j = 0; j < n-1; j++)
if (a[j] > a[j+1]) {
tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
Here, is called the number of passes of bubble sort. When , it is guaranteed that the sequence is sorted in nondecreasing order.
Reverse Bubble Sort is a lesser-known algorithm. Given a sequence of length and a parameter , it outputs all possible sequences such that after applying passes of bubble sort to , the result becomes .
You are given a tree with nodes numbered . Each node has a weight , and it is guaranteed that form a permutation of . In other words:
- For , we have .
- For any , there exists a node such that .
There are queries. Each query gives two nodes and a parameter . Let be the sequence consisting of the node weights of all nodes on the unique simple path from to in order. Clearly, is and is . You need to compute, after performing Reverse Bubble Sort on sequence with parameter , how many permutation sequences will be output. In other words, compute how many sequences satisfy that after passes of bubble sort, they become sequence .
Since the answer can be very large, output it modulo (that is, output the remainder when the answer is divided by ).
Input Format
The first line contains two natural numbers separated by spaces, denoting the number of nodes and the number of queries.
The next line contains integers separated by spaces, denoting the weight of each node.
The next lines each contain two numbers , indicating that there is an edge connecting and in the tree. The input guarantees that all edges form a tree.
The next lines each contain a query , whose meaning has been explained in the description.
Output Format
Output lines. The -th line contains one integer, denoting the answer to the -th query modulo .
4 4
1 3 2 4
1 2
2 3
3 4
1 1 1
1 2 1
1 3 1
1 4 1
1
2
0
4
见附件中的 2.in。
见附件中的 2.ans。
Hint
[Sample Explanation #1]
In this sample, the tree is a chain of length .
- For the first query, the visited node sequence is , the weight sequence is , and Reverse Bubble Sort outputs .
- For the second query, the visited node sequence is , the weight sequence is , and Reverse Bubble Sort outputs .
- For the third query, the visited node sequence is , the weight sequence is , and Reverse Bubble Sort outputs .
- For the fourth query, the visited node sequence is , the weight sequence is , and Reverse Bubble Sort outputs .
[Subtasks]
| Subtask | Type | Score | |||
|---|---|---|---|---|---|
For all testdata, .
Meaning of data types:
- : The tree is a chain, i.e. , and all queries satisfy .
- : The tree is a chain, i.e. , and all queries satisfy .
- : No special restrictions.
[Hint]
Don't be afraid of the pain in climbing.
Your tough journey is just beginning.
But the fruit you will gain is not only peaches.
Keep counting on the tree! Young gentlemen and ladies.
Hope in your heart will lead you to the destiny.
Translated by ChatGPT 5