#P10421. [蓝桥杯 2023 国 A] 树上的路径

[蓝桥杯 2023 国 A] 树上的路径

Problem Description

Given a tree with nn nodes, where the length of every edge is 11, find the sum of lengths of all paths in this tree whose lengths are between LRL \sim R. Two paths are considered the same path if the sets of edges they pass through are exactly the same.

That is, compute $\sum\limits_{i=1}^n{\sum\limits_{j=i+1}^{n}{dis(i,j)\cdot[L \le dis(i,j) \le R]}}$, where dis(i,j)dis(i,j) denotes the distance between node ii and node jj, and [C][C] equals 11 if condition CC holds, otherwise 00.

Input Format

The first line contains three integers n,L,Rn, L, R, separated by one space.

The next n1n - 1 lines each contain one integer. On the ii-th line, the integer FiF_i indicates the parent node of node i+1i + 1 in the tree. Node 11 is the root and has no parent.

Output Format

Output one line containing one integer, which is the answer.

4 2 3
1
1
3

7

Hint

[Test Case Size and Conventions]

For 40%40\% of the testdata, n2000n \le 2000.
For all testdata, 1LRn1061 \le L \le R \le n \le 10^6, 1Fii1 \le F_i \le i.

Translated by ChatGPT 5