#P16903. [CCO 2026] Beyond Counting

[CCO 2026] Beyond Counting

Problem Description

Andy Jiang is studying data structures. One day, his friend Austin Zhu gave him a task on trees.

Austin provided a tree with NN vertices, numbered from 11 to NN. Each vertex ii has a value AiA_i.

For each query, Austin asked Andy to consider a path between two vertices sis_i and tit_i, and compute how many times a given value xix_i appears on that path.

Andy glanced at the problem and thought that this task was too easy for him.

Instead of just counting occurrences, Andy decided to challenge himself further. For each query, he wants to know how the frequency of xix_i compares to other values on the same path.

Formally, for each query (si,ti,xi)(s_i,t_i,x_i):

  • Consider the simple path from sis_i to tit_i.
  • Let cnt(y)\operatorname{cnt}(y) be the number of occurrences of value yy on this path.

Andy defines the rank of xix_i as $1 + |\{y \mid \operatorname{cnt}(y) > \operatorname{cnt}(x_i)\}|$.

That is, 11 plus the number of distinct values that appear more frequently than xix_i on the path. Note that it is possible the value xix_i does not appear on the path, i.e. cnt(xi)=0\operatorname{cnt}(x_i)=0. In this case, you should return 11 plus the number of distinct values on the path.

In some test cases, the queries are given in an encoded form as described below.

Help Andy compute the rank of xix_i for each query.

Input Format

The first line contains 33 positive integers NN, QQ, and TT (1N,Q1051 \le N,Q \le 10^5, T{0,1}T \in \{0,1\}).

The second line contains NN integers A1,A2,,ANA_1, A_2,\ldots,A_N (1Ai1091 \le A_i \le 10^9).

The next N1N-1 lines each contain two integers ui,viu_i, v_i (1ui,viN1 \le u_i, v_i \le N), representing the ii-th edge.

Each of the next QQ lines contains 33 integers s^i,t^i,x^i\hat{s}_i, \hat{t}_i, \hat{x}_i (1s^i,t^iN1 \le \hat{s}_i, \hat{t}_i \le N, 1x^i1091 \le \hat{x}_i \le 10^9), describing the ii-th query.

Let last0=0\operatorname{last}_0 = 0. For each query i=1,2,,Qi = 1,2,\ldots,Q, the actual parameters are defined as:

$$\begin{aligned} s_i &= ((\hat{s}_i + \operatorname{last}_{i-1} \times T - 1) \bmod N) + 1,\\ t_i &= ((\hat{t}_i + \operatorname{last}_{i-1} \times T - 1) \bmod N) + 1,\\ x_i &= ((\hat{x}_i + \operatorname{last}_{i-1} \times T - 1) \bmod 10^9) + 1. \end{aligned}$$

After computing the answer to the ii-th query, set

$$\operatorname{last}_i = \text{answer to the } i\text{-th query}.$$

It may also be useful to note that “mod” corresponds to the %\% operator in most programming languages, indicating the remainder after division. For example, 5mod3=25 \bmod 3 = 2 and 17mod4=117 \bmod 4 = 1.

Output Format

For each query, output the answer to the query on a new line.

5 5 0
1 2 3 4 4
4 3
2 5
1 3
3 2
4 5 3
4 5 4
4 5 5
1 5 1
1 5 4
2
1
4
1
1
5 5 1
1 2 3 4 4
4 3
2 5
1 3
3 2
4 5 3
2 3 2
3 4 4
2 1 999999997
5 4 3
2
1
4
1
1

Hint

The following table shows how the 2525 available marks are distributed:

Marks Awarded Bounds on N,QN,Q Bounds on TT Additional Constraints
11 mark 1N,Q1031 \le N,Q \le 10^3 T=1T=1 None.
1N,Q1051 \le N,Q \le 10^5 T=0T=0 All sis_i are equal.
44 marks ^ T=1T=1 ^
T=0T=0 ui=iu_i=i and vi=i+1v_i=i+1.
55 marks T=1T=1 ^
33 marks T=0T=0 None.
77 marks T=1T=1 ^