CF1740H.MEX Tree Manipulation

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

Given a rooted tree, define the value of vertex uu in the tree recursively as the MEX†^\dagger of the values of its children. Note that it is only the children, not all of its descendants. In particular, the value of a leaf is 00.

Pak Chanek has a rooted tree that initially only contains a single vertex with index 11, which is the root. Pak Chanek is going to do qq queries. In the ii-th query, Pak Chanek is given an integer xix_i. Pak Chanek needs to add a new vertex with index i+1i+1 as the child of vertex xix_i. After adding the new vertex, Pak Chanek needs to recalculate the values of all vertices and report the sum of the values of all vertices in the current tree.

†^\dagger The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For example, the MEX of [0,1,1,2,6,7][0,1,1,2,6,7] is 33 and the MEX of [6,9][6,9] is 00.

给定一棵有根树,定义树中顶点 uu 的值为:其所有子节点(注意:仅指直接子节点,而非所有后代节点)的值所构成数组的 MEX†^\dagger。特别地,叶子节点的值为 00。

Pak Chanek 拥有一棵初始仅含一个顶点(编号为 11)的有根树,该顶点即为根节点。Pak Chanek 将执行 qq 个查询。在第 ii 个查询中,Pak Chanek 会得到一个整数 xix_i,他需要将一个新顶点(编号为 i+1i+1)作为顶点 xix_i 的子节点加入树中。添加新顶点后,Pak Chanek 需要重新计算树中所有顶点的值,并报告当前树中所有顶点的值之和。

†^\dagger 数组的 MEX(最小未出现值)是指不属于该数组的最小非负整数。例如,[0,1,1,2,6,7][0,1,1,2,6,7] 的 MEX 是 33,而 [6,9][6,9] 的 MEX 是 00。

输入格式

The first line contains a single integer qq (1≤q≤3⋅1051 \le q \le 3 \cdot 10^5) — the number of operations.

Each of the next qq lines contains a single integer xix_i (1≤xi≤i1 \leq x_i \leq i) — the description of the ii-th query.

第一行包含一个整数 qq(1≤q≤3⋅1051 \le q \le 3 \cdot 10^5)—— 操作的次数。

接下来的 qq 行中,每行包含一个整数 xix_i(1≤xi≤i1 \leq x_i \leq i)—— 第 ii 个查询的描述。

输出格式

For each query, print a line containing an integer representing the sum of the new values of all vertices in the tree after adding the vertex.

对于每个查询,输出一行,包含一个整数,表示在添加该顶点后树中所有顶点的新值之和。

输入输出样例

  • 输入#1

    7
    1
    1
    3
    2
    5
    2
    1

    输出#1

    1
    1
    3
    2
    4
    4
    7
  • 输入#2

    8
    1
    1
    1
    1
    5
    6
    7
    8

    输出#2

    1
    1
    1
    1
    3
    2
    4
    3

说明/提示

In the first example, the tree after the 66-th query will look like this.

  • Vertex 77 is a leaf, so its value is 00.
  • Vertex 66 is a leaf, so its value is 00.
  • Vertex 55 only has a child with value 00, so its value is 11.
  • Vertex 44 is a leaf, so its value is 00.
  • Vertex 33 only has a child with value 00, so its value is 11.
  • Vertex 22 has children with values 00 and 11, so its value is 22.
  • Vertex 11 has children with values 11 and 22, so its value is 00.

The sum of the values of all vertices is 0+0+1+0+1+2+0=40+0+1+0+1+2+0=4.

在第一个例子中,第 66 次查询后的树结构如下所示。

  • 顶点 77 是叶子节点,因此其值为 00。
  • 顶点 66 是叶子节点,因此其值为 00。
  • 顶点 55 仅有一个子节点,其值为 00,因此其值为 11。
  • 顶点 44 是叶子节点,因此其值为 00。
  • 顶点 33 仅有一个子节点,其值为 00,因此其值为 11。
  • 顶点 22 有两个子节点,其值分别为 00 和 11,因此其值为 22。
  • 顶点 11 有两个子节点,其值分别为 11 和 22,因此其值为 00。

所有顶点的值之和为 0+0+1+0+1+2+0=40+0+1+0+1+2+0=4。

输入解题思路,AI测评打分。不知道怎么写?

首页