#abc309e. E - Family and Insurance

E - Family and Insurance

Score : 425425 points

问题描述

存在一个家庭,成员包括第 11 人、第 22 人,……,直至第 NN 人。对于任意 i2i\geq 2,第 ii 人的父母是第 pip_i 人。

他们购买了保险共 MM 次。对于 i=1,2,,Mi=1,2,\ldots,M,第 xix_i 人购买了第 ii 次保险,该保险覆盖其本人及其下 yiy_i 代的后代。

请问至少被一份保险覆盖的人数有多少?

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

There is a family consisting of person 11, person 22, \ldots, and person NN. For i2i\geq 2, person ii's parent is person pip_i.

They bought insurance MM times. For i=1,2,,Mi=1,2,\ldots,M, person xix_i bought the ii-th insurance, which covers that person and their descendants in the next yiy_i generations.

How many people are covered by at least one insurance?

Constraints

  • 2N3×1052 \leq N \leq 3 \times 10^5
  • 1M3×1051 \leq M \leq 3 \times 10^5
  • 1pii11 \leq p_i \leq i-1
  • 1xiN1 \leq x_i \leq N
  • 1yi3×1051 \leq y_i \leq 3 \times 10^5
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN MM

p2p_2 \ldots pNp_N

x1x_1 y1y_1

\vdots

xMx_M yMy_M

Output

Print the answer.

Sample Input 1

7 3
1 2 1 3 3 3
1 1
1 2
4 3

Sample Output 1

4

The 11-st insurance covers people 11, 22, and 44, because person 11's 11-st generation descendants are people 22 and 44.
The 22-nd insurance covers people 11, 22, 33, and 44, because person 11's 11-st generation descendants are people 22 and 44, and person 11's 22-nd generation descendant is person 33.
The 33-rd insurance covers person 44, because person 44 has no 11-st, 22-nd, or 33-rd descendants.

Therefore, four people, people 11, 22, 33, and 44, are covered by at least one insurance.

Sample Input 2

10 10
1 1 3 1 2 3 3 5 7
2 1
5 1
4 3
6 3
2 1
7 3
9 2
1 2
6 2
8 1

Sample Output 2

10

update @ 2024/3/10 08:46:42