QOJ.ac

QOJ

時間限制: 1 s 記憶體限制: 512 MB 總分: 100

#2977. Old Words

统计

You have already solved five problems; why not sing a song of "Old Words" under this great tree to express your feelings? The final problem is about this tree, and its description is very simple.

Given a rooted tree with $n$ nodes, labeled $1 \sim n$, where node $1$ is the root. Given a constant $k$. Given $Q$ queries, each query provides $x, y$. Calculate: $$\sum_{i \le x} depth(lca(i, y))^k$$

$lca(x, y)$ denotes the lowest common ancestor of node $x$ and node $y$ in the rooted tree. $depth(x)$ denotes the depth of node $x$, where the depth of the root is $1$. Since the answer can be very large, you only need to output the result modulo $998244353$.

Input

The input contains $n + Q$ lines. The first line contains three positive integers $n, Q, k$. For the next $n-1$ lines, each line contains a positive integer $f_i$ ($1 \le f_i \le n$), representing the parent of node $i$ (for $i = 2 \sim n$). The next $Q$ lines each contain two positive integers $x, y$ ($1 \le x, y \le n$), representing a query.

Output

The output contains $Q$ lines, each containing an integer representing the result modulo $998244353$.

Examples

Input 1

5 5 2
1
4
1
2
4 3
5 4
2 5
1 2
3 2

Output 1

15
11
5
1
6

Note

The input tree:

1
| \
2 4 - 3
|
5

The depths of the nodes are $1, 2, 3, 2, 3$ respectively. For the first query $x = 4, y = 3$, it is easy to find: $lca(1, 3) = 1$ $lca(2, 3) = 1$ $lca(3, 3) = 3$ $lca(4, 3) = 4$ Thus $depth(1)^2 + depth(1)^2 + depth(3)^2 + depth(4)^2 = 1 + 1 + 9 + 4 = 15$.

Data Scale and Convention

Test Case ID $n$ Size $Q$ Size $k$ Size Note
1 $n \le 2,000$ $Q \le 2,000$ $1 \le k \le 10^9$ None
2
3
4
5 $n \le 50,000$ $Q \le 50,000$ $1 \le k \le 10^9$ There exists a node with depth $n$
6
7
8
9 $n \le 50,000$ $Q = n$ $1 \le k \le 10^9$ For the $i$-th query, $x = i$
10
11 $n \le 50,000$ $Q \le 50,000$ $k = 1$ None
12
13 $k = 2$
14
15 $k = 3$
16
17 $1 \le k \le 10^9$
18
19
20

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.