2021 年东华大学金马程序设计联赛

E. A Similar Game
PDF 题面可用
你可以在这里下载。

单点时限: 2.0 sec

内存限制: 512 MB

This question is similar to Move The Numbers.

A permutation of size 2n is an array of size 2n such that each integer from 1 to 2n occurs exactly once in this array.

An inversion in a permutation p is a pair of indices (i,j) such that i>j and aiaj. For example, a permutation [4,1,3,2] contains 4 inversions: (2,1),(3,1),(4,1),(4,3).

You are given a permutation a of size 2n and m queries to it. Each query is represented by one number t(0tn) denoting that you have to reverse the segment [1,2t],[2t+1,2×2t],[2×2t+1,3×2t],,[2n2t+1,2n] of the permutation.

For example, if a=[1,2,3,4,5,6,7,8] and a query t=1 is applied, then the resulting permutation is [2,1,4,3,6,5,8,7].

After each query you have to answer the number of inversions.

输入格式

There are multiple test cases in this problem. (T12)

In each test case,The first line contains two integers n and m.(2n17,3m10000).

The second line contains 2n integers a1,a2,,a2n(1ai2n) — the elements of the permutation. These integers are pairwise distinct.

The third line contains m integers t1,t2,,tm(0tin).

输出格式

For each testcase print m lines denoting the answer.

样例

Input
2 3
4 3 2 1
1 1 0
2 3
1 4 2 3
0 1 2
Output
4
6
6
2
4
2