CF1361F.Johnny and New Toy

普及/提高-

通过率:0%

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 n1n - 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 n1n - 1 . There are also extra weights W0=Wn=0W_0 = W_n = 0 .

The instruction defines subsegment [L,R][L, R] as good if WL1<WiW_{L - 1} < W_i and WR<WiW_R < W_i for any ii in {L,L+1,,R1}\{L, L + 1, \ldots, R - 1\} . For such subsegment it also defines WMW_M as minimum of set {WL,WL+1,,WR1}\{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 P_XP\_X and P_YP\_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.

输入格式

The first line contains single integer nn (2n2105)(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 (1Pin)(1 \leq P_i \leq n) denoting the initial permutation PP . The third line contains n1n - 1 distinct integers W1,W2,,Wn1W_1, W_2, \ldots, W_{n - 1} (1Win1)(1 \leq W_i \leq n - 1) denoting the weights.

The fourth line contains single integer qq (1q5104)(1 \leq q \leq 5 \cdot 10^4) — the number of Megan's swaps. The following qq lines contain two integers XX and YY (1X,Yn)(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.

输出格式

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.

输入输出样例

  • 输入#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.

首页