#include <bits/stdc++.h>   using namespace std;   int n;   int a[1111111];   bool curr_state[22];   long long sum_inversions[22];   long long sum_non_inversions[22];   int tmp_array1[1111111];   int tmp_array2[1111111];   int binary_search(int i, int sz) {     if (tmp_array2[0] >= tmp_array1[i]) return -1;     int low = 0;     int high = sz - 1;     while (low < high) {       int mid = (low + high + 1) / 2;       if (tmp_array2[mid] >= tmp_array1[i])         high = mid - 1;       else         low = mid;     }     return low;   }   long long find_num_inversions(int sz) {     sort(tmp_array2, tmp_array2 + sz);     long long answer = 0;     for (int i = 0; i < sz; i++) {       int j = binary_search(i, sz);       answer += j + 1;     }     return answer;   }   int main() {     scanf( %d , &n);     for (int i = 0; i < (1 << n); i++) scanf( %d , &a[i]);     for (int level = 1; level <= n; level++) {       sum_inversions[level - 1] = 0;       sum_non_inversions[level - 1] = 0;       for (int j = 0; j < (1 << n); j += (1 << (n - level + 1))) {         int l1 = j;         int u1 = j + (1 << (n - level)) - 1;         int l2 = j + (1 << (n - level));         int u2 = j + (1 << (n - level + 1)) - 1;         for (int i = l1; i <= u1; i++) {           tmp_array1[i - l1] = a[i];         }         for (int j = l2; j <= u2; j++) {           tmp_array2[j - l2] = a[j];         }         sum_inversions[level - 1] += find_num_inversions(u1 - l1 + 1);         for (int i = l1; i <= u1; i++) {           tmp_array2[i - l1] = a[i];         }         for (int j = l2; j <= u2; j++) {           tmp_array1[j - l2] = a[j];         }         sum_non_inversions[level - 1] += find_num_inversions(u1 - l1 + 1);       }     }     for (int i = 0; i <= n - 1; i++) {       curr_state[i] = false;     }     int m;     scanf( %d , &m);     for (int i = 1; i <= m; i++) {       int q;       scanf( %d , &q);       for (int j = n - q; j <= n - 1; j++) curr_state[j] = (!curr_state[j]);       long long answer = 0;       for (int level = 0; level <= n - 1; level++)         answer += (curr_state[level] ? sum_non_inversions[level]                                      : sum_inversions[level]);       cout << answer << endl;     }   }   