#P10305. [THUWC 2020] 序排泡冒

[THUWC 2020] 序排泡冒

Background

You are given a peach tree TT.

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 a[0],a[1],,a[n1]a[0], a[1], \dots, a[n-1] 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, kk is called the number of passes of bubble sort. When k=nk = n, it is guaranteed that the sequence is sorted in nondecreasing order.

Reverse Bubble Sort is a lesser-known algorithm. Given a sequence aa of length nn and a parameter kk, it outputs all possible sequences bb such that after applying kk passes of bubble sort to bb, the result becomes aa.

You are given a tree TT with nodes numbered 1,2,,n1,2,\dots,n. Each node uu has a weight p(u)p(u), and it is guaranteed that p(1),p(2),,p(n)p(1), p(2), \dots, p(n) form a permutation of {1,2,,n}\{1,2,\dots,n\}. In other words:

  1. For uvu \ne v, we have p(u)p(v)p(u) \ne p(v).
  2. For any i{1,2,,n}i \in \{1, 2, \dots, n\}, there exists a node uu such that p(u)=ip(u)=i.

There are mm queries. Each query gives two nodes u,vu, v and a parameter kk. Let t1,t2,,tlt_1, t_2, \dots, t_l be the sequence consisting of the node weights of all nodes on the unique simple path from uu to vv in order. Clearly, t1t_1 is p(u)p(u) and tlt_l is p(v)p(v). You need to compute, after performing Reverse Bubble Sort on sequence tt with parameter kk, how many permutation sequences will be output. In other words, compute how many sequences satisfy that after kk passes of bubble sort, they become sequence tt.

Since the answer can be very large, output it modulo 998,244,353998,244,353 (that is, output the remainder when the answer is divided by 998,244,353998,244,353).

Input Format

The first line contains two natural numbers n,mn, m separated by spaces, denoting the number of nodes and the number of queries.

The next line contains nn integers p(1),p(2),,p(n)p(1), p(2), \dots, p(n) separated by spaces, denoting the weight of each node.

The next n1n-1 lines each contain two numbers xi,yix_i, y_i, indicating that there is an edge connecting xix_i and yiy_i in the tree. The input guarantees that all edges form a tree.

The next mm lines each contain a query ui,vi,kiu_i, v_i, k_i, whose meaning has been explained in the description.

Output Format

Output mm lines. The ii-th line contains one integer, denoting the answer to the ii-th query modulo 998,244,353998,244,353.

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

  1. For the first query, the visited node sequence is [1][1], the weight sequence is [1][1], and Reverse Bubble Sort outputs {[1]}\{[1]\}.
  2. For the second query, the visited node sequence is [1,2][1,2], the weight sequence is [1,3][1,3], and Reverse Bubble Sort outputs {[1,3],[3,1]}\{[1,3], [3,1]\}.
  3. For the third query, the visited node sequence is [1,2,3][1,2,3], the weight sequence is [1,3,2][1,3,2], and Reverse Bubble Sort outputs {}\{\}.
  4. For the fourth query, the visited node sequence is [1,2,3,4][1,2,3,4], the weight sequence is [1,3,2,4][1,3,2,4], and Reverse Bubble Sort outputs {[1,3,4,2],[3,1,4,2],[1,4,3,2],[4,1,3,2]}\{[1,3,4,2], [3,1,4,2], [1,4,3,2], [4,1,3,2]\}.

[Subtasks]

Subtask nn mm kk Type Score
11 5×105\le 5 \times 10^{5} =0= 0 33 11
22 10\le 10 1\le 1 n\le n 11 44
33 5×105\le 5 \times 10^{5} 33
44 103\le 10^{3} 103\le 10^{^{3}} 11 1010
55 5×105\le 5 \times 10^{5} 1212
66 22 1313
77 103\le 10^{3} 33 55
88 105\le 10^{5} 2525
99 5×105\le 5 \times 10^{5} 2727

For all testdata, 1n,m5×105,0kn1\le n,m\le 5\times 10^5, 0\le k\le n.

Meaning of data types:

  • Type=1\mathrm{Type}=1: The tree is a chain, i.e. xi=i,yi=i+1x_i = i, y_i=i+1, and all queries satisfy u1=1,vi=nu_1=1, v_i=n.
  • Type=2\mathrm{Type}=2: The tree is a chain, i.e. xi=i,yi=i+1x_i = i, y_i=i+1, and all queries satisfy ui<viu_i < v_i.
  • Type=3\mathrm{Type}=3: 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