#P4211. [LNOI2014] LCA

    ID: 3142 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2014线段树各省省选辽宁最近公共祖先,LCA树链剖分,树剖概率论,统计差分

[LNOI2014] LCA

Description

You are given a rooted tree with nn nodes (numbered from 00 to n1n - 1), with root 00.

The depth of a node is defined as its distance to the root +1+1.

Let dep[i]dep[i] denote the depth of node ii, and let LCA(i,j)\operatorname{LCA}(i, j) denote the lowest common ancestor of ii and jj.

There are mm queries. For each query with given l,r,zl, r, z, compute i=lrdep[LCA(i,z)]\sum_{i=l}^{r} dep[\operatorname{LCA}(i, z)].

Input Format

  • The first line contains 22 integers n,mn, m.
  • The next n1n - 1 lines give parent indices: for each ii from 11 to n1n - 1, the ii-th of these lines contains the parent of node ii.
  • The next mm lines each contain 33 integers l,r,zl, r, z.

Output Format

Output mm lines. Each line contains the answer for one query, taken modulo 201314201314.

5 2
0
0
1
1
1 4 3
1 4 2
8
5

Hint

Constraints

  • For 20%20\% of the testdata, n10000n \le 10000, m10000m \le 10000.
  • For 40%40\% of the testdata, n20000n \le 20000, m20000m \le 20000.
  • For 60%60\% of the testdata, n30000n \le 30000, m30000m \le 30000.
  • For 80%80\% of the testdata, n40000n \le 40000, m40000m \le 40000.
  • For 100%100\% of the testdata, 1n500001 \le n \le 50000, 1m500001 \le m \le 50000.

Translated by ChatGPT 5