#include<bits/stdc++.h>
using namespace std;
#define gx getchar_unlocked
#define px putchar_unlocked
inline void iscan( int & n )
{
register int sign = 1 ; n = 0 ;
register char c = gx( ) ;
while ( c < '0' || c > '9' ) {
if ( c == '-' ) sign = - 1 ;
c = gx( ) ;
}
while ( c >= '0' && c <= '9' )
n = ( n<< 3 ) + ( n<< 1 ) + c - '0' , c = gx( ) ;
n = n * sign;
}
inline void lprint( ULL n )
{
if ( n< 0 ) {
n= - n;
px( '-' ) ;
}
register int i= 64 ;
register char o[ 64 ] ;
do {
o[ -- i] = ( n% 10 ) + '0' ;
n/ = 10 ;
} while ( n) ;
do {
px( o[ i] ) ;
} while ( ++ i< 64 ) ;
}
int BIT[ 501 ] [ 50001 ] ;
long long inv[ 50001 ] ;
int A[ 250001 ] ;
int sq,N;
inline void update( int idx)
{
for ( ; idx<= 50000 ; )
{
++ inv[ idx] ;
idx+ = idx& - idx;
}
return ;
}
inline long long Get( int idx)
{
long long sum= 0 ;
for ( ; idx> 0 ; )
{
sum+ = inv[ idx] ;
idx- = idx& - idx;
}
return sum;
}
inline void update( int number,int idx,int value)
{
for ( ; idx<= 50000 ; )
{
BIT[ number] [ idx] + = value;
idx+ = idx& - idx;
}
return ;
}
inline int Get( int idx , int number)
{
int sum= 0 ;
for ( ; idx> 0 ; )
{
sum+ = BIT[ number] [ idx] ;
idx- = idx& - idx;
}
return sum;
}
inline long long changeL( int L, int value)
{
long long change= 0 ;
const int ped= L/ sq+ 1 ;
for ( int tree= 1 ; tree<= ( ( N- 1 ) / sq+ 1 ) ; ++ tree)
if ( tree== ped)
continue ;
else if ( tree< ped)
change= change+ ( Get( 50000 ,tree) - Get( value,tree) ) - ( Get( 50000 ,tree) - Get( A[ L] ,tree) ) ;
else
change= change+ Get( value- 1 ,tree) - Get( A[ L] - 1 ,tree) ;
update( ped,A[ L] ,- 1 ) ;
update( ped,value,+ 1 ) ;
for ( int i= ( ped- 1 ) * sq; i< L; ++ i)
{
if ( A[ i] > A[ L] )
-- change;
if ( value< A[ i] )
++ change;
}
for ( int i= L+ 1 ; i% sq! = 0 ; ++ i)
{
if ( A[ i] < A[ L] )
-- change;
if ( value> A[ i] )
++ change;
}
return change;
}
int main( )
{
int i,M,L,R;
iscan( N) ;
sq= sqrt ( N) ;
long long ans= 0 ;
int tree_num= 0 ;
for ( i= 0 ; i< N; ++ i)
{
if ( i% sq== 0 )
tree_num++ ;
iscan( A[ i] ) ;
update( tree_num,A[ i] ,+ 1 ) ;
}
for ( i= N- 1 ; i>= 0 ; -- i)
{
ans+ = Get( A[ i] - 1 ) ;
update( A[ i] ) ;
}
iscan( M) ;
for ( i= 0 ; i< M; ++ i)
{
iscan( L) ; iscan( R) ;
ans+ = changeL( L- 1 ,R) ;
A[ L- 1 ] = R;
lprint( ans) ;
px( '\n ' ) ;
}
return 0 ;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBneCBnZXRjaGFyX3VubG9ja2VkCiNkZWZpbmUgcHggcHV0Y2hhcl91bmxvY2tlZAoKaW5saW5lIHZvaWQgaXNjYW4oIGludCAmbiApCnsKICAgIHJlZ2lzdGVyIGludCBzaWduID0gMTsgbiA9IDA7CiAgICByZWdpc3RlciBjaGFyIGMgPSBneCgpOwogICAgd2hpbGUoIGMgPCAnMCcgfHwgYyA+ICc5JyApIHsKICAgICAgICBpZiggYyA9PSAnLScgKSBzaWduID0gLTE7CiAgICAgICAgYyA9IGd4KCk7CiAgICB9CiAgICB3aGlsZSggYyA+PSAnMCcgJiYgYyA8PSAnOScgKQogICAgICAgIG4gPSAobjw8MykgKyAobjw8MSkgKyBjIC0gJzAnLCBjID0gZ3goKTsKICAgIG4gPSBuICogc2lnbjsKfQoKaW5saW5lIHZvaWQgbHByaW50KCBVTEwgbiApCnsKICAgIGlmKG48MCkgewogICAgICAgIG49LW47CiAgICAgICAgcHgoJy0nKTsKICAgIH0KICAgIHJlZ2lzdGVyIGludCBpPTY0OwogICAgcmVnaXN0ZXIgY2hhciBvWzY0XTsKICAgIGRvIHsKICAgICAgICBvWy0taV0gPSAobiUxMCkgKyAnMCc7CiAgICAgICAgbi89MTA7CiAgICB9d2hpbGUobik7CiAgICBkbyB7CiAgICAgICAgcHgob1tpXSk7CiAgICB9d2hpbGUoKytpPDY0KTsKfQoKaW50IEJJVFs1MDFdWzUwMDAxXTsKCmxvbmcgbG9uZyBpbnZbNTAwMDFdOwoKaW50IEFbMjUwMDAxXTsKCmludCBzcSxOOwoKaW5saW5lIHZvaWQgdXBkYXRlKGludCBpZHgpCnsKICAgIGZvcig7aWR4PD01MDAwMDspCiAgICB7CiAgICAgICAgKytpbnZbaWR4XTsKICAgICAgICBpZHgrPWlkeCYtaWR4OwogICAgfQogICAgcmV0dXJuIDsKfQoKaW5saW5lIGxvbmcgbG9uZyBHZXQoaW50IGlkeCkKewogICAgbG9uZyBsb25nIHN1bT0wOwogICAgZm9yKDtpZHg+MDspCiAgICB7CiAgICAgICAgc3VtKz1pbnZbaWR4XTsKICAgICAgICBpZHgtPWlkeCYtaWR4OwogICAgfQogICAgcmV0dXJuIHN1bTsKfQoKaW5saW5lIHZvaWQgdXBkYXRlKGludCBudW1iZXIsaW50IGlkeCxpbnQgdmFsdWUpCnsKICAgIGZvcig7aWR4PD01MDAwMDspCiAgICB7CiAgICAgICAgQklUW251bWJlcl1baWR4XSs9dmFsdWU7CiAgICAgICAgaWR4Kz1pZHgmLWlkeDsKICAgIH0KICAgIHJldHVybiA7Cn0KaW5saW5lIGludCBHZXQoaW50IGlkeCAsIGludCBudW1iZXIpCnsKICAgIGludCBzdW09MDsKICAgIGZvcig7aWR4PjA7KQogICAgewogICAgICAgIHN1bSs9QklUW251bWJlcl1baWR4XTsKICAgICAgICBpZHgtPWlkeCYtaWR4OwogICAgfQogICAgcmV0dXJuIHN1bTsKfQoKaW5saW5lIGxvbmcgbG9uZyBjaGFuZ2VMKGludCBMLCBpbnQgdmFsdWUpCnsKICAgICAgICBsb25nIGxvbmcgY2hhbmdlPTA7CiAgICAgICAgY29uc3QgaW50IHBlZD1ML3NxKzE7CiAgICAgICAgZm9yKGludCB0cmVlPTE7dHJlZTw9KChOLTEpL3NxKzEpOysrdHJlZSkKICAgICAgICAgICAgaWYodHJlZT09cGVkKQogICAgICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgICAgIGVsc2UgaWYgKHRyZWU8cGVkKQogICAgICAgICAgICAgICAgICAgIGNoYW5nZT1jaGFuZ2UrKEdldCg1MDAwMCx0cmVlKS1HZXQodmFsdWUsdHJlZSkpLShHZXQoNTAwMDAsdHJlZSktR2V0KEFbTF0sdHJlZSkpOwogICAgICAgICAgICBlbHNlCiAgICAgICAgICAgICAgICAgICAgY2hhbmdlPWNoYW5nZStHZXQodmFsdWUtMSx0cmVlKS1HZXQoQVtMXS0xLHRyZWUpOwoKICAgICAgICB1cGRhdGUocGVkLEFbTF0sLTEpOwogICAgICAgIHVwZGF0ZShwZWQsdmFsdWUsKzEpOwogICAgICAgIGZvcihpbnQgaT0ocGVkLTEpKnNxO2k8TDsrK2kpCiAgICAgICAgewogICAgICAgICAgICBpZihBW2ldPkFbTF0pCiAgICAgICAgICAgICAgICAtLWNoYW5nZTsKICAgICAgICAgICAgaWYodmFsdWU8QVtpXSkKICAgICAgICAgICAgICAgICsrY2hhbmdlOwogICAgICAgIH0KICAgICAgICBmb3IoaW50IGk9TCsxO2klc3EhPTA7KytpKQogICAgICAgIHsKICAgICAgICAgICAgaWYoQVtpXTxBW0xdKQogICAgICAgICAgICAgICAgLS1jaGFuZ2U7CiAgICAgICAgICAgIGlmKHZhbHVlPkFbaV0pCiAgICAgICAgICAgICAgICArK2NoYW5nZTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNoYW5nZTsKfQppbnQgbWFpbigpCnsKICAgIGludCBpLE0sTCxSOwogICAgaXNjYW4oTik7CiAgICBzcT1zcXJ0KE4pOwogICAgbG9uZyBsb25nIGFucz0wOwogICAgaW50IHRyZWVfbnVtPTA7CiAgICBmb3IoaT0wO2k8TjsrK2kpCiAgICAgICAgewogICAgICAgICAgICBpZihpJXNxPT0wKQogICAgICAgICAgICAgICAgdHJlZV9udW0rKzsKICAgICAgICAgICAgaXNjYW4oQVtpXSk7CiAgICAgICAgICAgIHVwZGF0ZSh0cmVlX251bSxBW2ldLCsxKTsKICAgICAgICB9CiAgICBmb3IoaT1OLTE7aT49MDstLWkpCiAgICAgICAgewogICAgICAgICAgICAgICAgYW5zKz1HZXQoQVtpXS0xKTsKICAgICAgICAgICAgICAgIHVwZGF0ZShBW2ldKTsKICAgICAgICB9CiAgICBpc2NhbihNKTsKICAgIGZvcihpPTA7aTxNOysraSkKICAgIHsKICAgICAgICBpc2NhbihMKTtpc2NhbihSKTsKICAgICAgICBhbnMrPWNoYW5nZUwoTC0xLFIpOwogICAgICAgIEFbTC0xXT1SOwogICAgICAgIGxwcmludChhbnMpOwogICAgICAgIHB4KCdcbicpOwogICAgfQogICAgcmV0dXJuIDA7Cn0K