#include "bits/stdc++.h"
using namespace std;
inline int scan(){
int x = 0;
char c = getchar_unlocked();
while(c < '0' || c > '9'){
c = getchar_unlocked();
}
while(c >= '0' && c <= '9'){
x = (x << 3) + (x << 1) + c - '0';
c = getchar_unlocked();
}
return x;
}
const int N = 2.5e5 + 5;
const int M = 5e4 + 5;
const int LN = 15;
int n;
struct snode{
snode* left;
snode* right;
int cnt;
snode(){
left = NULL;
right = NULL;
cnt = 0;
}
};
struct segtree{
snode* root;
segtree(){
root = new snode();
}
int cnt(snode* tree){
if(tree){
return tree -> cnt;
}
return 0;
}
void insert(snode* &tree , int l , int r , int val){
if(!tree){
tree = new snode();
}
if(l == r){
tree -> cnt = 1;
}
else{
int mid = l + r >> 1;
if(mid >= val){
insert(tree -> left , l , mid , val);
}
else{
insert(tree -> right , mid + 1 , r , val);
}
tree -> cnt = cnt(tree -> left) + cnt(tree -> right);
}
}
void erase(snode* &tree , int l , int r , int val){
assert(tree);
if(l == r){
tree -> cnt = 0;
}
else{
int mid = l + r >> 1;
if(mid >= val){
erase(tree -> left , l , mid , val);
}
else{
erase(tree -> right , mid + 1 , r , val);
}
tree -> cnt = cnt(tree -> left) + cnt(tree -> right);
}
}
void insert(int key){
insert(root , 1 , n , key);
}
void erase(int key){
erase(root , 1 , n , key);
}
int query(snode* tree , int l , int r , int ql , int qr){
if(l > qr || r < ql || !tree){
return 0;
}
if(l >= ql && r <= qr){
return cnt(tree);
}
int mid = l + r >> 1;
return query(tree -> left , l , mid , ql , qr) + query(tree -> right , mid + 1 , r , ql , qr);
}
int range(int l , int r){
return query(root , 1 , n , l , r);
}
};
int arr[N];
int q;
int x , y;
long long inv = 0;
struct node{
segtree indices;
int left;
int right;
node(){
left = -1;
right = -1;
}
};
struct TRIE{
node nodes[(N + 10001) * LN];
int cur;
TRIE(){
cur = 0;
}
void insert(int num ,int index){
int nd = 0;
for(int i = LN ; i >= 0 ; --i){
bool b = (num >> i) & 1;
if(b){
if(nodes[nd].right == -1){
nodes[nd].right = ++cur;
}
nd = nodes[nd].right;
nodes[nd].indices.insert(index);
}
else{
if(nodes[nd].left == -1){
nodes[nd].left = ++cur;
}
nd = nodes[nd].left;
nodes[nd].indices.insert(index);
}
}
}
void erase(int num , int index){
int nd = 0;
for(int i = LN ; i >= 0 ; --i){
bool b = (num >> i) & 1;
if(b){
nd = nodes[nd].right;
}
else{
nd = nodes[nd].left;
}
nodes[nd].indices.erase(index);
}
}
void update(int index , int value){
erase(arr[index] , index);
arr[index] = value;
insert(arr[index] , index);
}
int get(int l , int r , int k){
++k;
int ret = 0;
int nd = 0;
for(int i = LN ; i >= 0 && nd >= 0; --i){
bool b = (k >> i) & 1;
if(b){
int z = 0;
if(nodes[nd].left != -1){
z = nodes[nodes[nd].left].indices.range(l , r);
}
ret += z;
nd = nodes[nd].right;
}
else{
nd = nodes[nd].left;
}
}
return ret;
}
};
TRIE trie;
int main(){
n = scan();
for(int i = 1 ; i <= n ; ++i){
//arr[i] = rand() % 50000;
//++arr[i];
arr[i] = scan();
trie.insert(arr[i] , i);
}
for(int i = n ; i >= 1 ; --i){
inv += trie.get(i + 1 , n , arr[i] - 1);
}
q = scan();
while(q--){
//x = rand() % n;
//++x;
//y = rand() % 50000;
//++y;
x = scan() , y = scan();
inv -= x - 1 - trie.get(1 , x - 1 , arr[x]);
inv -= trie.get(x + 1 , n , arr[x] - 1);
trie.update(x , y);
inv += x - 1 - trie.get(1 , x - 1 , arr[x]);
inv += trie.get(x + 1 , n , arr[x] - 1);
printf("%lld\n" , inv);
}
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmlubGluZSBpbnQgc2NhbigpewoJaW50IHggPSAwOwoJY2hhciBjID0gZ2V0Y2hhcl91bmxvY2tlZCgpOwoJd2hpbGUoYyA8ICcwJyB8fCBjID4gJzknKXsKCQljID0gZ2V0Y2hhcl91bmxvY2tlZCgpOwoJfQoJd2hpbGUoYyA+PSAnMCcgJiYgYyA8PSA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OwogICAgaW50IGxlZnQ7CiAgICBpbnQgcmlnaHQ7CiAgICBub2RlKCl7CiAgICAgICAgbGVmdCA9IC0xOwogICAgICAgIHJpZ2h0ID0gLTE7CiAgICB9Cn07CnN0cnVjdCBUUklFewoJbm9kZSBub2Rlc1soTiArIDEwMDAxKSAqIExOXTsKICAgIGludCBjdXI7CiAgICBUUklFKCl7CiAgICAgICAgY3VyID0gMDsKICAgIH0KICAgIHZvaWQgaW5zZXJ0KGludCBudW0gLGludCBpbmRleCl7CiAgICAJaW50IG5kID0gMDsKICAgICAgICBmb3IoaW50IGkgPSBMTiA7IGkgPj0gMCA7IC0taSl7CiAgICAgICAgICAgIGJvb2wgYiA9IChudW0gPj4gaSkgJiAxOwogICAgICAgICAgICBpZihiKXsKICAgICAgICAgICAgICAgIGlmKG5vZGVzW25kXS5yaWdodCA9PSAtMSl7CiAgICAgICAgICAgICAgICAgICAgbm9kZXNbbmRdLnJpZ2h0ID0gKytjdXI7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBuZCA9IG5vZGVzW25kXS5yaWdodDsKICAgICAgICAgICAgICAgIG5vZGVzW25kXS5pbmRpY2VzLmluc2VydChpbmRleCk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZXsKICAgICAgICAgICAgICAgIGlmKG5vZGVzW25kXS5sZWZ0ID09IC0xKXsKICAgICAgICAgICAgICAgICAgICBub2Rlc1tuZF0ubGVmdCA9ICsrY3VyOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgbmQgPSBub2Rlc1tuZF0ubGVmdDsKICAgICAgICAgICAgICAgIG5vZGVzW25kXS5pbmRpY2VzLmluc2VydChpbmRleCk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICB2b2lkIGVyYXNlKGludCBudW0gLCBpbnQgaW5kZXgpewogICAgICAgIGludCBuZCA9IDA7CiAgICAgICAgZm9yKGludCBpID0gTE4gOyBpID49IDAgOyAtLWkpewogICAgICAgICAgICBib29sIGIgPSAobnVtID4+IGkpICYgMTsKICAgICAgICAgICAgaWYoYil7CiAgICAgICAgICAgICAgICBuZCA9IG5vZGVzW25kXS5yaWdodDsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlewogICAgICAgICAgICAgICAgbmQgPSBub2Rlc1tuZF0ubGVmdDsKICAgICAgICAgICAgfQogICAgICAgICAgICBub2Rlc1tuZF0uaW5kaWNlcy5lcmFzZShpbmRleCk7CiAgICAgICAgfQogICAgfQogICAgdm9pZCB1cGRhdGUoaW50IGluZGV4ICwgaW50IHZhbHVlKXsKICAgIAllcmFzZShhcnJbaW5kZXhdICwgaW5kZXgpOwogICAgCWFycltpbmRleF0gPSB2YWx1ZTsKICAgIAlpbnNlcnQoYXJyW2luZGV4XSAsIGluZGV4KTsKICAgIH0KICAgIGludCBnZXQoaW50IGwgLCBpbnQgciAsIGludCBrKXsKICAgICAgICArK2s7CiAgICAgICAgaW50IHJldCA9IDA7CiAgICAgICAgaW50IG5kID0gMDsKICAgICAgICBmb3IoaW50IGkgPSBMTiA7IGkgPj0gMCAmJiBuZCA+PSAwOyAtLWkpewogICAgICAgICAgICBib29sIGIgPSAoayA+PiBpKSAmIDE7CiAgICAgICAgICAgIGlmKGIpewogICAgICAgICAgICAgICAgaW50IHogPSAwOwogICAgICAgICAgICAgICAgaWYobm9kZXNbbmRdLmxlZnQgIT0gLTEpewogICAgICAgICAgICAgICAgCXogPSBub2Rlc1tub2Rlc1tuZF0ubGVmdF0uaW5kaWNlcy5yYW5nZShsICwgcik7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICByZXQgKz0gejsKICAgICAgICAgICAgICAgIG5kID0gbm9kZXNbbmRdLnJpZ2h0OwogICAgICAgICAgICB9CiAgICAgICAgICAgIGVsc2V7CiAgICAgICAgICAgICAgICBuZCA9IG5vZGVzW25kXS5sZWZ0OwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIHJldHVybiByZXQ7CiAgICB9Cn07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