#P6589. 『JROI-1』 关系树

『JROI-1』 关系树

Background

Xiao L has many favorite game characters, and he connects these characters according to certain relationships. These characters and their relationships form a tree, which Xiao L calls the "relationship tree".

Problem Description

A relationship tree is a tree consisting of nn vertices and n1n - 1 undirected edges.

For a given graph GG, define the vertex-induced subgraph of GG on a vertex set EE as the graph consisting of the vertex set EE and all edges in the original graph GG whose both endpoints belong to EE.

A graph is defined to be clean if and only if for any two vertices u,vu, v in the graph, either uu and vv are not connected, or the distance is at most kk.

Xiao L wants to know, for a query pair l,rl, r (lrl \leq r), how many pairs (a,b)(a, b) satisfy labrl \leq a \leq b \leq r, such that the vertex-induced subgraph formed by all vertices with indices from aa to bb (including a,ba, b) is clean. Moreover, he also wants you to output the sum of all interval lengths (i.e., ba+1b - a + 1).

Since Xiao L likes asking questions, you need to answer a total of qq queries.

Input Format

The first line contains three integers nn, qq, kk, with the meanings as described above.

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

The next qq lines each contain two integers ll, rr, representing a query.

Output Format

Output qq lines. Each line contains two integers: the number of valid pairs (a,b)(a, b), and the sum of all interval lengths.

5 3 2
1 2
1 5
4 5
3 5
1 3
2 5
1 5
6 10
10 20
14 30

Hint

Sample 1 Explanation

The relationship tree formed is shown in the figure.

The valid (a,b)(a, b) are $(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(2,5),(3,3),(3,4),(3,5),(4,4),(4,5),(5,5)$.

The answers to the three queries are 6,106,10, 10,2010,20, and 14,3014,30, respectively.


Constraints

This problem uses bundled testdata.

  • Subtask 1 ( 10%10\% ): n2000n \leq 2000.
  • Subtask 2 ( 30%30\% ): n2×104n \leq 2 \times 10^4, and the relationship tree is a chain.
  • Subtask 3 ( 60%60\% ): n2×104n \leq 2 \times 10^4.
  • Subtask 4 ( Enhanced version testdata, time limit 4.5s4.5s ): No special restrictions.

For all test cases (100%100\%), it is guaranteed that 1n8×1041 \leq n \leq 8 \times 10^4, 1q1051 \leq q \leq 10^5, 0k<n0 \leq k < n, 1u,v,l,rn1 \leq u, v, l, r \leq n.

Translated by ChatGPT 5