/*
MiYu原创, 转帖请注明 : 转载自 ______________白白の屋
http://www.cnblog.com/MiYu
Author By : MiYu
Test : 1
Program : 2688
*/
#include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 10000;
int nCount = 0, N, M, x , y;
int com[MAX + 1], num[300 * MAX + 1];
long long sum = 0;
char ask[5];
inline int low ( int x ) {
return x & ( -x );
}
void modify ( int x, int val ){ // 修改
while ( x <= MAX ){
com[x] += val; x += low ( x );
}
}
int quy ( int x ){ // 查询
int sum = 0;
while ( x > 0 ){
sum += com[x]; x ^= low ( x );
}
return sum ;
}
inline bool scan_d(int &num) // 输入
{
char in;bool IsN=false;
in=getchar();
if(in==EOF) return false;
while(in!='-'&&(in<'0'||in>'9')) in=getchar();
if(in=='-'){ IsN=true;num=0;}
else num=in-'0';
while(in=getchar(),in>='0'&&in<='9'){
num*=10,num+=in-'0';
}
if(IsN) num=-num;
return true;
}
int main ()
{
while ( scan_d ( N ) ){
memset ( com, 0, sizeof ( com ) ); sum = 0;
for ( int i = 0; i < N; ++ i ){
scan_d ( num[i] ); modify ( num[i], 1 );
sum += quy ( num[i] - 1 );
}
scan_d ( M );
while ( M -- ){
scanf ( "%s",ask ); int temp;
switch ( ask[0] ){
case 'Q' : cout << sum << endl; break;
case 'R' : scan_d ( x ), scan_d ( y ); temp = num[x];
while ( x < y ) { num[x] = num[x+1];
num[x] > temp ? sum -- : num[x] == temp ?: sum++ ; x ++;
}
num[y] = temp; break;
}
}
}
return 0;
}