#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 vertices, numbered from to . Each vertex has a value .
For each query, Austin asked Andy to consider a path between two vertices and , and compute how many times a given value 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 compares to other values on the same path.
Formally, for each query :
- Consider the simple path from to .
- Let be the number of occurrences of value on this path.
Andy defines the rank of as $1 + |\{y \mid \operatorname{cnt}(y) > \operatorname{cnt}(x_i)\}|$.
That is, plus the number of distinct values that appear more frequently than on the path. Note that it is possible the value does not appear on the path, i.e. . In this case, you should return 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 for each query.
Input Format
The first line contains positive integers , , and (, ).
The second line contains integers ().
The next lines each contain two integers (), representing the -th edge.
Each of the next lines contains integers (, ), describing the -th query.
Let . For each query , 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 -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, and .
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 available marks are distributed:
| Marks Awarded | Bounds on | Bounds on | Additional Constraints |
|---|---|---|---|
| mark | None. | ||
| All are equal. | |||
| marks | ^ | ^ | |
| and . | |||
| marks | ^ | ||
| marks | None. | ||
| marks | ^ |