#P11364. [NOIP2024] 树上查询

[NOIP2024] 树上查询

Description

One day, Xiao S and her friend Xiao N studied a tree with nn nodes.

This is a rooted tree with root node numbered 11. The depth depu\text{dep}_u of a node uu is defined as the number of nodes on the simple path from uu to 11.

Additionally, LCA*(l,r)\text{LCA*}(l, r) is defined as the lowest common ancestor (LCA) of all nodes with numbers in [l,r][l, r], i.e., the deepest node that is an ancestor of all nodes l,l+1,,rl, l + 1, \dots, r.

Xiao N posed qq queries about this tree. For each query, he provides three parameters l,r,kl, r, k, asking for the maximum depth among the LCAs of all continuous subintervals of [l,r][l, r] with length at least kk. 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 nn, the number of nodes in the tree.

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

The (n+1)(n + 1)-th line contains a positive integer qq, the number of queries.

The next qq lines each contain three positive integers l,r,kl, r, k, 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】

Figure 3

Pic 3: The tree in sample 1\text{Pic 3: The tree in sample 1}

  • For the first query:

    • LCA*(2,3)=2\text{LCA*}(2, 3) = 2, LCA*(3,4)=2\text{LCA*}(3, 4) = 2, LCA*(4,5)=6\text{LCA*}(4, 5) = 6.
    • The depth of node 22 is 33, and the depth of node 66 is 22.
    • Thus, the answer is max{3,3,2}=3\max\{3, 3, 2\} = 3.
  • For the second query:

    • The answer is the maximum depth among nodes 1,2,3,41, 2, 3, 4, which is 44.
  • For the third query:

    • LCA*(1,3)=1\text{LCA*}(1, 3) = 1, LCA*(2,4)=2\text{LCA*}(2, 4) = 2, LCA*(3,5)=6\text{LCA*}(3, 5) = 6, LCA*(4,6)=6\text{LCA*}(4, 6) = 6.
    • The depth of node 22 is 33, which remains the maximum, so the answer is 33.

【Sample 2】

See the attached files query/query2.in and query/query2.ans.

This sample satisfies n,q500n, q \leq 500.

【Sample 3】

See the attached files query/query3.in and query/query3.ans.

This sample satisfies n,q105n, q \leq 10^5 and the tree forms a chain.

【Sample 4】

See the attached files query/query4.in and query/query4.ans.

This sample satisfies n,q5×105n, q \leq 5 \times 10^5.

【Data Range】

For all test data:

  • 1n,q5×1051 \leq n, q \leq 5 \times 10^5
  • 1lrn1 \leq l \leq r \leq n
  • 1krl+11 \leq k \leq r - l + 1
Test Case Range n,qn, q \leq Special Constraints
121\sim2 500500 None
353\sim5 50005000
696\sim9 10510^5 Property A
101310\sim13 5×1055 \times 10^5
141614\sim16 Property B
172017\sim20 10510^5 None
212521\sim25 5×1055 \times 10^5
  • Property A: The tree is a chain (root has degree 11).
  • Property B: For every query, k=rl+1k = r - l + 1.

Translated by DeepSeek R1