#include <bits/stdc++.h>
using namespace std;
long long Scal(vector<int>& a, vector<int>& b) {
  int n = a.size();
  long long odp = 0;
  int i = 0, j = 0;
  while (i < n && j < n) {
    if (a[i] <= b[j])
      ++i;
    else {
      odp += n - i;
      ++j;
    }
  }
  return odp;
}
int main() {
  int n;
  scanf("%d", &n);
  int wlk = 1 << n;
  vector<int> a(wlk);
  for (int i = 0; i < wlk; ++i) scanf("%d", &a[i]);
  vector<vector<long long> > inw(2, vector<long long>(n + 1, 0));
  for (int wyk = 1; wyk <= n; ++wyk) {
    int blok = 1 << wyk;
    for (int p1 = 0; p1 < wlk; p1 += blok) {
      int k1 = p1 + blok / 2 - 1;
      int p2 = k1 + 1;
      int k2 = p2 + blok / 2 - 1;
      vector<int> L, P;
      for (int i = p1; i <= k1; ++i) L.push_back(a[i]);
      for (int i = p2; i <= k2; ++i) P.push_back(a[i]);
      inw[0][wyk] += Scal(L, P);
      inw[1][wyk] += Scal(P, L);
      for (int i = 0; i < blok / 2; ++i) L.push_back(P[i]);
      sort(L.begin(), L.end());
      for (int i = 0; i < blok; ++i) a[p1 + i] = L[i];
    }
  }
  int m;
  scanf("%d", &m);
  vector<int> stan(n + 1, 0);
  long long odp = 0;
  for (int i = 0; i <= n; ++i) odp += inw[0][i];
  for (int i = 0; i < m; ++i) {
    int q;
    scanf("%d", &q);
    for (int j = 0; j <= q; ++j) {
      odp -= inw[stan[j]][j];
      stan[j] ^= 1;
      odp += inw[stan[j]][j];
    }
    printf("%I64d\n", odp);
  }
  return 0;
}
