Think Twice, Code Once
#include
#define il inline
#define get getchar
#define put putchar
#define is isdigit
#define re register
#define int long long
#define dfor(i,a,b) for(re int i=a;i<=b;++i)
#define dforr(i,a,b) for(re int i=a;i>=b;--i)
#define dforn(i,a,b) for(re int i=a;i<=b;++i,put(10))
#define mem(a,b) memset(a,b,sizeof a)
#define memc(a,b) memcpy(a,b,sizeof a)
#define pr 114514191981
#define gg(a) cout<<a,put(32)
#define INF 0x7fffffff
#define tt(x) cout<<x<<'\n'
#define ls i<<1
#define rs i<<1|1
#define la(r) tr[r].ch[0]
#define ra(r) tr[r].ch[1]
#define lowbit(x) (x&-x)
using namespace std;
typedef unsigned int ull;
int read(void)
{
re int x=0,f=1;re char c=get();
while(!is(c)) (f=c==45?-1:1),c=get();
while(is(c)) x=(x<<1)+(x<<3)+(c^48),c=get();
return x*f;
}
void write(int x)
{
if(x<0) x=-x,put(45);
if(x>9) write(x/10);
put((x%10)^48);
}
#define writeln(a) write(a),put(10)
#define writesp(a) write(a),put(32)
#define writessp(a) put(32),write(a)
const int N=1e6+10,M=3e4+10,SN=1e4+10,mod=998244353;
int n,q,cnt,a[N],rt[N],pre[N];
set<int > s[N];
struct TR
{
int l,r,sum;
}tr[N*50];
void modify(int &p,int l,int r,int pos,int v)
{
if(!p) p=++cnt;
tr[p].sum+=v;
if(l==r) return ;
int mid=(l+r)>>1;
if(pos<=mid) modify(tr[p].l,l,mid,pos,v);
else modify(tr[p].r,mid+1,r,pos,v);
}
void add(int x,int v,int pos)
{
for(re int i=x;i<=n;i+=lowbit(i)) modify(rt[i],0,n,pos,v);
}
void modify1(int col,int pos)
{
auto it=s[a[pos]].lower_bound(pos);
re int tmp=*prev(it);
add(pos,-1,tmp);
if(next(it)!=s[a[pos]].end())
{
add(*next(it),-1,pos);
add(*next(it),1,tmp);
}
s[a[pos]].erase(pos);
a[pos]=col;
s[col].insert(pos);
it=s[col].lower_bound(pos);
tmp=*prev(it);
add(pos,1,tmp);
if(next(it)!=s[col].end())
{
add(*next(it),-1,tmp);
add(*next(it),1,pos);
}
}
int query1(int u,int l,int r,int x,int y)
{
if(x<=l&&r<=y) return tr[u].sum;
int mid=(l+r)>>1;
if(y<=mid) return query1(tr[u].l,l,mid,x,y);
else if(x>mid) return query1(tr[u].r,mid+1,r,x,y);
else return query1(tr[u].l,l,mid,x,y)+query1(tr[u].r,mid+1,r,x,y);
}
int pre_query(int l,int r)//这是我改之后的
{
re int ans=0;
for(re int i=r;i;i-=lowbit(i)) ans+=query1(rt[i],0,n,0,l-1);
for(re int i=l-1;i;i-=lowbit(i)) ans-=query1(rt[i],0,n,0,l-1);
return ans;
}
signed main()
{
n=read(),q=read();
dfor(i,1,n)
{
a[i]=read();
add(i,1,pre[a[i]]);
pre[a[i]]=i,s[a[i]].insert(i);
}
dfor(i,1,N-1) s[i].insert(0);
re int l,r;
re char op;
while(q--)
{
scanf("%c",&op),l=read(),r=read();
if(op^'R') writeln(pre_query(l,r));
else modify1(r,l);
}
return 0;
}