QOJ.ac

QOJ

时间限制: 4.5 s 内存限制: 1024 MB 总分: 100

#5403. Tree Numbering Technique

统计

Given a tree with $n$ nodes, labeled from $1$ to $n$. The root of the tree is $1$.

Given a sequence of $m$ positive integers $a_1, \cdots, a_m$, where $\forall i$, $a_i \in [1, n]$.

There are $q$ queries. Each query provides $2k$ integers $l_1, r_1, \cdots, l_k, r_k$, where $\forall i$, $1 \leq l_i \leq r_i \leq m$.

Let the sequence formed by concatenating the $k$ sub-intervals of $a$ be $b_1, \cdots, b_s$, where the sequence is $a_{l_1}, a_{l_1+1}, \cdots, a_{r_1}, a_{l_2}, a_{l_2+1}, \cdots, a_{r_2}, \cdots, a_{l_k}, a_{l_k + 1}, \cdots, a_{r_k}$. You need to find the number of indices $i$ that satisfy the following conditions:

  • $1 \leq i \leq s$.
  • $\forall 1 \leq j \leq i$, $b_i = b_j$ or $b_i$ is an ancestor of $b_j$.

Input

The first line contains three positive integers $n$, $m$, and $q$.

The next $n-1$ lines each contain two positive integers $x_i$ and $y_i$, representing an edge connecting $x_i$ and $y_i$ in the tree.

The next line contains $m$ integers, representing $a_1, \cdots, a_m$.

The next $q$ lines each start with a positive integer $k$, followed by $2k$ positive integers representing $l_1, r_1, l_2, r_2, \cdots, l_k, r_k$.

Output

For each query, output one integer on a new line representing the answer.

Examples

Input 1

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

Output 1

4
2
2

Subtasks

Since the input data size can reach 27MB, please be mindful of your I/O performance.

Subtask 1 ($10\%$): The given tree is a chain.

Subtask 2 ($20\%$): $k = 1$.

Subtask 3 ($30\%$): $n, m, \sum k \leq 100\,000$.

Subtask 4 ($40\%$): No additional constraints.

For all test cases, $n, m, \sum k \leq 700\,000$.

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.