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 PP of numbers from 11 to nn, written in one row next to each other.

For each ii from 11 to n−1n - 1 between PiP_i and Pi+1P_{i + 1} there is a weight WiW_i written, and those weights form a permutation of numbers from 11 to n−1n - 1. There are also extra weights W0=Wn=0W_0 = W_n = 0.

The instruction defines subsegment [L,R][L, R] as good if WL−1<WiW_{L - 1} \lt W_i and WR<WiW_R \lt W_i for any ii in L,L+1,…,R−1{L, L + 1, \ldots, R - 1}. For such subsegment it also defines WMW_M as minimum of set WL,WL+1,…,WR−1{W_L, W_{L + 1}, \ldots, W_{R - 1}}.

Now the fun begins. In one move, the player can choose one of the good subsegments, cut it into [L,M][L, M] and [M+1,R][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 PP.

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 XX and YY, and swapping PXP_X and PYP_Y (XX might be equal YY). After each sister's swap, Johnny wonders, what is the minimal number of inversions that he can achieve, starting with current PP and making legal moves?

You can assume that the input is generated randomly. PP and WW were chosen independently and equiprobably over all permutations; also, Megan's requests were chosen independently and equiprobably over all pairs of indices.

约翰尼有一个新玩具。如你所料,这个玩具有点特别。它是一个由 11 到 nn 的数字构成的排列 PP,这些数字并排写成一行。

对每个从 11 到 n−1n - 1 的 ii,在 PiP_i 和 Pi+1P_{i + 1} 之间写有一个权重 WiW_i;这些权重构成 11 到 n−1n - 1 的一个排列。此外还有额外的权重 W0=Wn=0W_0 = W_n = 0。

说明书将子段 [L,R][L, R] 定义为好子段,当且仅当对任意 i∈{L,L+1,…,R−1}i \in \{L, L + 1, \ldots, R - 1\},均有 WL−1<WiW_{L - 1} \lt W_i 且 WR<WiW_R \lt W_i。对于这样的子段,还定义 WMW_M 为集合 {WL,WL+1,…,WR−1}\{W_L, W_{L + 1}, \ldots, W_{R - 1}\} 中的最小值。

现在有趣的部分开始了。在一次操作中,玩家可以选择一个好子段,并将其沿位置 MM 切分为 [L,M][L, M] 和 [M+1,R][M + 1, R],然后交换这两部分。更准确地说,一次操作前,所选子段的玩具外观为:

WL−1,PL,WL,…,WM−1,PM,WM,PM+1,WM+1,…,WR−1,PR,WRW_{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

而操作后变为:

WL−1,PM+1,WM+1,…,WR−1,PR,WM,PL,WL,…,WM−1,PM,WRW_{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

此类操作可执行多次(可能为零次),目标是使排列 PP 中的逆序对数量最少。

约翰尼的妹妹梅根认为规则过于复杂,于是她想通过选择某一对下标 XX 和 YY 并交换 PXP_X 与 PYP_Y(允许 X=YX = Y)来测试哥哥。每次妹妹完成一次交换后,约翰尼都会思考:以当前的 PP 为起点,通过合法操作所能达到的最小逆序对数量是多少?

你可以假设输入是随机生成的:PP 和 WW 独立地、等概率地从所有排列中选取;同时,梅根的每次查询也独立地、等概率地从所有下标对中选取。

输入格式

The first line contains single integer nn (2≤n≤2⋅105)(2 \leq n \leq 2 \cdot 10^5) denoting the length of the toy.

The second line contains nn distinct integers P1,P2,…,PnP_1, P_2, \ldots, P_n (1≤Pi≤n)(1 \leq P_i \leq n) denoting the initial permutation PP. The third line contains n−1n - 1 distinct integers W1,W2,…,Wn−1W_1, W_2, \ldots, W_{n - 1} (1≤Wi≤n−1)(1 \leq W_i \leq n - 1) denoting the weights.

The fourth line contains single integer qq (1≤q≤5⋅104)(1 \leq q \leq 5 \cdot 10^4) — the number of Megan's swaps. The following qq lines contain two integers XX and YY (1≤X,Y≤n)(1 \leq X, Y \leq n) — the indices of elements of PP to swap. The queries aren't independent; after each of them, the permutation is changed.

第一行包含一个整数 nn (2≤n≤2⋅105)(2 \leq n \leq 2 \cdot 10^5),表示玩具的长度。

第二行包含 nn 个互不相同的整数 P1,P2,…,PnP_1, P_2, \ldots, P_n (1≤Pi≤n)(1 \leq P_i \leq n),表示初始排列 PP。第三行包含 n−1n - 1 个互不相同的整数 W1,W2,…,Wn−1W_1, W_2, \ldots, W_{n - 1} (1≤Wi≤n−1)(1 \leq W_i \leq n - 1),表示权重。

第四行包含一个整数 qq (1≤q≤5⋅104)(1 \leq q \leq 5 \cdot 10^4) —— Megan 执行的交换操作次数。接下来的 qq 行每行包含两个整数 XX 和 YY (1≤X,Y≤n)(1 \leq X, Y \leq n) —— 表示要交换的排列 PP 中元素的下标。这些查询不是相互独立的;每次交换后,排列都会被更新。

输出格式

Output qq lines. The ii-th line should contain exactly one integer — the minimum number of inversions in permutation, which can be obtained by starting with the PP after first ii queries and making moves described in the game's instruction.

输出 qq 行。第 ii 行应恰好包含一个整数——即从执行完前 ii 个查询后的排列 PP 出发,按照游戏规则进行操作后所能得到的排列的最小逆序对数量。

输入输出样例

  • 输入#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, PP is sorted, so we already achieved a permutation with no inversions.

After the second query, PP is equal to [11, 33, 22], it has one inversion, it can be proven that it is impossible to achieve 00 inversions.

In the end, PP is equal to [22, 33, 11]; we can make a move on the whole permutation, as it is a good subsegment itself, which results in PP equal to [11, 22, 33], which has 00 inversions.

考虑第一个样例。第一次查询后,PP 已经被排序,因此我们已经得到了一个没有逆序对的排列。

第二次查询后,PP 变为 [11, 33, 22],它含有一个逆序对;可以证明此时无法达到 00 个逆序对。

最终,PP 变为 [22, 33, 11];我们可以对整个排列执行一次操作,因为该排列本身就是一个“好”的子段,操作后 PP 变为 [11, 22, 33],其逆序对数量为 00。

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

首页