#include "bits/stdc++.h"
using namespace std;
const int N = 2.5e5 + 5;
const int M = 5e4 + 5;
const int SQN = 505;
int n;
int arr[N];
int q;
int x , y;
int start[SQN];
int finish[SQN];
int block[N];
int bit[SQN][M] = {0};
void update(int i , int idx , int val){
while(idx < M){
bit[i][idx] += val;
idx += idx & -idx;
}
}
int sum(int i , int idx){
int res = 0;
while(idx){
res += bit[i][idx];
idx -= idx & -idx;
}
return res;
}
long long inv = 0;
int get(int l , int r , int x){
int res = 0;
for(int i = block[l] ; i <= block[r] ; ++i){
if(start[i] >= l && finish[i] <= r){
res += sum(i , x);
}
else{
for(int j = max(start[i] , l) ; j <= min(finish[i] , r) ; ++j){
res += (arr[j] <= x);
}
}
}
return res;
}
int main(){
scanf("%d" , &n);
for(int i = 1 ; i <= n ; ++i){
scanf("%d" , arr + i);
}
int i = 1;
int cur = 0;
while(i <= n){
int j = i;
start[++cur] = i;
while(j <= n && j < i + SQN){
block[j] = cur;
update(cur , arr[j] , 1);
++j;
}
finish[cur] = j - 1;
i = j;
}
for(int i = n ; i >= 1 ; --i){
inv += get(i + 1 , n , arr[i] - 1);
}
scanf("%d" , &q);
while(q--){
scanf("%d %d" , &x , &y);
inv -= x - 1 - get(1 , x - 1 , arr[x]);
inv -= get(x + 1 , n , arr[x] - 1);
update(block[x] , y , 1);
update(block[x] , arr[x] , -1);
arr[x] = y;
inv += x - 1 - get(1 , x - 1 , arr[x]);
inv += get(x + 1 , n , arr[x] - 1);
printf("%lld\n" , inv);
}
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBOID0gMi41ZTUgKyA1Owpjb25zdCBpbnQgTSA9IDVlNCArIDU7CmNvbnN0IGludCBTUU4gPSA1MDU7CmludCBuOwppbnQgYXJyW05dOwppbnQgcTsKaW50IHggLCB5OwppbnQgc3RhcnRbU1FOXTsKaW50IGZpbmlzaFtTUU5dOwppbnQgYmxvY2tbTl07CmludCBi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