QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 128 MB 満点: 100

#7453. rplexq

統計

Given a rooted tree with $n$ nodes, where the $i$-th node is labeled with the number $i$.

There are $m$ queries. Each query provides $l, r, x$. Find the number of pairs of node labels $(i, j)$ such that $l \le i < j \le r$ and the lowest common ancestor (LCA) of $i$ and $j$ is node $x$.

Input

The first line contains three integers $n$, $m$, and $rt$, where $rt$ is the label of the root node.

The next $n-1$ lines each contain two integers $u$ and $v$, representing an edge.

The next $m$ lines each contain three integers $l$, $r$, and $x$, representing a query.

Output

Output $m$ lines, each containing the answer to the corresponding query.

Examples

Input 1

10 10 7
4 2
10 4
3 2
6 10
9 2
7 3
1 4
8 2
5 3
8 10 10
2 6 2
3 6 2
4 6 4
3 10 2
8 8 10
3 10 4
2 3 2
2 6 4
1 7 10

Output 1

0
2
0
1
7
0
2
0
1
0

Note

For 2 6 2: the valid pairs are $(2, 4)$ and $(2, 6)$.

For 4 6 4: the valid pair is $(4, 6)$.

For 3 10 2: the valid pairs are $(4, 8)$, $(4, 9)$, $(6, 8)$, $(6, 9)$, $(8, 9)$, $(8, 10)$, and $(9, 10)$.

For 3 10 4: the valid pairs are $(4, 6)$ and $(4, 10)$.

For 2 6 4: the valid pair is $(4, 6)$.

Subtasks

Idea: Ynoi, Solution: nzhtl1477, Code: nzhtl1477, Data: nzhtl1477

For $100\%$ of the data, $1 \le n, m \le 2 \cdot 10^5$, $1 \le l, r, x \le n$.

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.