#include <bits/stdc++.h>
using namespace std;
int a[250006], n, s_size,m;
pair<long long, long long> pr;
long long sum = 0;
vector<int> sq[506];
inline void init() {
s_size = ceil(sqrt(n));
sq[0].push_back(a[0]);
for (int i = 1; i < n; i++) {
sq[i / s_size].push_back(a[i]);
}
for (int i = 0; i < s_size; i++) {
sort(sq[i].begin(), sq[i].end());
}
}
inline void update(int index, int val) {
int sindex = index / s_size;
vector<int>& v = sq[sindex];
vector<int>::iterator it = lower_bound(v.begin(), v.end(), a[index]);
v.erase(it);
it = upper_bound(v.begin(), v.end(), val);
v.insert(it, val);
a[index] = val;
}
inline int between(vector<int>& v, int s, int e) {
return upper_bound(v.begin(), v.end(), e) - lower_bound(v.begin(), v.end(), s);
}
inline void between(int index, int s, int e) {
pr.first = 0;
pr.second = 0;
int ee = ((s_size + index) / s_size) * s_size;
int ss = (index / s_size) * s_size;
for (int i = ss; i < index; i++) {
if (a[i] > s && a[i] <= e)
pr.first++;
}
for (int i = index + 1; i < ee; i++) {
if (a[i] >= s && a[i] < e)
pr.second++;
}
ss = (index / s_size);
for (int i = 0; i < ss; i++) {
pr.first += between(sq[i], s + 1, e);
}
for (int i = ss + 1; i < s_size; i++) {
pr.second += between(sq[i], s, e - 1);
}
}
struct trie_nod {
trie_nod* t[2];
long long c[2];
trie_nod() {
t[0] = t[1] = NULL;
c[0] = c[1] = 0;
}
};
struct trie {
trie_nod* root = new trie_nod();
int insert(int x) {
bool index;
long long cnt = 0;
trie_nod* curr = root;
for (int i = 16; i >= 0; i--) {
index = (((1 << i) & x) != 0);
if(!index)
cnt += curr->c[1];
curr->c[index]++;
if (curr->t[index] == NULL)
curr->t[index] = new trie_nod();
curr = curr->t[index];
}
return cnt;
}
};
int main() {
trie tr;
scanf("%d", &n);
sum = 0;
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += tr.insert(a[i]);
}
init();
scanf("%d", &m);
while (m--) {
int x, y;
scanf("%d%d", &x, &y);
x--;
if (a[x] < y) {
between(x, a[x], y);
sum -= pr.first;
sum += pr.second;
update(x, y);
} else if (a[x] > y) {
between(x, y, a[x]);
sum += pr.first;
sum -= pr.second;
update(x, y);
}
printf("%lld\n", sum);
}
return 0;
}
ICAgICNpbmNsdWRlIDxiaXRzL3N0ZGMrKy5oPgogICAgIAogICAgdXNpbmcgbmFtZXNwYWNlIHN0ZDsKICAgICAKICAgIGludCBhWzI1MDAwNl0sIG4sIHNfc2l6ZSxtOwogICAgcGFpcjxsb25nIGxvbmcsIGxvbmcgbG9uZz4gcHI7CiAgICBsb25nIGxvbmcgc3VtID0gMDsgCiAgICB2ZWN0b3I8aW50PiBzcVs1MDZdOwogICAgIAogICAgaW5saW5lIHZvaWQgaW5pdCgpIHsKICAgICAgICBzX3NpemUgPSBjZWlsKHNxcnQobikpOwogICAgICAgIHNxWzBdLnB1c2hfYmFjayhhWzBdKTsKICAgICAgICBmb3IgKGludCBpID0gMTsgaSA8IG47IGkrKykgewogICAgICAgICAgICBzcVtpIC8gc19zaXplXS5wdXNoX2JhY2soYVtpXSk7CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgc19zaXplOyBpKyspIHsKICAgICAgICAgICAgc29ydChzcVtpXS5iZWdpbigpLCBzcVtpXS5lbmQoKSk7CiAgICAgICAgfQogICAgfQogICAgIAogICAgaW5saW5lIHZvaWQgdXBkYXRlKGludCBpbmRleCwgaW50IHZhbCkgewogICAgICAgIGludCBzaW5kZXggPSBpbmRleCAvIHNfc2l6ZTsKICAgICAgICB2ZWN0b3I8aW50PiYgdiA9IHNxW3NpbmRleF07CiAgICAgICAgdmVjdG9yPGludD46Oml0ZXJhdG9yIGl0ID0gbG93ZXJfYm91bmQodi5iZWdpbigpLCB2LmVuZCgpLCBhW2luZGV4XSk7CiAgICAgICAgdi5lcmFzZShpdCk7CiAgICAgICAgaXQgPSB1cHBlcl9ib3VuZCh2LmJlZ2luKCksIHYuZW5kKCksIHZhbCk7CiAgICAgICAgdi5pbnNlcnQoaXQsIHZhbCk7CiAgICAgICAgYVtpbmRleF0gPSB2YWw7CiAgICB9CiAgICAgCiAgICBpbmxpbmUgaW50IGJldHdlZW4odmVjdG9yPGludD4mIHYsIGludCBzLCBpbnQgZSkgewogICAgICAgIHJldHVybiB1cHBlcl9ib3VuZCh2LmJlZ2luKCksIHYuZW5kKCksIGUpIC0gbG93ZXJfYm91bmQodi5iZWdpbigpLCB2LmVuZCgpLCBzKTsKICAgIH0KICAgICAKICAgIGlubGluZSB2b2lkIGJldHdlZW4oaW50IGluZGV4LCBpbnQgcywgaW50IGUpIHsKICAgICAgICBwci5maXJzdCA9IDA7CiAgICAgICAgcHIuc2Vjb25kID0gMDsKICAgICAgICBpbnQgZWUgPSAoKHNfc2l6ZSArIGluZGV4KSAvIHNfc2l6ZSkgKiBzX3NpemU7CiAgICAgICAgaW50IHNzID0gKGluZGV4IC8gc19zaXplKSAqIHNfc2l6ZTsKICAgICAgICBmb3IgKGludCBpID0gc3M7IGkgPCBpbmRleDsgaSsrKSB7CiAgICAgICAgICAgIGlmIChhW2ldID4gcyAmJiBhW2ldIDw9IGUpCiAgICAgICAgICAgICAgICBwci5maXJzdCsrOwogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gaW5kZXggKyAxOyBpIDwgZWU7IGkrKykgewogICAgICAgICAgICBpZiAoYVtpXSA+PSBzICYmIGFbaV0gPCBlKQogICAgICAgICAgICAgICAgcHIuc2Vjb25kKys7CiAgICAgICAgfQogICAgICAgIHNzID0gKGluZGV4IC8gc19zaXplKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHNzOyBpKyspIHsKICAgICAgICAgICAgcHIuZmlyc3QgKz0gYmV0d2VlbihzcVtpXSwgcyArIDEsIGUpOwogICAgICAgIH0KICAgICAgICBmb3IgKGludCBpID0gc3MgKyAxOyBpIDwgc19zaXplOyBpKyspIHsKICAgICAgICAgICAgcHIuc2Vjb25kICs9IGJldHdlZW4oc3FbaV0sIHMsIGUgLSAxKTsKICAgICAgICB9CiAgICB9CiAgICAgCiAgICBzdHJ1Y3QgdHJpZV9ub2QgewogICAgICAgIHRyaWVfbm9kKiB0WzJdOwogICAgICAgIGxvbmcgbG9uZyBjWzJdOwogICAgIAogICAgICAgIHRyaWVfbm9kKCkgewogICAgICAgICAgICB0WzBdID0gdFsxXSA9IE5VTEw7CiAgICAgICAgICAgIGNbMF0gPSBjWzFdID0gMDsKICAgICAgICB9CiAgICB9OwogICAgIAogICAgc3RydWN0IHRyaWUgewogICAgICAgIHRyaWVfbm9kKiByb290ID0gbmV3IHRyaWVfbm9kKCk7CiAgICAgCiAgICAgICAgaW50IGluc2VydChpbnQgeCkgewogICAgICAgICAgICBib29sIGluZGV4OwogICAgICAgICAgICBsb25nIGxvbmcgY250ID0gMDsKICAgICAgICAgICAgdHJpZV9ub2QqIGN1cnIgPSByb290OwogICAgICAgICAgICBmb3IgKGludCBpID0gMTY7IGkgPj0gMDsgaS0tKSB7CiAgICAgICAgICAgICAgICBpbmRleCA9ICgoKDEgPDwgaSkgJiB4KSAhPSAwKTsKICAgICAgICAgICAgICAgIGlmKCFpbmRleCkKICAgICAgICAgICAgICAgICAgICBjbnQgKz0gY3Vyci0+Y1sxXTsKICAgICAgICAgICAgICAgIGN1cnItPmNbaW5kZXhdKys7CiAgICAgICAgICAgICAgICBpZiAoY3Vyci0+dFtpbmRleF0gPT0gTlVMTCkKICAgICAgICAgICAgICAgICAgICBjdXJyLT50W2luZGV4XSA9IG5ldyB0cmllX25vZCgpOwogICAgICAgICAgICAgICAgY3VyciA9IGN1cnItPnRbaW5kZXhdOwogICAgICAgICAgICB9ICAgCiAgICAgICAgICAgIHJldHVybiBjbnQ7CiAgICAgICAgfQogICAgfTsKICAgICAKICAgIGludCBtYWluKCkgewogICAgICAgIHRyaWUgdHI7CiAgICAgICAgc2NhbmYoIiVkIiwgJm4pOwogICAgICAgIHN1bSA9IDA7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgc2NhbmYoIiVkIiwgJmFbaV0pOwogICAgICAgICAgICBzdW0gKz0gdHIuaW5zZXJ0KGFbaV0pOwogICAgICAgIH0KICAgICAgICBpbml0KCk7CiAgICAgICAgc2NhbmYoIiVkIiwgJm0pOwogICAgICAgIHdoaWxlIChtLS0pIHsKICAgICAgICAgICAgaW50IHgsIHk7CiAgICAgICAgICAgIHNjYW5mKCIlZCVkIiwgJngsICZ5KTsKICAgICAgICAgICAgeC0tOwogICAgICAgICAgICBpZiAoYVt4XSA8IHkpIHsKICAgICAgICAgICAgICAgIGJldHdlZW4oeCwgYVt4XSwgeSk7CiAgICAgICAgICAgICAgICBzdW0gLT0gcHIuZmlyc3Q7CiAgICAgICAgICAgICAgICBzdW0gKz0gcHIuc2Vjb25kOwogICAgICAgICAgICAgICAgdXBkYXRlKHgsIHkpOwogICAgICAgICAgICB9IGVsc2UgaWYgKGFbeF0gPiB5KSB7CiAgICAgICAgICAgICAgICBiZXR3ZWVuKHgsIHksIGFbeF0pOwogICAgICAgICAgICAgICAgc3VtICs9IHByLmZpcnN0OwogICAgICAgICAgICAgICAgc3VtIC09IHByLnNlY29uZDsKICAgICAgICAgICAgICAgIHVwZGF0ZSh4LCB5KTsKICAgICAgICAgICAgfQogICAgICAgICAgICBwcmludGYoIiVsbGRcbiIsIHN1bSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiAwOwogICAgfSA=