QOJ.ac

QOJ

実行時間制限: 5.0 s メモリ制限: 256 MB 満点: 100 ハック可能 ✓

#9076. 笛卡尔树

統計

笛卡尔树(Cartesian tree)对于一个由不同数字组成的序列,可以通过以下性质唯一确定:

笛卡尔树中的每个节点对应序列中的一个数字。每个节点关联一个序列值。

树的中序遍历结果即为原序列。也就是说,左子树由序列中在根节点之前的数值组成,右子树由序列中在根节点之后的数值组成,且树的每个下层节点也满足类似的顺序约束。

该树具有堆性质:任何非根节点的父节点的值都比该节点本身的值大。 — 维基百科

用一个包含 $N$ 个不同数字的数组构建笛卡尔树很容易,对吧?

如果通过交换位置 $i$ 和 $i+1$ 处的数值来改变数组,会发生什么呢?

现在你需要回答在 $M$ 次连续修改中,每次修改后树中所有节点层数(高度)变化绝对值之和。

输入格式

第一行包含一个整数 $T$ ($1 \le T \le 20$),表示测试用例的数量。

对于每个测试用例,第一行包含两个整数 $N$ ($2 \le N \le 10^5$) 和 $M$ ($1 \le M \le 10^5$),分别表示数组中元素的数量和查询的数量。

第二行包含 $N$ 个元素,表示数组中的元素。

接下来的 $M$ 行描述了上述 $M$ 次修改。每行包含一个从 $0$ 开始的索引 $i$ ($0 \le i < N-1$),表示我们要交换位于 $i$ 和 $i+1$ 的元素。

输出格式

对于每个测试用例,首先输出 Case T:,其中 $T$ 是测试用例编号。对于每次修改,在单独的一行中输出树节点层数变化绝对值之和。

样例

输入 1

3
4 2
1 2 3 4
2
1
3 6
1 2 3
0
1
1
0
0
0
5 10
2 3 5 4 1
0
1
2
3
2
3
1
2
3
2

输出 1

Case 1:
2
2
Case 2:
0
1
1
0
0
0
Case 3:
0
0
1
0
1
1
1
1
3
2

说明

样例 1 的第一个测试用例:

Original: Node 1: 4, Node 2: 3, Node 3: 2, Node 4: 1, ans: - After First Change: Node 1: 3, Node 2: 2, Node 3: 2, Node 4: 1, ans: $|3-4| + |2-3| + |2-2| + |1-1| = 2$ After Second Change: Node 1: 2, Node 2: 3, Node 3: 2, Node 4: 1, ans: $|2-3| + |3-2| + |2-2| + |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.