CF1361F.Johnny and New Toy
NOI/NOI+/CTSC
通过率:0%
时间限制:15.00s
内存限制:1024MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Johnny has a new toy. As you may guess, it is a little bit extraordinary. The toy is a permutation P of numbers from 1 to n, written in one row next to each other.
For each i from 1 to n−1 between Pi and Pi+1 there is a weight Wi written, and those weights form a permutation of numbers from 1 to n−1. There are also extra weights W0=Wn=0.
The instruction defines subsegment [L,R] as good if WL−1<Wi and WR<Wi for any i in L,L+1,…,R−1. For such subsegment it also defines WM as minimum of set WL,WL+1,…,WR−1.
Now the fun begins. In one move, the player can choose one of the good subsegments, cut it into [L,M] and [M+1,R] and swap the two parts. More precisely, before one move the chosen subsegment of our toy looks like: $$W_{L - 1}, P_L, W_L, \ldots, W_{M - 1}, P_M, W_M, P_{M + 1}, W_{M + 1}, \ldots, W_{R - 1}, P_R, W_R$$ and afterwards it looks like this: $$W_{L - 1}, P_{M + 1}, W_{M + 1}, \ldots, W_{R - 1}, P_R, W_M, P_L, W_L, \ldots, W_{M - 1}, P_M, W_R$$ Such a move can be performed multiple times (possibly zero), and the goal is to achieve the minimum number of inversions in P.
Johnny's younger sister Megan thinks that the rules are too complicated, so she wants to test her brother by choosing some pair of indices X and Y, and swapping PX and PY (X might be equal Y). After each sister's swap, Johnny wonders, what is the minimal number of inversions that he can achieve, starting with current P and making legal moves?
You can assume that the input is generated randomly. P and W were chosen independently and equiprobably over all permutations; also, Megan's requests were chosen independently and equiprobably over all pairs of indices.
约翰尼有一个新玩具。如你所料,这个玩具有点特别。它是一个由 1 到 n 的数字构成的排列 P,这些数字并排写成一行。
对每个从 1 到 n−1 的 i,在 Pi 和 Pi+1 之间写有一个权重 Wi;这些权重构成 1 到 n−1 的一个排列。此外还有额外的权重 W0=Wn=0。
说明书将子段 [L,R] 定义为好子段,当且仅当对任意 i∈{L,L+1,…,R−1},均有 WL−1<Wi 且 WR<Wi。对于这样的子段,还定义 WM 为集合 {WL,WL+1,…,WR−1} 中的最小值。
现在有趣的部分开始了。在一次操作中,玩家可以选择一个好子段,并将其沿位置 M 切分为 [L,M] 和 [M+1,R],然后交换这两部分。更准确地说,一次操作前,所选子段的玩具外观为:
WL−1,PL,WL,…,WM−1,PM,WM,PM+1,WM+1,…,WR−1,PR,WR
而操作后变为:
WL−1,PM+1,WM+1,…,WR−1,PR,WM,PL,WL,…,WM−1,PM,WR
此类操作可执行多次(可能为零次),目标是使排列 P 中的逆序对数量最少。
约翰尼的妹妹梅根认为规则过于复杂,于是她想通过选择某一对下标 X 和 Y 并交换 PX 与 PY(允许 X=Y)来测试哥哥。每次妹妹完成一次交换后,约翰尼都会思考:以当前的 P 为起点,通过合法操作所能达到的最小逆序对数量是多少?
你可以假设输入是随机生成的:P 和 W 独立地、等概率地从所有排列中选取;同时,梅根的每次查询也独立地、等概率地从所有下标对中选取。
输入格式
The first line contains single integer n (2≤n≤2⋅105) denoting the length of the toy.
The second line contains n distinct integers P1,P2,…,Pn (1≤Pi≤n) denoting the initial permutation P. The third line contains n−1 distinct integers W1,W2,…,Wn−1 (1≤Wi≤n−1) denoting the weights.
The fourth line contains single integer q (1≤q≤5⋅104) — the number of Megan's swaps. The following q lines contain two integers X and Y (1≤X,Y≤n) — the indices of elements of P to swap. The queries aren't independent; after each of them, the permutation is changed.
第一行包含一个整数 n (2≤n≤2⋅105),表示玩具的长度。
第二行包含 n 个互不相同的整数 P1,P2,…,Pn (1≤Pi≤n),表示初始排列 P。第三行包含 n−1 个互不相同的整数 W1,W2,…,Wn−1 (1≤Wi≤n−1),表示权重。
第四行包含一个整数 q (1≤q≤5⋅104) —— Megan 执行的交换操作次数。接下来的 q 行每行包含两个整数 X 和 Y (1≤X,Y≤n) —— 表示要交换的排列 P 中元素的下标。这些查询不是相互独立的;每次交换后,排列都会被更新。
输出格式
Output q lines. The i-th line should contain exactly one integer — the minimum number of inversions in permutation, which can be obtained by starting with the P after first i queries and making moves described in the game's instruction.
输出 q 行。第 i 行应恰好包含一个整数——即从执行完前 i 个查询后的排列 P 出发,按照游戏规则进行操作后所能得到的排列的最小逆序对数量。
输入输出样例
输入#1
3 3 2 1 2 1 3 1 3 3 2 3 1
输出#1
0 1 0
输入#2
5 4 3 2 5 1 3 2 1 4 7 5 4 5 2 1 5 2 4 2 4 4 3 3 3
输出#2
3 1 2 1 2 3 3
说明/提示
Consider the first sample. After the first query, P is sorted, so we already achieved a permutation with no inversions.
After the second query, P is equal to [1, 3, 2], it has one inversion, it can be proven that it is impossible to achieve 0 inversions.
In the end, P is equal to [2, 3, 1]; we can make a move on the whole permutation, as it is a good subsegment itself, which results in P equal to [1, 2, 3], which has 0 inversions.
考虑第一个样例。第一次查询后,P 已经被排序,因此我们已经得到了一个没有逆序对的排列。
第二次查询后,P 变为 [1, 3, 2],它含有一个逆序对;可以证明此时无法达到 0 个逆序对。
最终,P 变为 [2, 3, 1];我们可以对整个排列执行一次操作,因为该排列本身就是一个“好”的子段,操作后 P 变为 [1, 2, 3],其逆序对数量为 0。
输入解题思路,AI测评打分。不知道怎么写?