CF1830E.Bully Sort
NOI/NOI+/CTSC
通过率:0%
时间限制:10.00s
内存限制:1024MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
On a permutation p of length n, we define a bully swap as follows:
- Let i be the index of the largest element pi such that pi=i.
- Let j be the index of the smallest element pj such that i<j.
- Swap pi and pj.
We define f(p) as the number of bully swaps we need to perform until p becomes sorted. Note that if p is the identity permutation, f(p)=0.
You are given n and a permutation p of length n. You need to process the following q updates.
In each update, you are given two integers x and y. You will swap px and py and then find the value of f(p).
Note that the updates are persistent. Changes made to the permutation p will apply when processing future updates.
对于一个长度为 n 的排列 p,我们定义一次“霸凌交换”(bully swap)如下:
- 设 i 是满足 pi=i 的最大元素 pi 的下标;
- 设 j 是满足 i<j 的最小元素 pj 的下标;
- 交换 pi 和 pj。
我们定义 f(p) 为将 p 变为升序排列(即恒等排列)所需执行的霸凌交换次数。注意:若 p 本身就是恒等排列,则 f(p)=0。
给定 n 和一个长度为 n 的排列 p,你需要处理接下来的 q 次更新。
每次更新中,你将收到两个整数 x 和 y;你需要先交换 px 和 py,然后计算当前 p 对应的 f(p) 的值。
注意:这些更新是持久化的,即对排列 p 所做的修改将在后续更新中持续生效。
输入格式
The first line of the input contains two integers n and q (2≤n≤5⋅105, 1≤q≤5⋅104) — the length of the permutation and the number of updates.
The second line of input contains n integer p1,p2,…,pn (1≤pi≤n) — the permutation p. All elements of p are distinct.
The i-th of the next q lines of input contains two integers xi and yi (1≤xi<yi≤n) — describing the i-th update.
输入的第一行包含两个整数 n 和 q(2≤n≤5⋅105,1≤q≤5⋅104)—— 分别表示排列的长度和更新操作的次数。
输入的第二行包含 n 个整数 p1,p2,…,pn(1≤pi≤n)—— 表示排列 p。p 中所有元素互不相同。
接下来 q 行中的第 i 行包含两个整数 xi 和 yi(1≤xi<yi≤n)—— 描述第 i 次更新操作。
输出格式
After each update, output f(p).
每次更新后,输出 f(p)。
输入输出样例
输入#1
8 5 6 2 1 5 3 4 7 8 1 8 2 3 4 7 7 8 3 6
输出#1
5 6 9 8 7
说明/提示
After the first update, we have f(p)=5. The 5 bully swaps are illustrated below.
- [1,2,8,5,3,4,7,6],
- [1,2,3,5,8,4,7,6],
- [1,2,3,5,4,8,7,6],
- [1,2,3,5,4,6,7,8],
- [1,2,3,4,5,6,7,8].
第一次更新后,我们有 f(p)=5。这 5 次“霸凌交换”如下所示:
- [1,2,8,5,3,4,7,6],
- [1,2,3,5,8,4,7,6],
- [1,2,3,5,4,8,7,6],
- [1,2,3,5,4,6,7,8],
- [1,2,3,4,5,6,7,8]。
输入解题思路,AI测评打分。不知道怎么写?