#P11364. [NOIP2024] 树上查询
[NOIP2024] 树上查询
Description
One day, Xiao S and her friend Xiao N studied a tree with nodes.
This is a rooted tree with root node numbered . The depth of a node is defined as the number of nodes on the simple path from to .
Additionally, is defined as the lowest common ancestor (LCA) of all nodes with numbers in , i.e., the deepest node that is an ancestor of all nodes .
Xiao N posed queries about this tree. For each query, he provides three parameters , asking for the maximum depth among the LCAs of all continuous subintervals of with length at least . Formally, compute:
$$\max_{l\le l'\le r'\le r \land r'-l'+1\ge k}\text{dep}_{\text{LCA*}(l', r')}$$Your task is to help Xiao S answer these queries.
Input Format
The first line contains a positive integer , the number of nodes in the tree.
The next lines each contain two positive integers and , representing an edge between nodes and .
The -th line contains a positive integer , the number of queries.
The next lines each contain three positive integers , describing a query.
Output Format
For each query, output one line containing the answer.
6
5 6
6 1
6 2
2 3
2 4
3
2 5 2
1 4 1
1 6 3
3
4
3
Hint
Explanation
【Sample 1 Explanation】

-
For the first query:
- , , .
- The depth of node is , and the depth of node is .
- Thus, the answer is .
-
For the second query:
- The answer is the maximum depth among nodes , which is .
-
For the third query:
- , , , .
- The depth of node is , which remains the maximum, so the answer is .
【Sample 2】
See the attached files query/query2.in and query/query2.ans.
This sample satisfies .
【Sample 3】
See the attached files query/query3.in and query/query3.ans.
This sample satisfies and the tree forms a chain.
【Sample 4】
See the attached files query/query4.in and query/query4.ans.
This sample satisfies .
【Data Range】
For all test data:
| Test Case Range | Special Constraints | |
|---|---|---|
| None | ||
| Property A | ||
| Property B | ||
| None | ||
- Property A: The tree is a chain (root has degree ).
- Property B: For every query, .
Translated by DeepSeek R1
京公网安备 11011102002149号