#P6782. [Ynoi2008] rplexq

[Ynoi2008] rplexq

Problem Description

Given a rooted tree with nn nodes, the index of node ii is ii.

There are mm queries. Each query gives l,r,xl, r, x. You need to count how many pairs of node indices (i,j)(i, j) satisfy li<jrl \le i < j \le r, and the lowest common ancestor of ii and jj is node xx.

Input Format

The first line contains three integers nn mm rtrt, where rtrt is the index of the root node.

The next n1n - 1 lines each contain two integers uu vv, representing an edge.

The next mm lines each contain three integers ll rr xx, representing a query.

Output Format

Output mm lines. Each line is the answer to the corresponding query.

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
0
2
0
1
7
0
2
0
1
0

Hint

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

Constraints: for 100%100\% of the testdata, 1n,m21051 \le n, m \le 2 \cdot 10^5, 1l,r,xn1 \le l, r, x \le n.

Sample Explanation

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

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

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

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

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

Translated by ChatGPT 5