#include <bits/stdc++.h>
using namespace std;
int a[250006], n, s_size, m;
long long sum = 0;
struct trie_nod_idx {
trie_nod_idx* t[2];
int c[2];
trie_nod_idx() {
t[0] = t[1] = NULL;
c[0] = c[1] = 0;
}
};
struct trie_idx {
trie_nod_idx* root;
trie_idx() {
root = new trie_nod_idx();
}
inline void insert(int &x) {
bool index;
trie_nod_idx* curr = root;
for (int i = 17; i >= 0; i--) {
index = (((1 << i) & x) != 0);
curr->c[index]++;
if (curr->t[index] == NULL) {
curr->t[index] = new trie_nod_idx();
}
curr = curr->t[index];
}
}
inline int greater(int &x) {
int cnt = 0;
bool index;
trie_nod_idx* curr = root;
for (int i = 17; i >= 0 && curr != NULL; i--) {
index = (((1 << i) & x) != 0);
if (!index)
cnt += curr->c[1];
curr = curr->t[index];
}
return cnt;
}
inline int less(int &x) {
int cnt = 0;
bool index;
trie_nod_idx* curr = root;
for (int i = 17; i >= 0 && curr != NULL; i--) {
index = (((1 << i) & x) != 0);
if (index)
cnt += curr->c[0];
curr = curr->t[index];
}
return cnt;
}
};
struct trie_nod {
trie_nod* t[2];
int c[2];
vector<int> idx;
trie_idx removed;
trie_idx added;
trie_nod() {
t[0] = t[1] = NULL;
c[0] = c[1] = 0;
}
};
struct trie {
trie_nod* root = new trie_nod();
inline int insert_f(int x, int idx) {
bool index;
int cnt = 0;
trie_nod* curr = root;
for (int i = 15; i >= 0; i--) {
index = (((1 << i) & x) != 0);
if (!index)
cnt += curr->c[1];
curr->c[index]++;
if (index) {
curr->idx.push_back(idx);
}
if (curr->t[index] == NULL) {
curr->t[index] = new trie_nod();
}
curr = curr->t[index];
}
return cnt;
}
inline int insert(int x, int idx) {
bool index;
trie_nod* curr = root;
for (int i = 15; i >= 0; i--) {
index = (((1 << i) & x) != 0);
if (index) {
curr->added.insert(idx);
}
if (curr->t[index] == NULL) {
curr->t[index] = new trie_nod();
}
curr = curr->t[index];
}
}
inline void remove(int x, int idx) {
bool index;
trie_nod* curr = root;
for (int i = 15; i >= 0; i--) {
index = (((1 << i) & x) != 0);
if (index) {
curr->removed.insert(idx);
}
curr = curr->t[index];
}
}
inline int greater_a(int x, int idx) {
int cnt = 0;
bool index;
trie_nod* curr = root;
for (int i = 15; i >= 0 && curr != NULL; i--) {
index = (((1 << i) & x) != 0);
if (!index) {
cnt += curr->idx.end() - upper_bound(curr->idx.begin(), curr->idx.end(), idx);
cnt += curr->added.greater(idx);
cnt -= curr->removed.greater(idx);
}
curr = curr->t[index];
}
return cnt;
}
inline int greater_b(int x, int idx) {
int cnt = 0;
bool index;
trie_nod* curr = root;
for (int i = 15; i >= 0 && curr != NULL; i--) {
index = (((1 << i) & x) != 0);
if (!index) {
cnt += lower_bound(curr->idx.begin(), curr->idx.end(), idx) - curr->idx.begin();
cnt += curr->added.less(idx);
cnt -= curr->removed.less(idx);
}
curr = curr->t[index];
}
return cnt;
}
inline int between_a(int x, int y, int idx) {
return greater_a(x - 1, idx) - greater_a(y, idx);
}
inline int between_b(int x, int y, int idx) {
return greater_b(x - 1, idx) - greater_b(y, idx);
}
};
int main() {
trie tr;
scanf("%d", &n);
sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += tr.insert_f(a[i], i);
}
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
x--;
if (a[x] < y) {
sum -= tr.between_b(a[x] + 1, y, x);
sum += tr.between_a(a[x], y - 1, x);
tr.remove(a[x], x);
tr.insert(y, x);
a[x] = y;
} else if (a[x] > y) {
sum += tr.between_b(y + 1, a[x], x);
sum -= tr.between_a(y, a[x] - 1, x);
tr.remove(a[x], x);
tr.insert(y, x);
a[x] = y;
}
printf("%lld\n", sum);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGFbMjUwMDA2XSwgbiwgc19zaXplLCBtOwpsb25nIGxvbmcgc3VtID0gMDsKCnN0cnVjdCB0cmllX25vZF9pZHggewogICAgdHJpZV9ub2RfaWR4KiB0WzJdOwogICAgaW50IGNbMl07CgogICAgdHJpZV9ub2RfaWR4KCkgewogICAgICAgIHRbMF0gPSB0WzFdID0gTlVMTDsKICAgICAgICBjWzBdID0gY1sxXSA9IDA7CiAgICB9Cn07CgpzdHJ1Y3QgdHJpZV9pZHggewogICAgdHJpZV9ub2RfaWR4KiByb290OwoKICAgIHRyaWVfaWR4KCkgewogICAgICAgIHJvb3QgPSBuZXcgdHJpZV9ub2RfaWR4KCk7CiAgICB9CgogICAgaW5saW5lIHZvaWQgaW5zZXJ0KGludCAmeCkgewogICAgICAgIGJvb2wgaW5kZXg7CiAgICAgICAgdHJpZV9ub2RfaWR4KiBjdXJyID0gcm9vdDsKICAgICAgICBmb3IgKGludCBpID0gMTc7IGkgPj0gMDsgaS0tKSB7CiAgICAgICAgICAgIGluZGV4ID0gKCgoMSA8PCBpKSAmIHgpICE9IDApOwogICAgICAgICAgICBjdXJyLT5jW2luZGV4XSsrOwogICAgICAgICAgICBpZiAoY3Vyci0+dFtpbmRleF0gPT0gTlVMTCkgewogICAgICAgICAgICAgICAgY3Vyci0+dFtpbmRleF0gPSBuZXcgdHJpZV9ub2RfaWR4KCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY3VyciA9IGN1cnItPnRbaW5kZXhdOwogICAgICAgIH0KICAgIH0KCiAgICBpbmxpbmUgaW50IGdyZWF0ZXIoaW50ICZ4KSB7CiAgICAgICAgaW50IGNudCA9IDA7CiAgICAgICAgYm9vbCBpbmRleDsKICAgICAgICB0cmllX25vZF9pZHgqIGN1cnIgPSByb290OwogICAgICAgIGZvciAoaW50IGkgPSAxNzsgaSA+PSAwICYmIGN1cnIgIT0gTlVMTDsgaS0tKSB7CiAgICAgICAgICAgIGluZGV4ID0gKCgoMSA8PCBpKSAmIHgpICE9IDApOwoKICAgICAgICAgICAgaWYgKCFpbmRleCkKICAgICAgICAgICAgICAgIGNudCArPSBjdXJyLT5jWzFdOwogICAgICAgICAgICBjdXJyID0gY3Vyci0+dFtpbmRleF07CiAgICAgICAgfQogICAgICAgIHJldHVybiBjbnQ7CiAgICB9CgogICAgaW5saW5lIGludCBsZXNzKGludCAmeCkgewogICAgICAgIGludCBjbnQgPSAwOwogICAgICAgIGJvb2wgaW5kZXg7CiAgICAgICAgdHJpZV9ub2RfaWR4KiBjdXJyID0gcm9vdDsKICAgICAgICBmb3IgKGludCBpID0gMTc7IGkgPj0gMCAmJiBjdXJyICE9IE5VTEw7IGktLSkgewogICAgICAgICAgICBpbmRleCA9ICgoKDEgPDwgaSkgJiB4KSAhPSAwKTsKICAgICAgICAgICAgaWYgKGluZGV4KQogICAgICAgICAgICAgICAgY250ICs9IGN1cnItPmNbMF07CiAgICAgICAgICAgIGN1cnIgPSBjdXJyLT50W2luZGV4XTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNudDsKICAgIH0KfTsKCnN0cnVjdCB0cmllX25vZCB7CiAgICB0cmllX25vZCogdFsyXTsKICAgIGludCBjWzJdOwogICAgdmVjdG9yPGludD4gaWR4OwogICAgdHJpZV9pZHggcmVtb3ZlZDsKICAgIHRyaWVfaWR4IGFkZGVkOwoKICAgIHRyaWVfbm9kKCkgewogICAgICAgIHRbMF0gPSB0WzFdID0gTlVMTDsKICAgICAgICBjWzBdID0gY1sxXSA9IDA7CiAgICB9Cn07CgpzdHJ1Y3QgdHJpZSB7CiAgICB0cmllX25vZCogcm9vdCA9IG5ldyB0cmllX25vZCgpOwoKICAgIGlubGluZSBpbnQgaW5zZXJ0X2YoaW50IHgsIGludCBpZHgpIHsKICAgICAgICBib29sIGluZGV4OwogICAgICAgIGludCBjbnQgPSAwOwogICAgICAgIHRyaWVfbm9kKiBjdXJyID0gcm9vdDsKICAgICAgICBmb3IgKGludCBpID0gMTU7IGkgPj0gMDsgaS0tKSB7CiAgICAgICAgICAgIGluZGV4ID0gKCgoMSA8PCBpKSAmIHgpICE9IDApOwogICAgICAgICAgICBpZiAoIWluZGV4KQogICAgICAgICAgICAgICAgY250ICs9IGN1cnItPmNbMV07CiAgICAgICAgICAgIGN1cnItPmNbaW5kZXhdKys7CiAgICAgICAgICAgIGlmIChpbmRleCkgewogICAgICAgICAgICAgICAgY3Vyci0+aWR4LnB1c2hfYmFjayhpZHgpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChjdXJyLT50W2luZGV4XSA9PSBOVUxMKSB7CiAgICAgICAgICAgICAgICBjdXJyLT50W2luZGV4XSA9IG5ldyB0cmllX25vZCgpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGN1cnIgPSBjdXJyLT50W2luZGV4XTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNudDsKICAgIH0KCiAgICBpbmxpbmUgaW50IGluc2VydChpbnQgeCwgaW50IGlkeCkgewogICAgICAgIGJvb2wgaW5kZXg7CiAgICAgICAgdHJpZV9ub2QqIGN1cnIgPSByb290OwogICAgICAgIGZvciAoaW50IGkgPSAxNTsgaSA+PSAwOyBpLS0pIHsKICAgICAgICAgICAgaW5kZXggPSAoKCgxIDw8IGkpICYgeCkgIT0gMCk7CiAgICAgICAgICAgIGlmIChpbmRleCkgewogICAgICAgICAgICAgICAgY3Vyci0+YWRkZWQuaW5zZXJ0KGlkeCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYgKGN1cnItPnRbaW5kZXhdID09IE5VTEwpIHsKICAgICAgICAgICAgICAgIGN1cnItPnRbaW5kZXhdID0gbmV3IHRyaWVfbm9kKCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY3VyciA9IGN1cnItPnRbaW5kZXhdOwogICAgICAgIH0KICAgIH0KCiAgICBpbmxpbmUgdm9pZCByZW1vdmUoaW50IHgsIGludCBpZHgpIHsKICAgICAgICBib29sIGluZGV4OwogICAgICAgIHRyaWVfbm9kKiBjdXJyID0gcm9vdDsKICAgICAgICBmb3IgKGludCBpID0gMTU7IGkgPj0gMDsgaS0tKSB7CiAgICAgICAgICAgIGluZGV4ID0gKCgoMSA8PCBpKSAmIHgpICE9IDApOwogICAgICAgICAgICBpZiAoaW5kZXgpIHsKICAgICAgICAgICAgICAgIGN1cnItPnJlbW92ZWQuaW5zZXJ0KGlkeCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY3VyciA9IGN1cnItPnRbaW5kZXhdOwogICAgICAgIH0KICAgIH0KCiAgICBpbmxpbmUgaW50IGdyZWF0ZXJfYShpbnQgeCwgaW50IGlkeCkgewogICAgICAgIGludCBjbnQgPSAwOwogICAgICAgIGJvb2wgaW5kZXg7CiAgICAgICAgdHJpZV9ub2QqIGN1cnIgPSByb290OwogICAgICAgIGZvciAoaW50IGkgPSAxNTsgaSA+PSAwICYmIGN1cnIgIT0gTlVMTDsgaS0tKSB7CiAgICAgICAgICAgIGluZGV4ID0gKCgoMSA8PCBpKSAmIHgpICE9IDApOwogICAgICAgICAgICBpZiAoIWluZGV4KSB7CiAgICAgICAgICAgICAgICBjbnQgKz0gY3Vyci0+aWR4LmVuZCgpIC0gdXBwZXJfYm91bmQoY3Vyci0+aWR4LmJlZ2luKCksIGN1cnItPmlkeC5lbmQoKSwgaWR4KTsKICAgICAgICAgICAgICAgIGNudCArPSBjdXJyLT5hZGRlZC5ncmVhdGVyKGlkeCk7CiAgICAgICAgICAgICAgICBjbnQgLT0gY3Vyci0+cmVtb3ZlZC5ncmVhdGVyKGlkeCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY3VyciA9IGN1cnItPnRbaW5kZXhdOwogICAgICAgIH0KICAgICAgICByZXR1cm4gY250OwogICAgfQoKICAgIGlubGluZSBpbnQgZ3JlYXRlcl9iKGludCB4LCBpbnQgaWR4KSB7CiAgICAgICAgaW50IGNudCA9IDA7CiAgICAgICAgYm9vbCBpbmRleDsKICAgICAgICB0cmllX25vZCogY3VyciA9IHJvb3Q7CiAgICAgICAgZm9yIChpbnQgaSA9IDE1OyBpID49IDAgJiYgY3VyciAhPSBOVUxMOyBpLS0pIHsKICAgICAgICAgICAgaW5kZXggPSAoKCgxIDw8IGkpICYgeCkgIT0gMCk7CiAgICAgICAgICAgIGlmICghaW5kZXgpIHsKICAgICAgICAgICAgICAgIGNudCArPSBsb3dlcl9ib3VuZChjdXJyLT5pZHguYmVnaW4oKSwgY3Vyci0+aWR4LmVuZCgpLCBpZHgpIC0gY3Vyci0+aWR4LmJlZ2luKCk7CiAgICAgICAgICAgICAgICBjbnQgKz0gY3Vyci0+YWRkZWQubGVzcyhpZHgpOwogICAgICAgICAgICAgICAgY250IC09IGN1cnItPnJlbW92ZWQubGVzcyhpZHgpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGN1cnIgPSBjdXJyLT50W2luZGV4XTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNudDsKICAgIH0KCiAgICBpbmxpbmUgaW50IGJldHdlZW5fYShpbnQgeCwgaW50IHksIGludCBpZHgpIHsKICAgICAgICByZXR1cm4gZ3JlYXRlcl9hKHggLSAxLCBpZHgpIC0gZ3JlYXRlcl9hKHksIGlkeCk7CiAgICB9CgogICAgaW5saW5lIGludCBiZXR3ZWVuX2IoaW50IHgsIGludCB5LCBpbnQgaWR4KSB7CiAgICAgICAgcmV0dXJuIGdyZWF0ZXJfYih4IC0gMSwgaWR4KSAtIGdyZWF0ZXJfYih5LCBpZHgpOwogICAgfQp9OwoKaW50IG1haW4oKSB7CiAgICB0cmllIHRyOwogICAgc2NhbmYoIiVkIiwgJm4pOwogICAgc3VtID0gMDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgc2NhbmYoIiVkIiwgJmFbaV0pOwogICAgICAgIHN1bSArPSB0ci5pbnNlcnRfZihhW2ldLCBpKTsKICAgIH0KICAgIHNjYW5mKCIlZCIsICZtKTsKICAgIHdoaWxlIChtLS0pIHsKICAgICAgICBpbnQgeCwgeTsKICAgICAgICBzY2FuZigiJWQlZCIsICZ4LCAmeSk7CiAgICAgICAgeC0tOwogICAgICAgIGlmIChhW3hdIDwgeSkgewogICAgICAgICAgICBzdW0gLT0gdHIuYmV0d2Vlbl9iKGFbeF0gKyAxLCB5LCB4KTsKICAgICAgICAgICAgc3VtICs9IHRyLmJldHdlZW5fYShhW3hdLCB5IC0gMSwgeCk7CiAgICAgICAgICAgIHRyLnJlbW92ZShhW3hdLCB4KTsKICAgICAgICAgICAgdHIuaW5zZXJ0KHksIHgpOwogICAgICAgICAgICBhW3hdID0geTsKICAgICAgICB9IGVsc2UgaWYgKGFbeF0gPiB5KSB7CiAgICAgICAgICAgIHN1bSArPSB0ci5iZXR3ZWVuX2IoeSArIDEsIGFbeF0sIHgpOwogICAgICAgICAgICBzdW0gLT0gdHIuYmV0d2Vlbl9hKHksIGFbeF0gLSAxLCB4KTsKICAgICAgICAgICAgdHIucmVtb3ZlKGFbeF0sIHgpOwogICAgICAgICAgICB0ci5pbnNlcnQoeSwgeCk7CiAgICAgICAgICAgIGFbeF0gPSB5OwogICAgICAgIH0KICAgICAgICBwcmludGYoIiVsbGRcbiIsIHN1bSk7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==