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

    ID: 13473 远端评测题 6000ms 1024MiB 尝试: 0 已通过: 0 显示难度提高+/省选− 上传者: 标签>点分治2023蓝桥杯国赛

[蓝桥杯 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 L∼RL \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 n−1n - 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, n≤2000n \le 2000.
For all testdata, 1≤L≤R≤n≤1061 \le L \le R \le n \le 10^6, 1≤Fi≤i1 \le F_i \le i.

Translated by ChatGPT 5