/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int logN, N, x[(1<<21)+5], tmp[(1<<21)+5], Q;
LL pwr[(1<<21)+5][2];

void mergesort(int L,int R,int h){
	if(h==0) return;
	int M=(L+R)/2;
	mergesort(L,M,h-1);
	mergesort(M+1,R,h-1);

	FORU(i,L,M){
		pwr[h][0]+=lower_bound(x+M+1,x+R+1,x[i])-(x+M+1);
		pwr[h][1]+=R-M-(upper_bound(x+M+1,x+R+1,x[i])-(x+M+1));
	}

	int l=L, r=M+1, idx=L;
	while(l<=M || r<=R){
		if(r>R || x[l]<x[r]) tmp[idx++]=x[l++];
		else tmp[idx++]=x[r++];
	}

	FORU(i,L,R) x[i]=tmp[i];
}

int main(){

	in(logN);
	N=1<<logN;

	FOR(i,N) in(x[i]);

	mergesort(1,N,logN);

	in(Q);
	FOR(q,Q){
		int D;
		in(D);
		LL ans=0;
		FOR(dep,D) swap(pwr[dep][0],pwr[dep][1]);
		FOR(dep,logN) ans+=pwr[dep][0];
		outl(ans);
	}

	return 0;
}


/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int logN, N, x[(1<<21)+5], tmp[(1<<21)+5], Q;
LL pwr[(1<<21)+5][2];

void mergesort(int L,int R,int h){

	if(L>=R) return;
	int M=(L+R)/2;
	mergesort(L,M,h-1);
	mergesort(M+1,R,h-1);

	FORU(i,L,M){
		pwr[h][0]+=lower_bound(x+M+1,x+R+1,x[i])-(x+M+1);
		pwr[h][1]+=R-M-(upper_bound(x+M+1,x+R+1,x[i])-(x+M+1));
	}

	sort(x+L,x+R+1);
}

int main(){

	in(logN);
	N=1<<logN;
	FOR(i,N) in(x[i]);
	mergesort(1,N,logN);

	in(Q);
	FOR(q,Q){
		int D;
		in(D);
		LL ans=0;
		FOR(dep,D) swap(pwr[dep][0],pwr[dep][1]);
		FOR(dep,logN) ans+=pwr[dep][0];
		outl(ans);
	}

	return 0;
}


/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int N,k,a[(1<<20)+5],Q, logN;
int tmp[(1<<20)+5], c[(1<<20)+5];
LL v[2][(1<<20)+5], ans[(1<<20)+5];

LL dnc(int L, int R, int dep, bool which){
	LL res=0;
	if(dep==logN) return 0;

	int M=(L+R)/2;
	res+=dnc(L,M,dep+1,which);
	res+=dnc(M+1,R,dep+1,which);

	int l=L, r=M+1, idx=1;
	while(l<=M || r<=R){
		if(l>M) tmp[idx++]=c[r++];
		else if(r>R) tmp[idx++]=c[l++], res+=r-M-1;
		else if(c[l] <=	 c[r]) tmp[idx++]=c[l++], res+=r-M-1;
		else tmp[idx++]=c[r++];
	}

	idx=1;
	FORU(i,L,R) c[i]=tmp[idx++];
	v[dep][which]+=res;
	return res;
}

void solve(int dep, int idx, bool which, LL val){
	if(dep>logN){
		ans[idx]=val;
		return;
	}
	solve(dep+1, idx, which, val);
	solve(dep+1, idx+(1<<dep), !which, val+v[dep][!which]-v[dep][which]);
}

int main(){

	in(logN);
	N=1<<logN;

	FOR(i,N) in(a[i]);

	FOR(i,N) c[i]=a[i];
	dnc(1,N,0,0);

	FOR(i,N) c[i]=a[N-i+1];
	dnc(1,N,0,1);

	solve(0,0,0,v[0][0]);

	in(Q);
	int state=0;
	FOR(q,Q){
		int x;
		in(x);
		state^=(1<<(logN-x));
		outl(ans[state]);
	}

	return 0;
}


/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int logN, N, x[(1<<21)+5], Q;
LL f[(1<<21)+5], g[(1<<21)+5];

LL binsearchlow(int lo,int hi,int key){
	LL ret=lo-1;
	while(lo<=hi){
		int mid=(lo+hi)/2;
		if(x[mid]<key){
			ret=mid;
			lo=mid+1;
		}
		else hi=mid-1;
	}
	return ret;
}

LL binsearchup(int lo,int hi,int key){
	LL ret=hi+1;
	while(lo<=hi){
		int mid=(lo+hi)/2;
		if(x[mid]>key){
			ret=mid;
			hi=mid-1;
		}
		else lo=mid+1;
	}
	return ret;
}

void DNC(int L,int R,int h){
	if(L>=R) return;
	int M=(L+R)/2;
	DNC(L,M,h-1);
	DNC(M+1,R,h-1);

	FORU(i,L,M){
		f[h]+=binsearchlow(M+1,R,x[i])-M;
		g[h]+=R-binsearchup(M+1,R,x[i])+1;
	}

	sort(x+L,x+R+1);
}

int main(){

	in(logN);
	N=1<<logN;
	FOR(i,N) in(x[i]);

	DNC(1,N,logN);

	in(Q);
	FOR(q,Q){
		int D;
		in(D);
		LL ans=0;
		FOR(h,D) swap(f[h], g[h]);
		FOR(h,logN) ans+=f[h];
		outl(ans);
	}

	return 0;
}


/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int N,k,a[(1<<20)+5],Q, logN;
int tmp[(1<<20)+5], c[(1<<20)+5];
LL v[(1<<20)+5][2], ans[(1<<20)+5];

LL dnc(int L, int R, int dep, bool which){
	LL res=0;
	if(dep==logN) return 0;

	int M=(L+R)/2;
	res+=dnc(L,M,dep+1,which);
	res+=dnc(M+1,R,dep+1,which);

	int l=L, r=M+1, idx=1;
	while(l<=M || r<=R){
		if((l>M || a[r]<a[l]) && r<=R) tmp[idx++]=c[r++];
		else{
			tmp[idx++]=c[l++];
			res+=r-M-1;
		}
	}

	idx=1;
	FORU(i,L,R) c[i]=tmp[idx++];
	v[dep][which]+=res;
	return res;
}

void solve(int dep, int idx, bool which, LL val){
	if(dep>logN){
		ans[idx]=val;
		return;
	}
	solve(dep+1, idx, which, val);
	solve(dep+1, idx+(1<<dep), !which, val+v[dep][!which]-v[dep][which]);
}

int main(){

	in(logN);
	N=1<<logN;

	FOR(i,N) in(a[i]);

	FOR(i,N) c[i]=a[i];
	dnc(1,N,0,0);

	FOR(i,N) c[i]=a[N-i+1];
	dnc(1,N,0,1);

	solve(0,0,0,v[0][0]);

	in(Q);
	int state=0;
	FOR(q,Q){
		int x;
		in(x);
		state^=(1<<(logN-x));
		outl(ans[state]);
	}

	return 0;
}


/*
	"Why do we fall? So we can learn how to pick ourselves up."
	- Alfred, Batman Begins -
*/

# include <iostream>
# include <cstdio>
# include <algorithm>
# include <cmath>
# include <set>
# include <cstring>
# include <string.h>
# include <sstream>
# include <map>
# include <queue>
# include <vector>

# define mp make_pair
# define LL long long
# define vi vector<int>
# define pqu priority_queue
# define iasc int,vector<int>,greater<int>
# define LLasc  LL,vector<LL>,greater<LL>
# define ins push_back
# define pii pair<int,int>
# define fi first
# define se second

inline void in(int &MAGNUS) {scanf("%d",&MAGNUS);}
inline void out(int MAGNUS) {printf("%d\n",MAGNUS);}
inline void in(int &MAGNUS,int &CLAR) {scanf("%d%d",&MAGNUS,&CLAR);}
inline void out(int MAGNUS,int CLAR) {printf("%d %d\n",MAGNUS,CLAR);}

inline void inl(LL &LIV) {scanf("%lld",&LIV);}
inline void inl(LL &LIV,LL &MART) {scanf("%lld%lld",&LIV,&MART);}
inline void outl(LL LIV) {printf("%lld\n",LIV);}
inline void outl(LL LIV, LL MART) {printf("%lld %lld\n",LIV,MART);}

# define FORU(a,b,c) for(int a=b; a<=c; a++)
# define FORD(a,b,c) for(int a=b; a>=c; a--)
# define FOR(a,b) for(int a=1; a<=b; a++)
# define FORA(a,b) for(auto (a) : (b))
# define REP(a,b) for(int a=0; a<b; a++)
# define RESET(a) memset(a,0,sizeof(a))
# define MEMO(a) memset(a,-1,sizeof(a))
# define ALL(a) (a).begin(),(a).end()
# define RNG(a,b) (a)+1,(a)+(b)+1
# define BSL(a,b) lower_bound(ALL(a),(b))-(a).begin()
# define BSLX(a,b,c) lower_bound((a)+1,(a)+(b)+1,(c))-a
# define BSU(a,b) upper_bound(ALL(a),(b))-(a).begin()
# define BSUX(a,b,c) upper_bound((a)+1,(a)+(b)+1,(c))-a
# define DEBUG puts("this-part-has-been-reach")

using namespace std;

int logN, N, x[(1<<21)+5], Q;
LL f[(1<<21)+5], g[(1<<21)+5];

LL binsearchlow(int lo,int hi,int key){
	LL ret=lo-1;
	while(lo<=hi){
		int mid=(lo+hi)/2;
		if(x[mid]<=key){
			ret=mid;
			lo=mid+1;
		}
		else hi=mid-1;
	}
	return ret;
}

LL binsearchup(int lo,int hi,int key){
	LL ret=hi+1;
	while(lo<=hi){
		int mid=(lo+hi)/2;
		if(x[mid]>=key){
			ret=mid;
			hi=mid-1;
		}
		else lo=mid+1;
	}
	return ret;
}

void DNC(int L,int R,int h){
	if(L>=R) return;
	int M=(L+R)/2;
	DNC(L,M,h-1);
	DNC(M+1,R,h-1);

	FORU(i,L,M){
		f[h]+=binsearchlow(M+1,R,x[i])-M;
		g[h]+=R-binsearchup(M+1,R,x[i])+1;
	}

	sort(x+L,x+R+1);
}

int main(){

	in(logN);
	N=1<<logN;
	FOR(i,N) in(x[i]);

	DNC(1,N,logN);

	in(Q);
	FOR(q,Q){
		int D;
		in(D);
		LL ans=0;
		FOR(h,D) swap(f[h], g[h]);
		FOR(h,logN) ans+=f[h];
		outl(ans);
	}

	return 0;
}


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

const int MAX_N = 20 + 5, MAX_2N = (1 << 20) + 5;
int a[MAX_2N], b[MAX_2N], dp[MAX_N], n;

void read_input() {
	cin >> n;
	for (int i = 0; i < (1 << n); i++)
		cin >> a[i];
}

void merge_sort(int l, int r) {
	if (l >= r - 1)
		return;
	int mid = (l + r) / 2;
	merge_sort(l, mid);
	merge_sort(mid, r);
	int inv = 0, p1 = l, p2 = mid, now = l;
	while (p1 < mid && p2 < r) {
		if (a[p1] <= a[p2])
			b[now++] = a[p1++];
		else {
			inv += mid - p1;
			b[now++] = a[p2++];
		}
	}
	while (p1 < mid)
		b[now++] = a[p1++];
	while (p2 < r)
		b[now++] = a[p2++];
	for (int i = l; i < r; i++)
		a[i] = b[i];
	dp[(int) log2(r - l)] += inv;
}

void solve() {
	merge_sort(0, 1 << n);
//	for (int i = 1; i <= n; i++)
//		dp[i] += dp[i - 1];
}

void print_output() {
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int q;
		cin >> q;
		if (n)
			for (int j = 1; j <= q; j++)
				dp[j] = (1 << (n + j - 2)) - dp[j];
		int ans = 0;
		for (int j = 0; j <= n; j++)
			ans += dp[j];
		cout << ans << endl;
	}
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	read_input();
	solve();
	print_output();
}


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

const int MAX_N = 20 + 5, MAX_2N = (1 << 20) + 5;
int a[MAX_2N], b[MAX_2N], dp[MAX_N], cnt[MAX_N], n;

void read_input() {
	cin >> n;
	for (int i = 0; i < (1 << n); i++)
		cin >> a[i];
}

void merge_sort(int l, int r) {
	if (l >= r - 1)
		return;
	int mid = (l + r) / 2;
	merge_sort(l, mid);
	merge_sort(mid, r);
	int x = log2(r - l);
	int p1 = l, p2 = mid, now = l;
	while (p1 < mid && p2 < r) {
		if (a[p1] <= a[p2]) {
			if (a[p1] == a[p2])
				cnt[x] += upper_bound(a + p2, a + r, a[p1]) - (a + p2);
			b[now++] = a[p1++];
		}
		else {
			dp[x] += mid - p1;
			b[now++] = a[p2++];
		}
	}
	while (p1 < mid)
		b[now++] = a[p1++];
	while (p2 < r)
		b[now++] = a[p2++];
	for (int i = l; i < r; i++)
		a[i] = b[i];
}

void solve() {
	merge_sort(0, 1 << n);
//	for (int i = 1; i <= n; i++)
//		dp[i] += dp[i - 1];
}

void print_output() {
//	for (int i = 0; i <= n; i++)
//		cout << i << " : " << cnt[i] << endl;
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int q;
		cin >> q;
		if (n)
			for (int j = 1; j <= q; j++)
				dp[j] = (1 << (n + j - 2)) - cnt[j] - dp[j];
		int ans = 0;
		for (int j = 0; j <= n; j++)
			ans += dp[j];
		cout << ans << endl;
	}
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	read_input();
	solve();
	print_output();
}


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

const int MAX_N = 20 + 5, MAX_2N = (1 << 20) + 5;
int a[MAX_2N], b[MAX_2N], n;
long long dp[MAX_N], cnt[MAX_N];

void read_input() {
	cin >> n;
	for (int i = 0; i < (1 << n); i++)
		cin >> a[i];
}

void merge_sort(int l, int r) {
	if (l >= r - 1)
		return;
	int mid = (l + r) / 2;
	merge_sort(l, mid);
	merge_sort(mid, r);
	int x = log2(r - l);
	int p1 = l, p2 = mid, now = l;
	while (p1 < mid && p2 < r) {
		if (a[p1] <= a[p2]) {
			if (a[p1] == a[p2])
				cnt[x] += upper_bound(a + p2, a + r, a[p1]) - (a + p2);
			b[now++] = a[p1++];
		}
		else {
			dp[x] += mid - p1;
			b[now++] = a[p2++];
		}
	}
	while (p1 < mid)
		b[now++] = a[p1++];
	while (p2 < r)
		b[now++] = a[p2++];
	for (int i = l; i < r; i++)
		a[i] = b[i];
}

void print_output() {
	int m;
	cin >> m;
	for (int i = 0; i < m; i++) {
		int q;
		cin >> q;
		if (n)
			for (int j = 1; j <= q; j++)
				dp[j] = (1LL << (n + j - 2)) - cnt[j] - dp[j];
		long long ans = 0;
		for (int j = 0; j <= n; j++)
			ans += dp[j];
		cout << ans << endl;
	}
}

int main() {
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	read_input();
	merge_sort(0, 1 << n);
	print_output();
}


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

#define ll long long
#define LL long long
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define sch(n) scanf(" %c", &n)
#define ss(n) scanf("%s", n)
#define pii pair <int, int>
#define pll pair <ll, ll>
#define plp pair <int, pll >
#define pb push_back
#define fr first
#define sc second
#define all(a)  a.begin(),a.end()
#define Unique(a)  sort(all(a)),a.erase(unique(all(a)),a.end())

#define inf (1LL<<50)
#define eps 1e-9

ll Set(ll n, ll pos)
{
    return n|(1LL<<pos);
}

bool Check(ll n, ll pos)
{
    return (bool) (n&(1LL<<pos));
}

ll a[2000010];

ll N;

ll nor[23], rev[23];

bool cur[23];

vector <ll> tree[8000010];
void create(ll node, ll lv, ll b, ll e)
{
    if(b == e)
    {
        tree[node].push_back(a[b]);
        return;
    }
    ll m = (b+e)/2, l = node*2;
    ll r = l+1;
    create(l, lv+1, b, m);
    create(r, lv+1, m+1, e);
    ll k1, k2, j;
    k1 = 0;
    k2 = 0;

    while(1)
    {
        if(k1 == tree[l].size())
        {
            for(j = k2; j < tree[r].size(); j++)
                tree[node].push_back(tree[r][j]);
            break;
        }
        if(k2 == tree[r].size())
        {
            for(j = k1; j < tree[l].size(); j++)
                tree[node].push_back(tree[l][j]);
            break;
        }

        if(tree[l][k1] < tree[r][k2])
        {
            if (tree[l][k1] != tree[r][k2])
                rev[lv] += tree[r].size() - k2;
            tree[node].push_back(tree[l][k1]);
            k1++;
        }
        else
        {
            if (tree[l][k1] != tree[r][k2])
                nor[lv] += tree[l].size() - k1;
            tree[node].push_back(tree[r][k2]);
            k2++;
        }

    }

 //   cout << b << " " << e << " " << lv << " " << nor[lv] << " " << rev[lv] << endl;

//    for (int i = 0; i < tree[node].size(); i++)
//    {
//        cout << tree[node][i] << " ";
//    }
//    cout << endl;
}

int main ()
{
    //  freopen("inl.txt", "r", stdin);
    // freopen("outu.txt", "w", stdout);
    //  ios_base::sync_with_stdio(0); // no printf/scanf must be present
    ll cs, ts, i, j, k, x, y, z, q, m, n;

    sl(n);

    N = (1LL<<n);

    for (i = 1; i <= N; i++)
        sl(a[i]);

    create(1, 0, 1, N);

    sl(m);

    while(m--)
    {
        sl(x);

        ll lv = n-x;

        for (i = lv; i <= n; i++)
        {
            cur[i] ^= 1;
        }

        ll ans = 0;

        for (i = 0; i <= n; i++)
        {
            if (!cur[i])
                ans += nor[i];
            else ans += rev[i];

        //    cout << i << " " << cur[i] << " " << nor[i] << " " << rev[i] << endl;
        }

        printf("%lld\n", ans);
    }

    return 0;
}


#include <bits/stdc++.h>`

#define slld(longvalue) scanf("%lld", &longvalue)
#define plld(longvalue) printf("%lld\n", longvalue)

#define slf(longvalue) scanf("%lf", &longvalue)
#define plf(longvalue) printf("%lf\n", longvalue)
#define sc(letter) scanf("%c", &letter)
#define pc(letter) printf("%c", letter)

#define ss(name) scanf("%s", name)
#define ps(name) printf("%s", name)

#define pnew printf("\n")

#define ll long long

#define printcase(indexing,ans) printf("Case %lld: %lld\n", indexing, ans)

#define pb(x) push_back(x)

#define bug printf("BUG\n")

#define mxlld LLONG_MAX
#define mnlld -LLONG_MAX

#define mxd 2e8
#define mnd -2e8

#define pi 3.14159265359

#define mod 1000000009

#define lim (1 << 21)
using namespace std;

ll arr[lim + 20];
vector < ll > tree[4 * lim];

ll normal[25];
ll rev[25];
bool check[25];

void merge_sort(ll node, ll a, ll b, ll lev) {
	if (a == b) {
		tree[node].pb(arr[a]);
		return;
	}
	ll mid = (a + b) / 2 , left, right;
	left = node * 2 ; right = left + 1;
	merge_sort( left, a, mid, lev + 1);
	merge_sort( right, mid + 1, b, lev + 1);

	ll i = 0, j = 0;

	while(1)
    {
        if(i == tree[left].size())
        {
            for(ll ind = j; ind < tree[right].size(); ind++)
            {
                tree[node].pb(tree[right][ind]);
            }
            break;
        }

        if(j == tree[right].size())
        {
            for(ll ind = i; ind < tree[left].size(); ind++)
            {
                tree[node].pb(tree[left][ind]);
            }
            break;
        }

        if(tree[left][i] > tree[right][j])
        {
            normal[lev] += tree[left].size()- i;
            tree[node].pb(tree[right][j]);
            j++;
        }
        else if(tree[left][i] < tree[right][j])
        {
            rev[lev] += tree[right].size() - j;
            tree[node].pb(tree[left][i]);
            i++;
        }
        else
        {
            tree[node].pb(tree[right][j]);
            j++;
        }

    }

}

//ll up( ll valu, ll node ) {
//
//    //cout<<valu<<" : "<<node<<endl;
//	ll l = 0 , r = tree[node].size() - 1 , mid;
//	ll pos = 0;
//
//	while ( l <= r )
//    {
//		mid = (l + r) / 2;
//
//		if ( tree[node][mid] <= valu )
//        {
//			pos = mid + 1;
//			l = mid + 1;
//		}
//		else
//        {
//            r = mid - 1;
//		}
//	}
//
//	ll x = (tree[node].size() - pos);
//
//	//cout << "check x: " << x << endl;
//
//	return x;
//}
//
//ll query(ll node, ll a, ll b, ll i, ll j, ll val) {
//	if ( a > j || b < i ) return 0;
//	if ( i <= a && b <= j ) {
//
//		ll ret = 0;
//		if ( tree[node].size() )
//			ret = up( val, node );
//		//scout<<"ret "<<ret<<endl;
//		return ret;
//
//	}
//	ll r1 , r2;
//	ll left, right, mid;
//	left = node * 2 ;  right = left + 1;
//	mid = (a + b) / 2;
//	r1 = query( left , a, mid, i , j , val );
//	r2 = query( right , mid + 1 , b, i, j, val);
//	return r1 + r2;
//
//}

int main()
{
    ll i, j, k, l, m, n, o;
    ll testcase;
    ll input, flag, tag, ans;

    // freopen("in.txt", "r", stdin);

    slld(n);

    ll sz = (1 << n);

    for(i = 1; i <= sz; i++) slld(arr[i]);

    merge_sort(1,1,sz,0);

    slld(m);

    for(i = 0; i < 24; i++) check[i] = 1;

    ll x;

    //bug;
    for(ll y = 1; y <= m; y++)
    {
        //cout << y << " :: " << endl;
        slld(x);

        ll lev = n - x;

        for(i = lev; i <= n; i++)
        {
            if(check[i]) check[i] = 0;
            else check[i] = 1;
        }

        ans = 0;
        for(i = 0; i <= n; i++)
        {
            if(check[i]) ans += normal[i];
            else ans += rev[i];
        }

        cout << ans << endl;
    }

}


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

#define ll long long
#define LL long long
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define sch(n) scanf(" %c", &n)
#define ss(n) scanf("%s", n)
#define pii pair <int, int>
#define pll pair <ll, ll>
#define plp pair <int, pll >
#define pb push_back
#define fr first
#define sc second
#define all(a)  a.begin(),a.end()
#define Unique(a)  sort(all(a)),a.erase(unique(all(a)),a.end())

#define inf (1LL<<50)
#define eps 1e-9

ll Set(ll n, ll pos)
{
    return n|(1LL<<pos);
}

bool Check(ll n, ll pos)
{
    return (bool) (n&(1LL<<pos));
}

ll a[200010];

ll N;

ll nor[23], rev[23];

bool cur[23];

vector <ll> tree[800010];
void create(ll node, ll lv, ll b, ll e)
{
    if(b == e)
    {
        tree[node].push_back(a[b]);
        return;
    }
    ll m = (b+e)/2, l = node*2;
    ll r = l+1;
    create(l, lv+1, b, m);
    create(r, lv+1, m+1, e);
    ll k1, k2, j;
    k1 = 0;
    k2 = 0;

    while(1)
    {
        if(k1 == tree[l].size())
        {
            for(j = k2; j < tree[r].size(); j++)
                tree[node].push_back(tree[r][j]);
            break;
        }
        if(k2 == tree[r].size())
        {
            for(j = k1; j < tree[l].size(); j++)
                tree[node].push_back(tree[l][j]);
            break;
        }

        if(tree[l][k1] < tree[r][k2])
        {
            if (tree[l][k1] != tree[r][k2])
                rev[lv] += tree[r].size() - k2;
            tree[node].push_back(tree[l][k1]);
            k1++;
        }
        else
        {
            if (tree[l][k1] != tree[r][k2])
                nor[lv] += tree[l].size() - k1;
            tree[node].push_back(tree[r][k2]);
            k2++;
        }

    }

 //   cout << b << " " << e << " " << lv << " " << nor[lv] << " " << rev[lv] << endl;

//    for (int i = 0; i < tree[node].size(); i++)
//    {
//        cout << tree[node][i] << " ";
//    }
//    cout << endl;
}

int main ()
{
    //  freopen("inl.txt", "r", stdin);
    // freopen("outu.txt", "w", stdout);
    //  ios_base::sync_with_stdio(0); // no printf/scanf must be present
    ll cs, ts, i, j, k, x, y, z, q, m, n;

    sl(n);

    N = 1<<n;

    for (i = 1; i <= N; i++)
        sl(a[i]);

    create(1, 0, 1, N);

    sl(m);

    while(m--)
    {
        sl(x);

        ll lv = n-x;

        for (i = lv; i <= n; i++)
        {
            cur[i] ^= 1;
        }

        ll ans = 0;

        for (i = 0; i <= n; i++)
        {
            if (!cur[i])
                ans += nor[i];
            else ans += rev[i];

        //    cout << i << " " << cur[i] << " " << nor[i] << " " << rev[i] << endl;
        }

        printf("%lld\n", ans);
    }

    return 0;
}


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

#define ll long long
#define LL long long
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define sch(n) scanf(" %c", &n)
#define ss(n) scanf("%s", n)
#define pii pair <int, int>
#define pll pair <ll, ll>
#define plp pair <int, pll >
#define pb push_back
#define fr first
#define sc second
#define all(a)  a.begin(),a.end()
#define Unique(a)  sort(all(a)),a.erase(unique(all(a)),a.end())

#define inf (1LL<<50)
#define eps 1e-9

ll Set(ll n, ll pos)
{
    return n|(1LL<<pos);
}

bool Check(ll n, ll pos)
{
    return (bool) (n&(1LL<<pos));
}

ll a[100010];

ll N;

ll nor[23], rev[23];

bool cur[23];

vector <ll> tree[400010];
void create(ll node, ll lv, ll b, ll e)
{
    if(b == e)
    {
        tree[node].push_back(a[b]);
        return;
    }
    ll m = (b+e)/2, l = node*2;
    ll r = l+1;
    create(l, lv+1, b, m);
    create(r, lv+1, m+1, e);
    ll k1, k2, j;
    k1 = 0;
    k2 = 0;

    while(1)
    {
        if(k1 == tree[l].size())
        {
            for(j = k2; j < tree[r].size(); j++)
                tree[node].push_back(tree[r][j]);
            break;
        }
        if(k2 == tree[r].size())
        {
            for(j = k1; j < tree[l].size(); j++)
                tree[node].push_back(tree[l][j]);
            break;
        }

        if(tree[l][k1] < tree[r][k2])
        {
            if (tree[l][k1] != tree[r][k2])
                rev[lv] += tree[r].size() - k2;
            tree[node].push_back(tree[l][k1]);
            k1++;
        }
        else
        {
            if (tree[l][k1] != tree[r][k2])
                nor[lv] += tree[l].size() - k1;
            tree[node].push_back(tree[r][k2]);
            k2++;
        }

    }

 //   cout << b << " " << e << " " << lv << " " << nor[lv] << " " << rev[lv] << endl;

//    for (int i = 0; i < tree[node].size(); i++)
//    {
//        cout << tree[node][i] << " ";
//    }
//    cout << endl;
}

int main ()
{
    //  freopen("inl.txt", "r", stdin);
    // freopen("outu.txt", "w", stdout);
    //  ios_base::sync_with_stdio(0); // no printf/scanf must be present
    ll cs, ts, i, j, k, x, y, z, q, m, n;

    sl(n);

    N = 1<<n;

    for (i = 1; i <= N; i++)
        sl(a[i]);

    create(1, 0, 1, N);

    sl(m);

    while(m--)
    {
        sl(x);

        ll lv = n-x;

        for (i = lv; i <= n; i++)
        {
            cur[i] ^= 1;
        }

        ll ans = 0;

        for (i = 0; i <= n; i++)
        {
            if (!cur[i])
                ans += nor[i];
            else ans += rev[i];

        //    cout << i << " " << cur[i] << " " << nor[i] << " " << rev[i] << endl;
        }

        printf("%lld\n", ans);
    }

    return 0;
}


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

#define ll long long
#define LL long long
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define sch(n) scanf(" %c", &n)
#define ss(n) scanf("%s", n)
#define pii pair <int, int>
#define pll pair <ll, ll>
#define plp pair <int, pll >
#define pb push_back
#define fr first
#define sc second
#define all(a)  a.begin(),a.end()
#define Unique(a)  sort(all(a)),a.erase(unique(all(a)),a.end())

#define inf (1LL<<50)
#define eps 1e-9

ll Set(ll n, ll pos)
{
    return n|(1LL<<pos);
}

bool Check(ll n, ll pos)
{
    return (bool) (n&(1LL<<pos));
}

ll a[2000010];

ll N;

ll nor[23], rev[23];

bool cur[23];

vector <ll> tree[8000010];
void create(ll node, ll lv, ll b, ll e)
{
    if(b == e)
    {
        tree[node].push_back(a[b]);
        return;
    }
    ll m = (b+e)/2, l = node*2;
    ll r = l+1;
    create(l, lv+1, b, m);
    create(r, lv+1, m+1, e);
    ll k1, k2, j;
    k1 = 0;
    k2 = 0;

    while(1)
    {
        if(k1 == tree[l].size())
        {
            for(j = k2; j < tree[r].size(); j++)
                tree[node].push_back(tree[r][j]);
            break;
        }
        if(k2 == tree[r].size())
        {
            for(j = k1; j < tree[l].size(); j++)
                tree[node].push_back(tree[l][j]);
            break;
        }

        if(tree[l][k1] <= tree[r][k2])
        {
            tree[node].push_back(tree[l][k1]);
            k1++;
        }
        else
        {
            nor[lv] += tree[l].size() - k1;
            tree[node].push_back(tree[r][k2]);
            k2++;
        }

    }

    k1 = k2 = 0;

    while(1)
    {
        if(k1 == tree[l].size())
        {
            break;
        }
        if(k2 == tree[r].size())
        {
            break;
        }

        if(tree[l][k1] < tree[r][k2])
        {
            rev[lv] += tree[r].size() - k2;
            k1++;
        }
        else
        {
            k2++;
        }

    }

  //  cout << b << " " << e << " " << lv << " " << nor[lv] << " " << rev[lv] << endl;

//    for (int i = 0; i < tree[node].size(); i++)
//    {
//        cout << tree[node][i] << " ";
//    }
//    cout << endl;
}

int main ()
{
    //  freopen("inl.txt", "r", stdin);
    // freopen("outu.txt", "w", stdout);
    //  ios_base::sync_with_stdio(0); // no printf/scanf must be present
    ll cs, ts, i, j, k, x, y, z, q, m, n;

    sl(n);

    N = (1LL<<n);

    for (i = 1; i <= N; i++)
        sl(a[i]);

    create(1, 0, 1, N);

    sl(m);

    while(m--)
    {
        sl(x);

        ll lv = n-x;

        for (i = lv; i <= n; i++)
        {
            cur[i] ^= 1;
        }

        ll ans = 0;

        for (i = 0; i <= n; i++)
        {
            if (!cur[i])
                ans += nor[i];
            else ans += rev[i];

        //    cout << i << " " << cur[i] << " " << nor[i] << " " << rev[i] << endl;
        }

        printf("%lld\n", ans);
    }

    return 0;
}


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

#define ll long long
#define LL long long
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define sch(n) scanf(" %c", &n)
#define ss(n) scanf("%s", n)
#define pii pair <int, int>
#define pll pair <ll, ll>
#define plp pair <int, pll >
#define pb push_back
#define fr first
#define sc second
#define all(a)  a.begin(),a.end()
#define Unique(a)  sort(all(a)),a.erase(unique(all(a)),a.end())

#define inf (1LL<<50)
#define eps 1e-9

ll Set(ll n, ll pos)
{
    return n|(1LL<<pos);
}

bool Check(ll n, ll pos)
{
    return (bool) (n&(1LL<<pos));
}

ll a[2000010];

ll N;

ll nor[23], rev[23];

bool cur[23];

vector <ll> tree[8000010];
void create(ll node, ll lv, ll b, ll e)
{
    if(b == e)
    {
        tree[node].push_back(a[b]);
        return;
    }
    ll m = (b+e)/2, l = node*2;
    ll r = l+1;
    create(l, lv+1, b, m);
    create(r, lv+1, m+1, e);
    ll k1, k2, j;
    k1 = 0;
    k2 = 0;

    while(1)
    {
        if(k1 == tree[l].size())
        {
            for(j = k2; j < tree[r].size(); j++)
                tree[node].push_back(tree[r][j]);
            break;
        }
        if(k2 == tree[r].size())
        {
            for(j = k1; j < tree[l].size(); j++)
                tree[node].push_back(tree[l][j]);
            break;
        }

        if(tree[l][k1] < tree[r][k2])
        {
            if (tree[l][k1] != tree[r][k2])
                rev[lv] += tree[r].size() - k2;
            tree[node].push_back(tree[l][k1]);
            k1++;
        }
        else
        {
            if (tree[l][k1] != tree[r][k2])
                nor[lv] += tree[l].size() - k1;
            tree[node].push_back(tree[r][k2]);
            k2++;
        }

    }

 //   cout << b << " " << e << " " << lv << " " << nor[lv] << " " << rev[lv] << endl;

//    for (int i = 0; i < tree[node].size(); i++)
//    {
//        cout << tree[node][i] << " ";
//    }
//    cout << endl;
}

int main ()
{
    //  freopen("inl.txt", "r", stdin);
    // freopen("outu.txt", "w", stdout);
    //  ios_base::sync_with_stdio(0); // no printf/scanf must be present
    ll cs, ts, i, j, k, x, y, z, q, m, n;

    sl(n);

    N = 1<<n;

    for (i = 1; i <= N; i++)
        sl(a[i]);

    create(1, 0, 1, N);

    sl(m);

    while(m--)
    {
        sl(x);

        ll lv = n-x;

        for (i = lv; i <= n; i++)
        {
            cur[i] ^= 1;
        }

        ll ans = 0;

        for (i = 0; i <= n; i++)
        {
            if (!cur[i])
                ans += nor[i];
            else ans += rev[i];

        //    cout << i << " " << cur[i] << " " << nor[i] << " " << rev[i] << endl;
        }

        printf("%lld\n", ans);
    }

    return 0;
}


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

#define ll long long
#define LL long long
#define sl(n) scanf("%lld", &n)
#define sf(n) scanf("%lf", &n)
#define si(n) scanf("%d", &n)
#define sch(n) scanf(" %c", &n)
#define ss(n) scanf("%s", n)
#define pii pair <int, int>
#define pll pair <ll, ll>
#define plp pair <int, pll >
#define pb push_back
#define fr first
#define sc second
#define all(a)  a.begin(),a.end()
#define Unique(a)  sort(all(a)),a.erase(unique(all(a)),a.end())

#define inf (1LL<<50)
#define eps 1e-9

ll Set(ll n, ll pos)
{
    return n|(1LL<<pos);
}

bool Check(ll n, ll pos)
{
    return (bool) (n&(1LL<<pos));
}

ll a[100010];

ll N;

ll nor[23], rev[23];

bool cur[23];

vector <ll> tree[400010];
void create(ll node, ll lv, ll b, ll e)
{
    if(b == e)
    {
        tree[node].push_back(a[b]);
        return;
    }
    ll m = (b+e)/2, l = node*2;
    ll r = l+1;
    create(l, lv+1, b, m);
    create(r, lv+1, m+1, e);
    ll k1, k2, j;
    k1 = 0;
    k2 = 0;

    while(1)
    {
        if(k1 == tree[l].size())
        {
            for(j = k2; j < tree[r].size(); j++)
                tree[node].push_back(tree[r][j]);
            break;
        }
        if(k2 == tree[r].size())
        {
            for(j = k1; j < tree[l].size(); j++)
                tree[node].push_back(tree[l][j]);
            break;
        }

        if(tree[l][k1] < tree[r][k2])
        {
            rev[lv] += tree[r].size() - k2;
            tree[node].push_back(tree[l][k1]);
            k1++;
        }
        else
        {
            nor[lv] += tree[l].size() - k1;
            tree[node].push_back(tree[r][k2]);
            k2++;
        }

    }

 //   cout << b << " " << e << " " << lv << " " << nor[lv] << " " << rev[lv] << endl;

//    for (int i = 0; i < tree[node].size(); i++)
//    {
//        cout << tree[node][i] << " ";
//    }
//    cout << endl;
}

int main ()
{
    //  freopen("inl.txt", "r", stdin);
    // freopen("outu.txt", "w", stdout);
    //  ios_base::sync_with_stdio(0); // no printf/scanf must be present
    ll cs, ts, i, j, k, x, y, z, q, m, n;

    sl(n);

    N = 1<<n;

    for (i = 1; i <= N; i++)
        sl(a[i]);

    create(1, 0, 1, N);

    sl(m);

    while(m--)
    {
        sl(x);

        ll lv = n-x;

        for (i = lv; i <= n; i++)
        {
            cur[i] ^= 1;
        }

        ll ans = 0;

        for (i = 0; i <= n; i++)
        {
            if (!cur[i])
                ans += nor[i];
            else ans += rev[i];

          //  cout << i << " " << cur[i] << " " << nor[i] << " " << rev[i] << endl;
        }

        printf("%lld\n", ans);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fi first
#define se second
#define pb push_back
#define int long long int

const int N = 3000005;
const int NN = 22;
int a[N],n,m;
int t[2][NN];
bitset<NN> b;
vector<int> v[N];
void merge(int nd,int s,int e,int d)
{
    if(s==e)
    {
        v[nd].pb(a[s]);
        return;
    }
    int m = (s+e)>>1;
    int nd1 = nd<<1;
    int nd2 = nd1|1;
    merge(nd1,s,m,d-1);
    merge(nd2,m+1,e,d-1);
    int i=0,j=0,n1=v[nd1].size(),n2=v[nd2].size();
    while(i<n1 && j<n2)
    {
        if(v[nd1][i]==v[nd2][j])
        {
            int val = v[nd1][i];
            int c1 = i,c2 = j;
            while(i<n1 && v[nd1][i]==val)
            {
                v[nd].pb(v[nd1][i++]);
                t[0][d]+=c2;
            }
            while(j<n2 && v[nd2][j]==val)
            {
                v[nd].pb(v[nd2][j++]);
                t[1][d]+=c1;
            }
        }
        else if(v[nd1][i]>v[nd2][j])
        {
            v[nd].pb(v[nd1][i++]);
            t[0][d]+=j;
        }
        else
        {
            v[nd].pb(v[nd2][j++]);
            t[1][d]+=i;
        }
    }
    while(i<n1)
    {
        v[nd].pb(v[nd1][i++]);
        t[0][d]+=j;
    }
    while(j<n2)
    {
        v[nd].pb(v[nd2][j++]);
        t[1][d]+=i;
    }
}
signed main()
{
   // freopen("inp.txt","r",stdin);
    scanf("%lld",&m);
    n = pow(2,m);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    merge(1,1,n,m);
    int q;
    scanf("%lld",&q);
    b.flip();
    while(q--)
    {
        int h;
        scanf("%lld",&h);
        for(int i=h;i>=0;i--)
            b[i]=1-b[i];
        int ans = 0;
        for(int i=0;i<=m;i++)
            ans+=t[b[i]][i];
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fi first
#define se second
#define pb push_back
#define int long long int

const int N = 2000005;
const int NN = 22;
int a[N],n,m;
int t[2][NN];
bitset<NN> b;
vector<int> v[N];
void merge(int nd,int s,int e,int d)
{
    if(s==e)
    {
        v[nd].pb(a[s]);
        return;
    }
    int m = (s+e)>>1;
    int nd1 = nd<<1;
    int nd2 = nd1|1;
    merge(nd1,s,m,d-1);
    merge(nd2,m+1,e,d-1);
    int i=0,j=0,n1=v[nd1].size(),n2=v[nd2].size();
    while(i<n1 && j<n2)
    {
        if(v[nd1][i]==v[nd2][j])
        {
            int val = v[nd1][i];
            int c1 = i,c2 = j;
            while(i<n1 && v[nd1][i]==val)
            {
                v[nd].pb(v[nd1][i++]);
                t[0][d]+=c2;
            }
            while(j<n2 && v[nd2][j]==val)
            {
                v[nd].pb(v[nd2][j++]);
                t[1][d]+=c1;
            }
        }
        else if(v[nd1][i]>v[nd2][j])
        {
            v[nd].pb(v[nd1][i++]);
            t[0][d]+=j;
        }
        else
        {
            v[nd].pb(v[nd2][j++]);
            t[1][d]+=i;
        }
    }
    while(i<n1)
    {
        v[nd].pb(v[nd1][i++]);
        t[0][d]+=j;
    }
    while(j<n2)
    {
        v[nd].pb(v[nd2][j++]);
        t[1][d]+=i;
    }
}
signed main()
{
   // freopen("inp.txt","r",stdin);
    scanf("%lld",&m);
    n = pow(2,m);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    merge(1,1,n,m);
    int q;
    scanf("%lld",&q);
    b.flip();
    while(q--)
    {
        int h;
        scanf("%lld",&h);
        for(int i=h;i>=0;i--)
            b[i]=1-b[i];
        int ans = 0;
        for(int i=0;i<=m;i++)
            ans+=t[b[i]][i];
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define fi first
#define se second
#define pb push_back
#define int long long int

const int N = 2000005;
const int NN = 22;
int a[N],n,m;
int t[2][NN];
bitset<NN> b;
vector<int> v[N];
void merge(int nd,int s,int e,int d)
{
    if(s==e)
    {
        v[nd].pb(a[s]);
        return;
    }
    int m = (s+e)>>1;
    int nd1 = nd<<1;
    int nd2 = nd1|1;
    merge(nd1,s,m,d-1);
    merge(nd2,m+1,e,d-1);
    int i=0,j=0,n1=v[nd1].size(),n2=v[nd2].size();
    while(i<n1 && j<n2)
    {
        if(v[nd1][i]==v[nd2][j])
            v[nd].pb(v[nd1][i++]);
        else if(v[nd1][i]>v[nd2][j])
        {
            v[nd].pb(v[nd1][i++]);
            t[0][d]+=j;
        }
        else
        {
            v[nd].pb(v[nd2][j++]);
            t[1][d]+=i;
        }
    }
    while(i<n1)
    {
        v[nd].pb(v[nd1][i++]);
        t[0][d]+=j;
    }
    while(j<n2)
    {
        v[nd].pb(v[nd2][j++]);
        t[1][d]+=i;
    }
}
signed main()
{
    //freopen("inp.txt","r",stdin);
    scanf("%lld",&m);
    n = pow(2,m);
    for(int i=1;i<=n;i++)
        scanf("%lld",&a[i]);
    merge(1,1,n,m);
    int q;
    scanf("%lld",&q);
    b.flip();
    while(q--)
    {
        int h;
        scanf("%lld",&h);
        for(int i=h;i>=0;i--)
            b[i]=1-b[i];
        int ans = 0;
        for(int i=0;i<=m;i++)
            ans+=t[b[i]][i];
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxn = 1e6+10;
int n, a[Maxn];
long long f[33], g[33];
bool cmp(int x, int y) {
  return x < y;
}

void merge(int l, int r, int h)
{
  if(l >= r) return;
  int Mid = (l + r) >> 1;
  merge(l, Mid, h - 1); merge(Mid + 1, r, h - 1);
  // cout<<l<<" "<<r<<" "<<h<<endl;
  for(int i = l; i <= Mid; i++){
    int to = lower_bound(a + Mid + 1, a + r + 1, a[i]) - a;
    f[h] += to - Mid - 1;
    //  printf("%d %d %lld\n", a[i], to, f[h]);
    to = upper_bound(a + Mid + 1, a + r + 1, a[i]) - a;
    g[h] += r - to + 1;
    // printf("%d %d %lld\n", a[i], to, g[h]);
  }
  sort(a+l, a+1+r, cmp);
}

int main()
{
  scanf("%d", &n);
  for(int i = 1; i <= 1<<n; i++){
    scanf("%d", &a[i]);
  }
  int m;
  merge(1, 1<<n, n);
  scanf("%d", &m);
  for(int i = 1; i <= m; i++){
    int x;
    long long ans = 0;
    scanf("%d", &x);
    for(int i = x; i > 0; i--){
      swap(f[i], g[i]);
    }
    for(int i = 1; i <= n; i++){
      ans += f[i];
    }
    printf("%lld\n", ans);
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxn = 1e6+10;
int n, a[Maxn];
long long f[33], g[33];
bool cmp(int x, int y) {
  return x < y;
}

void merge(int l, int r, int h)
{
  if(l >= r) return;
  int Mid = (l + r) >> 1;
  merge(l, Mid, h - 1); merge(Mid + 1, r, h - 1);
  cout<<l<<" "<<r<<" "<<h<<endl;
  for(int i = l; i <= Mid; i++){
    int to = lower_bound(a + Mid + 1, a + r + 1, a[i]) - a;
    f[h] += to - Mid - 1;
    //  printf("%d %d %lld\n", a[i], to, f[h]);
    to = upper_bound(a + Mid + 1, a + r + 1, a[i]) - a;
    g[h] += r - to + 1;
    // printf("%d %d %lld\n", a[i], to, g[h]);
  }
  sort(a+l, a+1+r, cmp);
}

int main()
{
  scanf("%d", &n);
  for(int i = 1; i <= 1<<n; i++){
    scanf("%d", &a[i]);
  }
  int m;
  merge(1, 1<<n, n);
  scanf("%d", &m);
  for(int i = 1; i <= m; i++){
    int x;
    long long ans = 0;
    scanf("%d", &x);
    for(int i = x; i > 0; i--){
      swap(f[i], g[i]);
    }
    for(int i = 1; i <= n; i++){
      ans += f[i];
    }
    printf("%lld\n", ans);
  }
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int Maxn = 2e6+10;
int n, a[Maxn];
long long f[33], g[33];
bool cmp(int x, int y) {
  return x < y;
}
void merge(int l, int r, int h)
{
  if(l >= r) return;
  int Mid = (l + r) >> 1;
  merge(l, Mid, h - 1); merge(Mid + 1, r, h - 1);
  // cout<<l<<" "<<r<<" "<<h<<endl;
  for(int i = l; i <= Mid; i++){
    int to = lower_bound(a + Mid + 1, a + r + 1, a[i]) - a;
    f[h] += to - Mid - 1;
    //  printf("%d %d %lld\n", a[i], to, f[h]);
    to = upper_bound(a + Mid + 1, a + r + 1, a[i]) - a;
    g[h] += r - to + 1;
    // printf("%d %d %lld\n", a[i], to, g[h]);
  }
  sort(a+l, a+1+r, cmp);
}

int main()
{
  scanf("%d", &n);
  for(int i = 1; i <= 1<<n; i++){
    scanf("%d", &a[i]);
  }
  int m;
  merge(1, 1<<n, n);
  scanf("%d", &m);
  for(int i = 1; i <= m; i++){
    int x;
    long long ans = 0;
    scanf("%d", &x);
    for(int i = x; i > 0; i--){
      swap(f[i], g[i]);
    }
    for(int i = 1; i <= n; i++){
      ans += f[i];
    }
    printf("%lld\n", ans);
  }
  // cout<<(1<<20)<<endl;
  return 0;
}


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

typedef long long ll;
const int MAXN = 1100000;

int n, N, m, k;
int num[MAXN];
ll sum[25], same[25];

ll merge(int l, int r, int x){
    if(l == r) return 0;

    int mid = (l + r) / 2;
    ll LHS = merge(l, mid, x+1);
    ll RHS = merge(mid+1, r, x+1);

    ll cnt = 0, mul = 0;
    int pos = mid+1;
    vector<int> V;
    for(int i = l; i <= mid; i++){
        while(pos <= r && num[pos] < num[i]) V.push_back(num[pos++]), mul++;
        V.push_back(num[i]); cnt += mul;
    }
    while(pos <= r) V.push_back(num[pos++]);

    ll scnt = 0, cur = 0;
    for(int i = 0; i < V.size(); i++){
        cur ++;
        if(i == V.size()-1 || V[i+1] != V[i]) scnt += (cur - 1) * cur /2, cur = 0;
    }

    for(int i = l; i <= r; i++){
        num[i] = V[i-l];
    }

    ll tot = LHS + RHS + cnt;
    same[x] += scnt;
    sum[x] += tot;
    return tot;
}

void update(int k){
    int i = n - k;
    ll dif = -sum[i];
    sum[i] = same[i] - sum[i];
    dif += sum[i];
    for(i = i-1; i >= 0; i--) sum[i] += dif;
}

int main(){
    fill(sum, sum+25, 0);
    fill(same, same+25, 0);
    scanf("%d", &n);
    N = (1 << n);
    for(int i = 0; i < N; i++){
        scanf("%d", &num[i]);
    }
    merge(0,N-1,0);
    for(int k = 0; k <= n; k++)
        same[n-k] = 1LL * (1 << (n - k)) * (1LL * (1<<k) * ((1<<k)-1) / 2) - same[n-k];
    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        scanf("%d", &k);
        update(k);
        cout << sum[0] << endl;
    }

}


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

typedef long long ll;
const int MAXN = (int)2e5;

int n, N, m, k;
int num[MAXN];
ll sum[25], same[25];

ll merge(int l, int r, int x){
    if(l == r) return 0;

    int mid = (l + r) / 2;
    ll LHS = merge(l, mid, x+1);
    ll RHS = merge(mid+1, r, x+1);

    ll cnt = 0, mul = 0;
    int pos = mid+1;
    vector<int> V;
    for(int i = l; i <= mid; i++){
        while(pos <= r && num[pos] < num[i]) V.push_back(num[pos++]), mul++;
        V.push_back(num[i]); cnt += mul;
    }
    while(pos <= r) V.push_back(num[pos++]);

    ll scnt = 0, cur = 0;
    for(int i = 0; i < V.size(); i++){
        cur ++;
        if(i == V.size()-1 || V[i+1] != V[i]) scnt += (cur - 1) * cur /2, cur = 0;
    }

    for(int i = l; i <= r; i++){
        num[i] = V[i-l];
    }

    ll tot = LHS + RHS + cnt;
    same[x] += scnt;
    sum[x] += tot;
    return tot;
}

void update(int k){
    int i = n - k;
    ll dif = -sum[i];
    sum[i] = same[i] - sum[i];
    dif += sum[i];
    for(i = i-1; i >= 0; i--) sum[i] += dif;
}

int main(){
    fill(sum, sum+25, 0);
    fill(same, same+25, 0);
    scanf("%d", &n);
    N = (1 << n);
    for(int i = 0; i < N; i++){
        scanf("%d", &num[i]);
    }
    merge(0,N-1,0);
    for(int k = 0; k <= n; k++)
        same[n-k] = 1LL * (1 << (n - k)) * (1LL * (1<<k) * ((1<<k)-1) / 2) - same[n-k];
    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        scanf("%d", &k);
        update(k);
        cout << sum[0] << endl;
    }

}


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

typedef long long ll;
const int MAXN = (int)2e5;

int n, N, m, k;
int num[MAXN];
ll sum[25];

ll merge(int l, int r, int x){
    if(l == r) return 0;

    int mid = (l + r) / 2;
    ll LHS = merge(l, mid, x+1);
    ll RHS = merge(mid+1, r, x+1);

    ll cnt = 0, mul = 0;
    int pos = mid+1;
    vector<int> V;
    for(int i = l; i <= mid; i++){
        while(pos <= r && num[pos] < num[i]) V.push_back(num[pos++]), mul++;
        V.push_back(num[i]); cnt += mul;
    }
    for(int i = l; i <= r; i++){
        num[i] = V[i-l];
    }

    ll tot = LHS + RHS + cnt;
    sum[x] += tot;
    return tot;
}

void update(int k){
    int i = n - k;
    ll dif = -sum[i];
    sum[i] = 1LL * (1 << (n - k)) * (1LL * (1<<k) * ((1<<k)-1) / 2) - sum[i];
    dif += sum[i];
    for(i = i-1; i >= 0; i--) sum[i] += dif;
}

int main(){
    fill(sum, sum+25, 0);
    scanf("%d", &n);
    N = (1 << n);
    for(int i = 0; i < N; i++){
        scanf("%d", &num[i]);
    }
    merge(0,N-1,0);

    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        scanf("%d", &k);
        update(k);
        cout << sum[0] << endl;
    }

}


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

typedef long long ll;
const int MAXN = 1100000;

int n, N, m, k;
int num[MAXN];
ll sum[25], same[25];

ll merge(int l, int r, int x){
    if(l == r) return 0;

    int mid = (l + r) / 2;
    ll LHS = merge(l, mid, x+1);
    ll RHS = merge(mid+1, r, x+1);

    ll cnt = 0, mul = 0;
    int pos = mid+1;
    vector<int> V;
    for(int i = l; i <= mid; i++){
        while(pos <= r && num[pos] < num[i]) V.push_back(num[pos++]), mul++;
        V.push_back(num[i]); cnt += mul;
    }
    while(pos <= r) V.push_back(num[pos++]);

    ll scnt = 0, cur = 0;
    for(int i = 0; i < V.size(); i++){
        cur ++;
        if(i == V.size()-1 || V[i+1] != V[i]) scnt += (cur - 1) * cur /2, cur = 0;
    }

    for(int i = l; i <= r; i++){
        num[i] = V[i-l];
    }

    ll tot = LHS + RHS + cnt;
    same[x] += scnt;
    sum[x] += tot;
    return tot;
}

void update(int k){
    int i = n - k;
    ll dif = -sum[i];
    sum[i] = same[i] - sum[i];
    dif += sum[i];
    for(int j = 20; j >= i+1; j--) sum[j] = same[j] - sum[j];
    for(int j = i-1; j >= 0; j--) sum[j] += dif;
}

int main(){
    fill(sum, sum+25, 0);
    fill(same, same+25, 0);
    scanf("%d", &n);
    N = (1 << n);
    for(int i = 0; i < N; i++){
        scanf("%d", &num[i]);
    }
    merge(0,N-1,0);
    for(int k = 0; k <= n; k++)
        same[n-k] = 1LL * (1 << (n - k)) * (1LL * (1<<k) * ((1<<k)-1) / 2) - same[n-k];
    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        scanf("%d", &k);
        update(k);
        cout << sum[0] << endl;
    }

}


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

typedef long long ll;
const int MAXN = (int)2e5;

int n, N, m, k;
int num[MAXN];
ll sum[25];

ll merge(int l, int r, int x){
    if(l == r) return 0;

    int mid = (l + r) / 2;
    ll LHS = merge(l, mid, x+1);
    ll RHS = merge(mid+1, r, x+1);

    ll cnt = 0, mul = 0;
    int pos = mid+1;
    vector<int> V;
    for(int i = l; i <= mid; i++){
        while(pos <= r && num[pos] < num[i]) V.push_back(num[pos++]), mul++;
        V.push_back(num[i]); cnt += mul;
    }
    for(int i = l; i <= r; i++){
        num[i] = V[i-l];
    }

    int tot = LHS + RHS + cnt;
    sum[x] += tot;
    return tot;
}

void update(int k){
    int i = n - k;
    ll dif = -sum[i];
    sum[i] = 1LL * (1 << (n - k)) * (1LL * (1<<k) * ((1<<k)-1) / 2) - sum[i];
    dif += sum[i];
    for(i = i-1; i >= 0; i--) sum[i] += dif;
}

int main(){
    scanf("%d", &n);
    N = (1 << n);
    for(int i = 0; i < N; i++){
        scanf("%d", &num[i]);
    }
    merge(0,N-1,0);

    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        scanf("%d", &k);
        update(k);
        cout << sum[0] << endl;
    }

}


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

typedef long long ll;
const int MAXN = 1100000;

int n, N, m, k;
int num[MAXN];
ll sum[25], same[25];

ll merge(int l, int r, int x){
    if(l == r) return 0;

    int mid = (l + r) / 2;
    ll LHS = merge(l, mid, x+1);
    ll RHS = merge(mid+1, r, x+1);

    ll cnt = 0, mul = 0;
    int pos = mid+1;
    vector<int> V;
    for(int i = l; i <= mid; i++){
        while(pos <= r && num[pos] < num[i]) V.push_back(num[pos++]), mul++;
        V.push_back(num[i]); cnt += mul;
    }
    while(pos <= r) V.push_back(num[pos++]);

    ll scnt = 0, cur = 0;
    for(int i = 0; i < V.size(); i++){
        cur ++;
        if(i == V.size()-1 || V[i+1] != V[i]) scnt += (cur - 1) * cur /2, cur = 0;
    }

    for(int i = l; i <= r; i++){
        num[i] = V[i-l];
    }

    ll tot = LHS + RHS + cnt;
    same[x] += scnt;
    sum[x] += tot;
    return tot;
}

void update(int k){
    int i = n - k;
    ll dif = -sum[i];
    sum[i] = same[i] - sum[i];
    dif += sum[i];
    for(int j = n; j >= i+1; j--) sum[j] = same[j] - sum[j];
    for(int j = i-1; j >= 0; j--) sum[j] += dif;
}

int main(){
    fill(sum, sum+25, 0);
    fill(same, same+25, 0);
    scanf("%d", &n);
    N = (1 << n);
    for(int i = 0; i < N; i++){
        scanf("%d", &num[i]);
    }
    merge(0,N-1,0);
    for(int k = 0; k <= n; k++)
        same[n-k] = 1LL * (1 << (n - k)) * (1LL * (1<<k) * ((1<<k)-1) / 2) - same[n-k];
    scanf("%d", &m);
    for(int i = 0; i < m; i++){
        scanf("%d", &k);
        update(k);
        printf("%I64d\n", sum[0]);
    }

}


#include<bits/stdc++.h>
#define rep(i,l,r) for (int i=l;i<=r;i++)
using namespace std;
const int maxn=15;
int n,m,a[maxn],b1[maxn],b2[maxn],c[maxn];
long long res[25][2];
void dfs(int l,int r,int len){
    if (l>=r) return;
    int mid=(l+r)/2;
    dfs(l,mid,len-1);
    dfs(mid+1,r,len-1);
    int r1=0,r2=0,l1=1,l2=1,now=0;
    rep(i,l,mid) b1[++r1]=a[i];
    rep(i,mid+1,r) b2[++r2]=a[i];
    while (l1<=r1||l2<=r2){
        if (l2>r2||l1<=r1&&b1[l1]<=b2[l2]){
            res[len][0]+=r2-l2+1;
            c[++now]=b1[l1++];
        }
        if (l1>r1||l2<=r2&&b2[l2]<b1[l1]){
            res[len][1]+=r1-l1+1;
            c[++now]=b2[l2++];
        }
    }
    rep(i,1,1<<len) a[l+i-1]=c[i];
}
int main(){
    scanf("%d",&n);
    rep(i,1,1<<n) scanf("%d",&a[i]);
    dfs(1,1<<n,n);
    scanf("%d",&m);
    while (m--){
        int q;
        scanf("%d",&q);
        long long ans=0;
        rep(i,1,n) {
            if (i<=q) swap(res[i][0],res[i][1]);
            ans+=res[i][1];
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,l,r) for (int i=l;i<=r;i++)
using namespace std;
const int maxn=1500500;
int n,m,a[maxn],b1[maxn],b2[maxn],c[maxn];
long long res[25][2];
void dfs(int l,int r,int len){
    if (l>=r) return;
    int mid=(l+r)/2;
    dfs(l,mid,len-1);
    dfs(mid+1,r,len-1);
    int r1=0,r2=0,l1=1,l2=1,now=0;
    rep(i,l,mid) b1[++r1]=a[i];
    rep(i,mid+1,r) b2[++r2]=a[i];
    while (l1<=r1||l2<=r2){
        if (l2>r2||l1<=r1&&b1[l1]<=b2[l2]){
            res[len][0]+=r2-l2+1;
            c[++now]=b1[l1++];
        }
        if (l1>r1||l2<=r2&&b2[l2]<b1[l1]){
            res[len][1]+=r1-l1+1;
            c[++now]=b2[l2++];
        }
    }
    rep(i,1,1<<len) a[l+i-1]=c[i];
}
int main(){
    scanf("%d",&n);
    rep(i,1,1<<n) scanf("%d",&a[i]);
    dfs(1,1<<n,n);
    scanf("%d",&m);
    while (m--){
        int q;
        scanf("%d",&q);
        long long ans=0;
        rep(i,1,n) {
            if (i<=q) swap(res[i][0],res[i][1]);
            ans+=res[i][1];
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,l,r) for (int i=l;i<=r;i++)
using namespace std;
const int maxn=1500500;
int n,m,a[maxn],b[maxn];
long long res[25][2];
void dfs(int l,int r,int len){
    if (l>=r) return;
    int mid=(l+r)/2;
    dfs(l,mid,len-1);
    dfs(mid+1,r,len-1);
    int l1=l,l2=mid+1,r1=mid,r2=r,now=0;
    while (l1<=r1&&l2<=r2){
        if (a[l1]<a[l2]) {
            res[len][0]+=r2-l2+1;
            l1++;
        }
        else l2++;
    }
    l1=l,l2=mid+1;
    while (l1<=r1&&l2<=r2){
        if (a[l1]>a[l2]){
            res[len][1]+=r1-l1+1;
            b[++now]=a[l2++];
        }
        else b[++now]=a[l1++];
    }
    while (l1<=r1) b[++now]=a[l1++];
    while (l2<=r2) b[++now]=a[l2++];
    rep(i,l,r) a[i]=b[i-l+1];
}
int main(){
    scanf("%d",&n);
    rep(i,1,1<<n) scanf("%d",&a[i]);
    dfs(1,1<<n,n);
    scanf("%d",&m);
    while (m--){
        int q;
        scanf("%d",&q);
        long long ans=0;
        rep(i,1,n) {
            if (i<=q) swap(res[i][0],res[i][1]);
            ans+=res[i][1];
        }
        printf("%lld\n",ans);
    }
    return 0;
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Agostinho Junior (junior94)
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
	int[] a, help;
	long[][] count;

	public void solve(int testNumber, InputReader in, OutputWriter out) {
		int N = in.readInt();
		a = new int[1<<N];
		help = new int[1<<N];
		count = new long[N + 1][2];
		for (int i = 0; i < a.length; i++) {
			a[i] = in.readInt();
		}
		mergeSort(N, 0, a.length - 1);
		int m = in.readInt();
		int mask = 0;
		while (m-- > 0) {
			int q = in.readInt();
			mask ^= 1 << q;
			int set = 0;
			long ans = 0;
			for (int i = N; i > 0; i--) {
				if (((1 << i) & mask) > 0) {
					set ^= 1;
				}
				ans += count[i][set];
			}
			out.println(ans);
		}
	}

	public void mergeSort(final int bit, final int start, final int end) {
		if (start == end) {
			return;
		}
		int mid = (start + end) / 2;
		mergeSort(bit - 1, start, mid);
		mergeSort(bit - 1, mid + 1, end);
		merge(bit, start, mid, end);
	}

	public void merge(final int bit, final int start, final int mid, final int end) {
		for (int i = start; i <= end; i++) {
			help[i] = a[i];
		}
		int i = start;
		int j = mid + 1;
		int k = start;
		long inv = 0;
		long matches = 0;
		for (; i <= mid && j <= end; k++) {
			if (help[i] <= help[j]) {
				if (help[i] == help[j]) {
					matches ++;
				}
				a[k] = help[i ++];
			} else {
				a[k] = help[j ++];
				inv += mid - i + 1;
			}
		}
		while (i <= mid) {
			if (help[i] == a[mid]) {
				matches ++;
			}
			a[k ++] = help[i ++];
		}
		count[bit][0] += inv;
		count[bit][1] += 1L * (end - (mid + 1)  + 1) * (mid - start + 1) - matches - inv;
	}
}

class InputReader {
	private BufferedReader input;
	private StringTokenizer line = new StringTokenizer("");
	public InputReader() {
		this(System.in);
	}
	public InputReader(InputStream in) {
		input = new BufferedReader(new InputStreamReader(in));
	}
	public InputReader(String s) {
		try {
			input = new BufferedReader(new FileReader(s));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}
	public void fill() {
		try {
			if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public int readInt() {
		fill();
		return Integer.parseInt(line.nextToken());
	}
}

class OutputWriter {
	private PrintWriter output;
	public OutputWriter() {
		this(System.out);
	}
	public OutputWriter(OutputStream out) {
		output = new PrintWriter(out);
	}
	public OutputWriter(Writer writer) {
		output = new PrintWriter(writer);
	}

	public void println(Object o) {
		output.println(o);
	}

	public void close() {
		output.close();
	}
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Agostinho Junior (junior94)
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
	int[] a, help;
	long[][] count;

	public void solve(int testNumber, InputReader in, OutputWriter out) {
		int N = in.readInt();
		a = new int[1<<N];
		help = new int[1<<N];
		count = new long[N + 1][2];
		for (int i = 0; i < a.length; i++) {
			a[i] = in.readInt();
		}
		mergeSort(N, 0, a.length - 1);
		int m = in.readInt();
		int mask = 0;
		while (m-- > 0) {
			int q = in.readInt();
			mask ^= 1 << q;
			int set = 0;
			long ans = 0;
			for (int i = N; i > 0; i--) {
				if (((1 << i) & mask) > 0) {
					set ^= 1;
				}
				ans += count[i][set];
			}
			out.println(ans);
		}
	}

	public void mergeSort(final int bit, final int start, final int end) {
		if (start == end) {
			return;
		}
		int mid = (start + end) / 2;
		mergeSort(bit - 1, start, mid);
		mergeSort(bit - 1, mid + 1, end);
		merge(bit, start, mid, end);
	}

	public void merge(final int bit, final int start, final int mid, final int end) {
		for (int i = start; i <= end; i++) {
			help[i] = a[i];
		}
		int i = start;
		int j = mid + 1;
		int k = start;
		long inv = 0;
		long matches = 0;
		for (; i <= mid && j <= end; k++) {
			//System.out.printf("help[%d] = %d, help[%d] = %d", i, help[i], j, help[j]);
			if (help[i] <= help[j]) {
				if (help[i] == help[j]) {
					matches ++;
				}
				a[k] = help[i ++];
			} else {
				a[k] = help[j ++];
				inv += mid - i + 1;
			}
		}
		while (i <= mid) {
			if (help[i] == a[mid]) {
				matches ++;
			}
			a[k ++] = help[i ++];
		}
		//long[] temp = {inv, (end - (mid + 1)  + 1) * (mid - start + 1) - matches - inv};
		count[bit][0] += inv;
		count[bit][1] += (end - (mid + 1)  + 1) * (mid - start + 1) - matches - inv;
		//System.out.printf("pairs: %d, matches: %d, different: %d\n", (end - (mid + 1)  + 1) * (mid - start + 1), matches, (end - (mid + 1)  + 1) * (mid - start + 1) - matches);
		//System.out.printf("bit: %d, help: %s, a: %s, count: %s\n", bit, Arrays.toString(Arrays.copyOfRange(help, start, end + 1)), Arrays.toString(Arrays.copyOfRange(a, start, end + 1)), Arrays.toString(temp));
	}
}

class InputReader {
	private BufferedReader input;
	private StringTokenizer line = new StringTokenizer("");
	public InputReader() {
		this(System.in);
	}
	public InputReader(InputStream in) {
		input = new BufferedReader(new InputStreamReader(in));
	}
	public InputReader(String s) {
		try {
			input = new BufferedReader(new FileReader(s));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}
	public void fill() {
		try {
			if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public int readInt() {
		fill();
		return Integer.parseInt(line.nextToken());
	}
}

class OutputWriter {
	private PrintWriter output;
	public OutputWriter() {
		this(System.out);
	}
	public OutputWriter(OutputStream out) {
		output = new PrintWriter(out);
	}
	public OutputWriter(Writer writer) {
		output = new PrintWriter(writer);
	}

	public void println(Object o) {
		output.println(o);
	}

	public void close() {
		output.close();
	}
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Agostinho Junior (junior94)
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
	int[] a, help;
	long[][] count;

	public void solve(int testNumber, InputReader in, OutputWriter out) {
		int N = in.readInt();
		a = new int[1<<N];
		help = new int[1<<N];
		count = new long[N + 1][2];
		for (int i = 0; i < a.length; i++) {
			a[i] = in.readInt();
		}
		mergeSort(N, 0, a.length - 1);
		int m = in.readInt();
		int mask = 0;
		while (m-- > 0) {
			int q = in.readInt();
			mask ^= 1 << q;
			int set = 0;
			long ans = 0;
			for (int i = N; i > 0; i--) {
				if (((1 << i) & mask) > 0) {
					set ^= 1;
				}
				ans += count[i][set];
			}
			out.println(ans);
		}
	}

	public void mergeSort(final int bit, final int start, final int end) {
		if (start == end) {
			return;
		}
		int mid = (start + end) / 2;
		mergeSort(bit - 1, start, mid);
		mergeSort(bit - 1, mid + 1, end);
		merge(bit, start, mid, end);
	}

	public void merge(final int bit, final int start, final int mid, final int end) {
		for (int i = start; i <= end; i++) {
			help[i] = a[i];
		}
		int i = start;
		int j = mid + 1;
		int k = start;
		long inv = 0;
		for (; i <= mid && j <= end; k++) {
			if (help[i] <= help[j]) {
				a[k] = help[i ++];
				i++;
			} else {
				a[k] = help[j ++];
				inv += mid - i + 1;
			}
		}
		while (i <= mid) {
			a[k ++] = help[i ++];
		}
		//long[] temp = {inv, (end - (mid + 1)  + 1) * (mid - start + 1) - inv};
		count[bit][0] += inv;
		count[bit][1] += (end - (mid + 1)  + 1) * (mid - start + 1) - inv;
		//System.out.println((end - (mid + 1)  + 1) * (mid - start + 1));
		//System.out.printf("bit: %d, help: %s, a: %s, count: %s\n", bit, Arrays.toString(Arrays.copyOfRange(help, start, end + 1)), Arrays.toString(Arrays.copyOfRange(a, start, end + 1)), Arrays.toString(temp));
	}
}

class InputReader {
	private BufferedReader input;
	private StringTokenizer line = new StringTokenizer("");
	public InputReader() {
		this(System.in);
	}
	public InputReader(InputStream in) {
		input = new BufferedReader(new InputStreamReader(in));
	}
	public InputReader(String s) {
		try {
			input = new BufferedReader(new FileReader(s));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}
	public void fill() {
		try {
			if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public int readInt() {
		fill();
		return Integer.parseInt(line.nextToken());
	}
}

class OutputWriter {
	private PrintWriter output;
	public OutputWriter() {
		this(System.out);
	}
	public OutputWriter(OutputStream out) {
		output = new PrintWriter(out);
	}
	public OutputWriter(Writer writer) {
		output = new PrintWriter(writer);
	}

	public void println(Object o) {
		output.println(o);
	}

	public void close() {
		output.close();
	}
}


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.StringTokenizer;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 * @author Agostinho Junior (junior94)
 */
public class Main {
	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		OutputWriter out = new OutputWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}
}

class TaskC {
	int[] a, help;
	long[][] count;

	public void solve(int testNumber, InputReader in, OutputWriter out) {
		int N = in.readInt();
		a = new int[1<<N];
		help = new int[1<<N];
		count = new long[N + 1][2];
		for (int i = 0; i < a.length; i++) {
			a[i] = in.readInt();
		}
		mergeSort(N, 0, a.length - 1);
		int m = in.readInt();
		int mask = 0;
		while (m-- > 0) {
			int q = in.readInt();
			mask ^= 1 << q;
			int set = 0;
			long ans = 0;
			for (int i = N; i > 0; i--) {
				if (((1 << i) & mask) > 0) {
					set ^= 1;
				}
				ans += count[i][set];
			}
			out.println(ans);
		}
	}

	public void mergeSort(final int bit, final int start, final int end) {
		if (start == end) {
			return;
		}
		int mid = (start + end) / 2;
		mergeSort(bit - 1, start, mid);
		mergeSort(bit - 1, mid + 1, end);
		merge(bit, start, mid, end);
	}

	public void merge(final int bit, final int start, final int mid, final int end) {
		for (int i = start; i <= end; i++) {
			help[i] = a[i];
		}
		int i = start;
		int j = mid + 1;
		int k = start;
		long inv = 0;
		long matches = 0;
		for (; i <= mid && j <= end; k++) {
			//System.out.printf("help[%d] = %d, help[%d] = %d", i, help[i], j, help[j]);
			if (help[i] <= help[j]) {
				if (help[i] == help[j]) {
					matches ++;
				}
				a[k] = help[i ++];
			} else {
				a[k] = help[j ++];
				inv += mid - i + 1;
			}
		}
		while (i <= mid) {
			if (help[i] == a[mid]) {
				matches ++;
			}
			a[k ++] = help[i ++];
		}
		//long[] temp = {inv, (end - (mid + 1)  + 1) * (mid - start + 1) - matches - inv};
		count[bit][0] += inv;
		count[bit][1] += 1L * (end - (mid + 1)  + 1) * (mid - start + 1) - matches - inv;
		//System.out.printf("pairs: %d, matches: %d, different: %d\n", (end - (mid + 1)  + 1) * (mid - start + 1), matches, (end - (mid + 1)  + 1) * (mid - start + 1) - matches);
		//System.out.printf("bit: %d, help: %s, a: %s, count: %s\n", bit, Arrays.toString(Arrays.copyOfRange(help, start, end + 1)), Arrays.toString(Arrays.copyOfRange(a, start, end + 1)), Arrays.toString(temp));
	}
}

class InputReader {
	private BufferedReader input;
	private StringTokenizer line = new StringTokenizer("");
	public InputReader() {
		this(System.in);
	}
	public InputReader(InputStream in) {
		input = new BufferedReader(new InputStreamReader(in));
	}
	public InputReader(String s) {
		try {
			input = new BufferedReader(new FileReader(s));
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}
	public void fill() {
		try {
			if(!line.hasMoreTokens()) line = new StringTokenizer(input.readLine());
		} catch(IOException io) { io.printStackTrace(); System.exit(0);}
	}

	public int readInt() {
		fill();
		return Integer.parseInt(line.nextToken());
	}
}

class OutputWriter {
	private PrintWriter output;
	public OutputWriter() {
		this(System.out);
	}
	public OutputWriter(OutputStream out) {
		output = new PrintWriter(out);
	}
	public OutputWriter(Writer writer) {
		output = new PrintWriter(writer);
	}

	public void println(Object o) {
		output.println(o);
	}

	public void close() {
		output.close();
	}
}


import static java.util.Arrays.*;
import static java.util.Collections.*;

import java.text.*;
import java.util.*;

public class C {

	public C () {
		int N = sc.nextInt(), Z = 1 << N;
		Integer [] A = new Integer [Z], B = new Integer [Z];
		for (int i : rep(Z))
			B[Z-1-i] = A[i] = sc.nextInt();

		long X = 0;

		long [] S = new long [N+1];
		for (int i : req(1, N))
			X += S[i] = calc(A, 1 << i);

		long [] T = new long [N+1];
		for (int i : req(1, N))
			T[i] = calc(B, 1 << i);

		int M = sc.nextInt();
		for (@SuppressWarnings("unused") int i : rep(M)) {
			int Q = sc.nextInt();
			for (int j : req(1, Q)) {
				X += T[j] - S[j];
				S[j] += T[j]; T[j] = S[j] - T[j]; S[j] -= T[j];
			}
			print(X);
		}
	}

	long calc(Integer [] A, int M) {
		int Z = A.length; long res = 0;
		Integer [] Q = new Integer [M];
		for (int i : rep(Z/M)) {
			System.arraycopy(A, M*i, Q, 0, M);
			sort(Q, 0, M/2); Integer [] K = invert(si(Q)); int k = 0;
			for (int j : rep(M/2)) {
				if (Q[j] > Q[k])
					k = j;
				res += K[k] - k;
			}
		}
		return res;
	}

	private static Integer [] invert(Integer [] J) {
		int N = J.length;
		Integer [] res = new Integer [N];
		for (int i = 0; i < N; ++i)
			res[J[i]] = i;
		return res;
	}
	private static int [] rep(int N) { return rep(0, N); }
	private static int [] rep(int S, int T) { if (T <= S) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
	private static int [] req(int S, int T) { return rep(S, T+1); }
	private static <T extends Comparable<T>> Integer [] si(final T [] X) { return si(X, reverseOrder(reverseOrder())); }
	private static <T> Integer [] si(final T [] X, final Comparator<T> C) {
		int N = X.length;
		Integer [] res = new Integer [N];
		for (int i = 0; i < N; ++i)
			res[i] = i;
		sort(res, new Comparator<Integer>() {
			public int compare(Integer i, Integer j) {
				int res = C.compare(X[i], X[j]);
				return res != 0 ? res : i.compareTo(j);
			}
		});
		return res;
	}
	////////////////////////////////////////////////////////////////////////////////////
	private final static IOUtils.MyScanner sc = new IOUtils.MyScanner();
	private static void print (Object o, Object ...a) { IOUtils.print(o, a); }
	private static void exit() { IOUtils.exit(); }
	private static class IOUtils {
		public static class MyScanner {
			public String next() { newLine(); return line[index++]; }
			public int nextInt() { return Integer.parseInt(next()); }
			//////////////////////////////////////////////
			private boolean eol() { return index == line.length; }
			private String readLine() {
				try {
					return r.readLine();
				} catch (Exception e) {
					throw new Error (e);
				}
			}
			private final java.io.BufferedReader r;
			private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
			private MyScanner (java.io.BufferedReader r) {
				try {
					this.r = r;
					while (!r.ready())
						Thread.sleep(1);
					start();
				} catch (Exception e) {
					throw new Error(e);
				}
			}
			private String [] line;
			private int index;
			private void newLine() {
				if (line == null || eol()) {
					line = split(readLine());
					index = 0;
				}
			}
			private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
		}
		private static String build(Object o, Object... a) { return buildDelim(" ", o, a); }
		private static String buildDelim(String delim, Object o, Object... a) {
			StringBuilder b = new StringBuilder();
			append(b, o, delim);
			for (Object p : a)
				append(b, p, delim);
			return b.substring(delim.length());
		}
		//////////////////////////////////////////////////////////////////////////////////
		private static void start() { if (t == 0) t = millis(); }
		private static void append(StringBuilder b, Object o, String delim) {
			if (o.getClass().isArray()) {
				int len = java.lang.reflect.Array.getLength(o);
				for (int i = 0; i < len; ++i)
					append(b, java.lang.reflect.Array.get(o, i), delim);
			} else if (o instanceof Iterable<?>)
				for (Object p : (Iterable<?>) o)
					append(b, p, delim);
			else {
				if (o instanceof Double)
					o = new DecimalFormat("#.############").format(o);
				b.append(delim).append(o);
			}
		}
		private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
		private static void print(Object o, Object ...a) { pw.println(build(o, a)); }
		private static void err(Object o, Object ...a) { System.err.println(build(o, a)); }
		private static void exit() {
			IOUtils.pw.close();
			System.out.flush();
			err("------------------");
			err(IOUtils.time());
			System.exit(0);
		}
		private static long t;
		private static long millis() { return System.currentTimeMillis(); }
		private static String time() { return "Time: " + (millis() - t) / 1000.0; }
	}
	public static void main (String[] args) { new C(); exit(); }
}


import static java.util.Arrays.*;
import static java.util.Collections.*;

import java.text.*;
import java.util.*;

public class C {

	public C () {
		int N = sc.nextInt(), Z = 1 << N;
		Integer [] A = new Integer [Z], B = new Integer [Z];
		for (int i : rep(Z))
			B[Z-1-i] = A[i] = sc.nextInt();

		long X = 0;

		long [] S = new long [N+1];
		for (int i : req(1, N))
			X += S[i] = calc(A, 1 << i);

		long [] T = new long [N+1];
		for (int i : req(1, N))
			T[i] = calc(B, 1 << i);

		int M = sc.nextInt();
		for (@SuppressWarnings("unused") int i : rep(M)) {
			int Q = sc.nextInt();
			for (int j : req(1, Q)) {
				X += T[j] - S[j];
				S[j] += T[j]; T[j] = S[j] - T[j]; S[j] -= T[j];
			}
			IOUtils.pw.println(X);
		}
	}

	long calc(Integer [] A, int M) {
		int Z = A.length; long res = 0;
		Integer [] Q = new Integer [M];
		for (int i : rep(Z/M)) {
			System.arraycopy(A, M*i, Q, 0, M);
			sort(Q, 0, M/2); Integer [] K = invert(si(Q)); int k = 0;
			for (int j : rep(M/2)) {
				if (Q[j] > Q[k])
					k = j;
				res += K[k] - k;
			}
		}
		return res;
	}

	private static Integer [] invert(Integer [] J) {
		int N = J.length;
		Integer [] res = new Integer [N];
		for (int i = 0; i < N; ++i)
			res[J[i]] = i;
		return res;
	}
	private static int [] rep(int N) { return rep(0, N); }
	private static int [] rep(int S, int T) { if (T <= S) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
	private static int [] req(int S, int T) { return rep(S, T+1); }
	private static <T extends Comparable<T>> Integer [] si(final T [] X) { return si(X, reverseOrder(reverseOrder())); }
	private static <T> Integer [] si(final T [] X, final Comparator<T> C) {
		int N = X.length;
		Integer [] res = new Integer [N];
		for (int i = 0; i < N; ++i)
			res[i] = i;
		sort(res, new Comparator<Integer>() {
			public int compare(Integer i, Integer j) {
				int res = C.compare(X[i], X[j]);
				return res != 0 ? res : i.compareTo(j);
			}
		});
		return res;
	}
	////////////////////////////////////////////////////////////////////////////////////
	private final static IOUtils.MyScanner sc = new IOUtils.MyScanner();
	private static void print (Object o, Object ...a) { IOUtils.print(o, a); }
	private static void exit() { IOUtils.exit(); }
	private static class IOUtils {
		public static class MyScanner {
			public String next() { newLine(); return line[index++]; }
			public int nextInt() { return Integer.parseInt(next()); }
			//////////////////////////////////////////////
			private boolean eol() { return index == line.length; }
			private String readLine() {
				try {
					return r.readLine();
				} catch (Exception e) {
					throw new Error (e);
				}
			}
			private final java.io.BufferedReader r;
			private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
			private MyScanner (java.io.BufferedReader r) {
				try {
					this.r = r;
					while (!r.ready())
						Thread.sleep(1);
					start();
				} catch (Exception e) {
					throw new Error(e);
				}
			}
			private String [] line;
			private int index;
			private void newLine() {
				if (line == null || eol()) {
					line = split(readLine());
					index = 0;
				}
			}
			private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
		}
		private static String build(Object o, Object... a) { return buildDelim(" ", o, a); }
		private static String buildDelim(String delim, Object o, Object... a) {
			StringBuilder b = new StringBuilder();
			append(b, o, delim);
			for (Object p : a)
				append(b, p, delim);
			return b.substring(delim.length());
		}
		//////////////////////////////////////////////////////////////////////////////////
		private static void start() { if (t == 0) t = millis(); }
		private static void append(StringBuilder b, Object o, String delim) {
			if (o.getClass().isArray()) {
				int len = java.lang.reflect.Array.getLength(o);
				for (int i = 0; i < len; ++i)
					append(b, java.lang.reflect.Array.get(o, i), delim);
			} else if (o instanceof Iterable<?>)
				for (Object p : (Iterable<?>) o)
					append(b, p, delim);
			else {
				if (o instanceof Double)
					o = new DecimalFormat("#.############").format(o);
				b.append(delim).append(o);
			}
		}
		private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
		private static void print(Object o, Object ...a) { pw.println(build(o, a)); }
		private static void err(Object o, Object ...a) { System.err.println(build(o, a)); }
		private static void exit() {
			IOUtils.pw.close();
			System.out.flush();
			err("------------------");
			err(IOUtils.time());
			System.exit(0);
		}
		private static long t;
		private static long millis() { return System.currentTimeMillis(); }
		private static String time() { return "Time: " + (millis() - t) / 1000.0; }
	}
	public static void main (String[] args) { new C(); exit(); }
}


import static java.util.Arrays.*;
import static java.util.Collections.*;

import java.text.*;
import java.util.*;

public class C {

	public C () {
		int N = sc.nextInt(), Z = 1 << N;
		Integer [] A = new Integer [Z], B = new Integer [Z];
		for (int i : rep(Z))
			B[Z-1-i] = A[i] = sc.nextInt();

		long X = 0;

		long [] S = new long [N+1];
		for (int i : req(1, N))
			X += S[i] = calc(A, 1 << i);

		long [] T = new long [N+1];
		for (int i : req(1, N))
			T[i] = calc(B, 1 << i);

		int M = sc.nextInt();
		for (@SuppressWarnings("unused") int i : rep(M)) {
			int Q = sc.nextInt();
			for (int j : req(1, Q)) {
				X += T[j] - S[j];
				S[j] += T[j]; T[j] = S[j] - T[j]; S[j] -= T[j];
			}
			print(X);
		}
	}

	long calc(Integer [] A, int M) {
		int Z = A.length; long res = 0;
		Integer [] Q = new Integer [M];
		for (int i : rep(Z/M)) {
			System.arraycopy(A, M*i, Q, 0, M);
			sort(Q, 0, M/2);
			Integer [] J = si(Q);
			for (int j : rep(M/2))
				res += J[j] - j;
		}
		return res;
	}

	private static int [] rep(int N) { return rep(0, N); }
	private static int [] rep(int S, int T) { if (T <= S) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
	private static int [] req(int S, int T) { return rep(S, T+1); }
	private static <T extends Comparable<T>> Integer [] si(final T [] X) { return si(X, reverseOrder(reverseOrder())); }
	private static <T> Integer [] si(final T [] X, final Comparator<T> C) {
		int N = X.length;
		Integer [] res = new Integer [N];
		for (int i = 0; i < N; ++i)
			res[i] = i;
		sort(res, new Comparator<Integer>() {
			public int compare(Integer i, Integer j) {
				int res = C.compare(X[i], X[j]);
				return res != 0 ? res : i.compareTo(j);
			}
		});
		return res;
	}
	////////////////////////////////////////////////////////////////////////////////////
	private final static IOUtils.MyScanner sc = new IOUtils.MyScanner();
	private static void print (Object o, Object ...a) { IOUtils.print(o, a); }
	private static void exit() { IOUtils.exit(); }
	private static class IOUtils {
		public static class MyScanner {
			public String next() { newLine(); return line[index++]; }
			public int nextInt() { return Integer.parseInt(next()); }
			//////////////////////////////////////////////
			private boolean eol() { return index == line.length; }
			private String readLine() {
				try {
					return r.readLine();
				} catch (Exception e) {
					throw new Error (e);
				}
			}
			private final java.io.BufferedReader r;
			private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
			private MyScanner (java.io.BufferedReader r) {
				try {
					this.r = r;
					while (!r.ready())
						Thread.sleep(1);
					start();
				} catch (Exception e) {
					throw new Error(e);
				}
			}
			private String [] line;
			private int index;
			private void newLine() {
				if (line == null || eol()) {
					line = split(readLine());
					index = 0;
				}
			}
			private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
		}
		private static String build(Object o, Object... a) { return buildDelim(" ", o, a); }
		private static String buildDelim(String delim, Object o, Object... a) {
			StringBuilder b = new StringBuilder();
			append(b, o, delim);
			for (Object p : a)
				append(b, p, delim);
			return b.substring(delim.length());
		}
		//////////////////////////////////////////////////////////////////////////////////
		private static void start() { if (t == 0) t = millis(); }
		private static void append(StringBuilder b, Object o, String delim) {
			if (o.getClass().isArray()) {
				int len = java.lang.reflect.Array.getLength(o);
				for (int i = 0; i < len; ++i)
					append(b, java.lang.reflect.Array.get(o, i), delim);
			} else if (o instanceof Iterable<?>)
				for (Object p : (Iterable<?>) o)
					append(b, p, delim);
			else {
				if (o instanceof Double)
					o = new DecimalFormat("#.############").format(o);
				b.append(delim).append(o);
			}
		}
		private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
		private static void print(Object o, Object ...a) { pw.println(build(o, a)); }
		private static void err(Object o, Object ...a) { System.err.println(build(o, a)); }
		private static void exit() {
			IOUtils.pw.close();
			System.out.flush();
			err("------------------");
			err(IOUtils.time());
			System.exit(0);
		}
		private static long t;
		private static long millis() { return System.currentTimeMillis(); }
		private static String time() { return "Time: " + (millis() - t) / 1000.0; }
	}
	public static void main (String[] args) { new C(); exit(); }
}


import static java.util.Arrays.*;
import static java.util.Collections.*;

import java.text.*;
import java.util.*;

public class C {

	public C () {
		int N = sc.nextInt(), Z = 1 << N;
		int [] A = new int [Z], B = new int [Z];
		for (int i = 0; i < Z; ++i)
			B[Z-1-i] = A[i] = sc.nextInt();

		long X = 0;

		long [] S = new long [N+1];
		for (int i = 1; i <= N; ++i)
			X += S[i] = calc(A, 1 << i);

		long [] T = new long [N+1];
		for (int i = 1; i <= N; ++i)
			T[i] = calc(B, 1 << i);

		int M = sc.nextInt();
		for (int i = 0; i < M; ++i) {
			int Q = sc.nextInt();
			for (int j = 1; j <= Q; ++j) {
				X += T[j] - S[j];
				S[j] += T[j]; T[j] = S[j] - T[j]; S[j] -= T[j];
			}
			IOUtils.pw.println(X);
		}
	}

	long calc(int [] A, int M) {
		int Z = A.length; long res = 0;
		int [] P = new int [M];
		for (int i = 0; i < Z/M; ++i) {
			System.arraycopy(A, M*i, P, 0, M); Integer [] Q = toObject(P);
			sort(Q, 0, M/2); int [] K = invert(si(Q)); int k = 0;
			for (int j = 0; j < M/2; ++j) {
				if (Q[j] > Q[k])
					k = j;
				res += K[k] - k;
			}
		}
		return res;
	}

	private static int [] invert(int [] J) {
		int N = J.length;
		int [] res = new int [N];
		for (int i = 0; i < N; ++i)
			res[J[i]] = i;
		return res;
	}
	private static <T extends Comparable<T>> int [] si(final T [] X) { return si(X, reverseOrder(reverseOrder())); }
	private static <T> int [] si(final T [] X, final Comparator<T> C) {
		int N = X.length;
		Integer [] res = new Integer [N];
		for (int i = 0; i < N; ++i)
			res[i] = i;
		sort(res, new Comparator<Integer>() {
			public int compare(Integer i, Integer j) {
				int res = C.compare(X[i], X[j]);
				return res != 0 ? res : i.compareTo(j);
			}
		});
		return toPrimitive(res);
	}
	private static Integer [] toObject(int ... A) { Integer [] res = new Integer [A.length]; for (int i = 0; i < A.length; ++i) res[i] = A[i]; return res; }
	private static int [] toPrimitive(Integer [] A) { int [] res = new int [A.length]; for (int i = 0; i < A.length; ++i) res[i] = A[i]; return res; }
	////////////////////////////////////////////////////////////////////////////////////
	private final static IOUtils.MyScanner sc = new IOUtils.MyScanner();
	private static void exit() { IOUtils.exit(); }
	private static class IOUtils {
		public static class MyScanner {
			public String next() { newLine(); return line[index++]; }
			public int nextInt() { return Integer.parseInt(next()); }
			//////////////////////////////////////////////
			private boolean eol() { return index == line.length; }
			private String readLine() {
				try {
					return r.readLine();
				} catch (Exception e) {
					throw new Error (e);
				}
			}
			private final java.io.BufferedReader r;
			private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
			private MyScanner (java.io.BufferedReader r) {
				try {
					this.r = r;
					while (!r.ready())
						Thread.sleep(1);
					start();
				} catch (Exception e) {
					throw new Error(e);
				}
			}
			private String [] line;
			private int index;
			private void newLine() {
				if (line == null || eol()) {
					line = split(readLine());
					index = 0;
				}
			}
			private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
		}
		private static String build(Object o, Object... a) { return buildDelim(" ", o, a); }
		private static String buildDelim(String delim, Object o, Object... a) {
			StringBuilder b = new StringBuilder();
			append(b, o, delim);
			for (Object p : a)
				append(b, p, delim);
			return b.substring(delim.length());
		}
		//////////////////////////////////////////////////////////////////////////////////
		private static void start() { if (t == 0) t = millis(); }
		private static void append(StringBuilder b, Object o, String delim) {
			if (o.getClass().isArray()) {
				int len = java.lang.reflect.Array.getLength(o);
				for (int i = 0; i < len; ++i)
					append(b, java.lang.reflect.Array.get(o, i), delim);
			} else if (o instanceof Iterable<?>)
				for (Object p : (Iterable<?>) o)
					append(b, p, delim);
			else {
				if (o instanceof Double)
					o = new DecimalFormat("#.############").format(o);
				b.append(delim).append(o);
			}
		}
		private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
		private static void err(Object o, Object ...a) { System.err.println(build(o, a)); }
		private static void exit() {
			IOUtils.pw.close();
			System.out.flush();
			err("------------------");
			err(IOUtils.time());
			System.exit(0);
		}
		private static long t;
		private static long millis() { return System.currentTimeMillis(); }
		private static String time() { return "Time: " + (millis() - t) / 1000.0; }
	}
	public static void main (String[] args) { new C(); exit(); }
}


import static java.util.Arrays.*;

import java.text.*;

public class C {

	public C () {
		int N = sc.nextInt(), Z = 1 << N;
		Integer [] A = new Integer [Z], B = new Integer [Z];
		for (int i : rep(Z))
			B[Z-1-i] = A[i] = sc.nextInt();

		long X = 0;

		long [] S = new long [N+1];
		for (int i : req(1, N))
			X += S[i] = calc(A, 1 << i);

		long [] T = new long [N+1];
		for (int i : req(1, N))
			T[i] = calc(B, 1 << i);

		int M = sc.nextInt();
		for (@SuppressWarnings("unused") int i : rep(M)) {
			int Q = sc.nextInt();
			for (int j : req(1, Q)) {
				X += T[j] - S[j];
				S[j] += T[j]; T[j] = S[j] - T[j]; S[j] -= T[j];
			}
			print(X);
		}
	}

	long calc(Integer [] A, int M) {
		int Z = A.length; long res = 0;
		for (int i : rep(Z/M)) {
			int S = M*i, p = S, q = S + M/2; long c = 0;
			int P = S + M/2, Q = S + M;
			sort(A, p, P); sort(A, q, Q);
			while (p < P)
				if (q == Q || A[p] <= A[q]) {
					++p; res += c;
				} else {
					++q; ++c;
				}
		}
		return res;
	}

	private static int [] rep(int N) { return rep(0, N); }
	private static int [] rep(int S, int T) { if (T <= S) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
	private static int [] req(int S, int T) { return rep(S, T+1); }
	////////////////////////////////////////////////////////////////////////////////////
	private final static IOUtils.MyScanner sc = new IOUtils.MyScanner();
	private static void print (Object o, Object ...a) { IOUtils.print(o, a); }
	private static void exit() { IOUtils.exit(); }
	private static class IOUtils {
		public static class MyScanner {
			public String next() { newLine(); return line[index++]; }
			public int nextInt() { return Integer.parseInt(next()); }
			//////////////////////////////////////////////
			private boolean eol() { return index == line.length; }
			private String readLine() {
				try {
					return r.readLine();
				} catch (Exception e) {
					throw new Error (e);
				}
			}
			private final java.io.BufferedReader r;
			private MyScanner () { this(new java.io.BufferedReader(new java.io.InputStreamReader(System.in))); }
			private MyScanner (java.io.BufferedReader r) {
				try {
					this.r = r;
					while (!r.ready())
						Thread.sleep(1);
					start();
				} catch (Exception e) {
					throw new Error(e);
				}
			}
			private String [] line;
			private int index;
			private void newLine() {
				if (line == null || eol()) {
					line = split(readLine());
					index = 0;
				}
			}
			private String [] split(String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
		}
		private static String build(Object o, Object... a) { return buildDelim(" ", o, a); }
		private static String buildDelim(String delim, Object o, Object... a) {
			StringBuilder b = new StringBuilder();
			append(b, o, delim);
			for (Object p : a)
				append(b, p, delim);
			return b.substring(delim.length());
		}
		//////////////////////////////////////////////////////////////////////////////////
		private static void start() { if (t == 0) t = millis(); }
		private static void append(StringBuilder b, Object o, String delim) {
			if (o.getClass().isArray()) {
				int len = java.lang.reflect.Array.getLength(o);
				for (int i = 0; i < len; ++i)
					append(b, java.lang.reflect.Array.get(o, i), delim);
			} else if (o instanceof Iterable<?>)
				for (Object p : (Iterable<?>) o)
					append(b, p, delim);
			else {
				if (o instanceof Double)
					o = new DecimalFormat("#.############").format(o);
				b.append(delim).append(o);
			}
		}
		private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
		private static void print(Object o, Object ...a) { pw.println(build(o, a)); }
		private static void err(Object o, Object ...a) { System.err.println(build(o, a)); }
		private static void exit() {
			IOUtils.pw.close();
			System.out.flush();
			err("------------------");
			err(IOUtils.time());
			System.exit(0);
		}
		private static long t;
		private static long millis() { return System.currentTimeMillis(); }
		private static String time() { return "Time: " + (millis() - t) / 1000.0; }
	}
	public static void main (String[] args) { new C(); exit(); }
}