http://codeforces.com/contest/414/problem/C

Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.

You have an array a of length 2n and m queries on it. The i-th query is described by an integer qi. In order to perform the i-th query you must:

  • split the array into 2n - qi parts, where each part is a subarray consisting of 2qi numbers; the j-th subarray (1 ≤ j ≤ 2n - qi) should contain the elements a[(j - 1)·2qi + 1], a[(j - 1)·2qi + 2], ..., a[(j - 1)·2qi + 2qi];
  • reverse each of the subarrays;
  • join them into a single array in the same order (this array becomes new array a);
  • output the number of inversions in the new a.

Given initial array a and all the queries. Answer all the queries. Please, note that the changes from some query is saved for further queries.

Input

The first line of input contains a single integer n (0 ≤ n ≤ 20).

The second line of input contains 2n space-separated integers a[1], a[2], ..., a[2n] (1 ≤ a[i] ≤ 109), the initial array.

The third line of input contains a single integer m (1 ≤ m ≤ 106).

The fourth line of input contains m space-separated integers q1, q2, ..., qm (0 ≤ qi ≤ n), the queries.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output

Output m lines. In the i-th line print the answer (the number of inversions) for the i-th query.

题意:

给定长度2^n的序列,q个询问,对于每个询问:

1、先把 序列分段分成: [0, 2^u-1], [2^u, 2^(u+1)-1],······

2、再把每段都翻转一下。

3、输出此时序列的逆序数。

思路:

分而治之:[l,r]的逆序数=[l,mid]内部的逆序数+[mid,r]内部的逆序数+[l,mid]与[mid,r]之间的逆序数,层层划分下去。把2^n的逆序数划分为n层。

用num[len][0/1]存长为2^len的区间 每个的左半与右半 之间的正/逆序数 之和。

如下图,num[2][]=[1,2]与[3,4]之间的逆序数+[5,6]与[7,8]之间的逆序数。

对于每一个查询x,显然num[len]以及len以上不受影响,num[0~len]的正序数与逆序数反转。

这篇要详细一点,不过还是看代码最有效。https://blog.csdn.net/littlewhite520/article/details/68951341

注意既要求正序数有要求逆序数,紫书的那个是只求逆序数的,貌似不大好改,还是sort一下比较稳妥。

时间复杂度O(nlog^2(n)+mn)

#include<bits/stdc++.h>
using namespace std;
#define ll long long

int n,m,q,a[1<<21];
ll num[21][2];

void merge_sort(int l,int r,int len)
{
	if(r-l==1)return;
	int m=(l+r)/2;
	merge_sort(l,m,len-1);
	merge_sort(m,r,len-1);
	for(int i=l;i<m;i++)
	{
		num[len][0]+=a+r-upper_bound(a+m,a+r,a[i]);
		num[len][1]+=lower_bound(a+m,a+r,a[i])-(a+m);
	}
	sort(a+l,a+r); 
}

int main()
{
	//freopen("input.in","r",stdin);
	cin>>n;
	for(int i=0;i<(1<<n);i++)scanf("%d",&a[i]);
	cin>>m;
	merge_sort(0,1<<n,n);
	while(m--)
	{
		scanf("%d",&q);
		for(int i=1;i<=q;i++)swap(num[i][0],num[i][1]);
		ll ans=0;
		for(int i=1;i<=n;i++)ans+=num[i][1];
		printf("%I64d\n",ans);
	}	
	return 0;
}