#include <bits/stdc++.h>
using namespace std;
const int bfsz = 1 << 16;
char bf[bfsz + 5];
int rsz = 0;
int ptr = 0;
char gc() {
  if (rsz <= 0) {
    ptr = 0;
    rsz = fread(bf, 1, bfsz, stdin);
    if (rsz <= 0) return EOF;
  }
  --rsz;
  return bf[ptr++];
}
void ga(char &c) {
  c = EOF;
  while (!isalpha(c)) c = gc();
}
int gs(char s[]) {
  int l = 0;
  char c = gc();
  while (isspace(c)) c = gc();
  while (c != EOF && !isspace(c)) {
    s[l++] = c;
    c = gc();
  }
  s[l] = '\0';
  return l;
}
template <class T>
bool gi(T &v) {
  v = 0;
  char c = gc();
  while (c != EOF && c != '-' && !isdigit(c)) c = gc();
  if (c == EOF) return false;
  bool neg = c == '-';
  if (neg) c = gc();
  while (isdigit(c)) {
    v = v * 10 + c - '0';
    c = gc();
  }
  if (neg) v = -v;
  return true;
}
bool exitInput = false;
int ntest = 1, itest = 1;
const int dx[4] = {-1, +0, +1, -0};
const int dy[4] = {+0, +1, +0, -1};
const long long Mod = 1e9 + 7;
const int maxn = (1 << 20) + 5;
const int maxv = 20 + 5;
const int maxe = 4 * (1 << 20) + 5;
int Logn, a[maxn], b[maxn], arr[maxv][maxn];
long long n, res, inversion_node[maxe], inversion_level[maxv],
    equal_pair_node[maxe], equal_pair_level[maxv];
long long totalChange;
void build(int node, int L, int R, int level) {
  if (L == R) {
    inversion_node[node] = equal_pair_node[node] = 0;
    arr[level][L] = a[L];
    return;
  }
  int mid = (L + R) >> 1;
  build((node << 1), L, mid, level - 1);
  build(((node << 1) | 1), mid + 1, R, level - 1);
  inversion_node[node] =
      inversion_node[(node << 1)] + inversion_node[((node << 1) | 1)];
  equal_pair_node[node] =
      equal_pair_node[(node << 1)] + equal_pair_node[((node << 1) | 1)];
  int i, j, k, cnt_smaller, cnt_equal;
  cnt_smaller = cnt_equal = 0;
  for (i = L, j = mid + 1, k = 0; i <= mid; ++i) {
    if (i == L || arr[level - 1][i] > arr[level - 1][i - 1]) {
      cnt_smaller += cnt_equal;
      for (; j <= R && arr[level - 1][j] < arr[level - 1][i];
           ++j, ++cnt_smaller) {
        b[k++] = arr[level - 1][j];
      }
      cnt_equal = 0;
      for (; j <= R && arr[level - 1][j] == arr[level - 1][i];
           ++j, ++cnt_equal) {
        b[k++] = arr[level - 1][j];
      }
    }
    inversion_node[node] += cnt_smaller;
    equal_pair_node[node] += cnt_equal;
    b[k++] = arr[level - 1][i];
  }
  for (; j <= R; ++j) b[k++] = arr[level - 1][j];
  inversion_level[level] += inversion_node[node];
  equal_pair_level[level] += equal_pair_node[node];
  for (i = 0; i <= R - L; ++i) {
    arr[level][L + i] = b[i];
  }
}
void pre() {}
void read() {
  gi(Logn);
  n = 1 << Logn;
  for (int i = 1; i <= n; ++i) {
    gi(a[i]);
  }
}
void solve() {
  build(1, 1, n, Logn);
  res = inversion_level[Logn];
  int Q, q, len, i;
  gi(Q);
  while (Q--) {
    gi(q);
    if (q > 0) {
      len = 1 << q;
      totalChange =
          n * (len - 1) / 2 - inversion_level[q] * 2 - equal_pair_level[q];
      res = res + totalChange;
      for (i = q; i >= 0; --i) {
        len = 1 << i;
        inversion_level[i] =
            n * (len - 1LL) / 2 - inversion_level[i] - equal_pair_level[i];
      }
      for (i = q + 1; i <= Logn; ++i) {
        inversion_level[i] += totalChange;
      }
    }
    printf("%lld\n", res);
  }
}
int main() {
  pre();
  for (itest = 1; itest <= ntest; ++itest) {
    read();
    if (exitInput) {
      break;
    }
    solve();
  }
  return 0;
}
