QOJ.ac

QOJ

Límite de tiempo: 8 s Límite de memoria: 1024 MB Puntuación total: 100 Dificultad: [mostrar]

#9419. 普通朋友

Estadísticas

线段树是 Little Q 非常喜欢的一种数据结构。它们结构简单、时间复杂度优异且功能强大,因此 Little Q 曾花很长时间研究线段树的一些性质。

最近,Little Q 又开始研究线段树了,但这次有所不同:她专注于更广义的线段树。在普通线段树中,对于区间 $[l, r]$,我们会取 $\text{mid} = \lfloor \frac{l+r}{2} \rfloor$,然后将该区间拆分为 $[l, \text{mid}]$ 和 $[\text{mid} + 1, r]$。在广义线段树中,$\text{mid}$ 不要求必须是区间的中点,但 $\text{mid}$ 仍需满足 $l \le \text{mid} < r$。不难发现,在广义线段树中,树的深度可以达到 $O(n)$ 层。

Little Q 不知道如何实现平衡二叉搜索树(BST),所以她想利用线段树来实现平衡 BST 的所有操作。线段树无法实现的最著名操作之一就是区间翻转,但 Little Q 不以为然,并希望用线段树来实现它。

具体来说,当 Little Q 翻转一个区间时,她会首先将该区间划分为线段树节点所代表的若干个极大区间;假设从左到右它们在线段树中对应的 $k$ 个节点分别为 $a_1, a_2, \dots, a_k$。然后,Little Q 会拆下这 $k$ 棵子树,并以相反的顺序将它们重新挂载到线段树上,即:原先位于 $a_1$ 位置的子树被 $a_k$ 的子树替换,原先位于 $a_2$ 位置的子树被 $a_{k-1}$ 的子树替换,以此类推;原先位于 $a_k$ 位置的子树被 $a_1$ 的子树替换。最后,Little Q 会交换这 $k$ 棵子树中所有节点的左右孩子。容易发现,经过这样的操作后,这棵树仍然是一棵广义线段树,且元素从左到右的顺序正好是翻转区间后的结果。

给定这样一棵广义线段树,Little Q 需要执行 $m$ 次操作;每次给定一个前缀 $[1, x]$,她对其进行翻转。同时,Little Q 想知道 $k$ 的值,即在翻转操作过程中,当前广义线段树中被划分出的区间数量。

当然,Little Q 轻松解决了这个问题,这使她更加确信不需要学习如何编写平衡 BST。那么,你能解决它吗?

输入格式

第一行包含两个正整数 $n$ 和 $m$ ($2 \le n, m \le 3 \times 10^5$),分别表示树的大小和操作次数。

接下来一行包含 $n - 1$ 个正整数,按深度优先搜索(DFS)顺序给出每个区间的拆分点 $\text{mid}$。

接下来 $m$ 行,每行包含一个正整数 $x$,表示一次翻转前缀 $[1, x]$ 的操作。

输出格式

输出 $m$ 行,每行包含一个正整数,表示每次操作中 $k$ 的值。

样例

输入 1

3 3
2 1
2
3
2

输出 1

1
1
2

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.