线段树套TREAP。。。。。不太会写,调了N久
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
const int N=100005;
int a[N];
inline int ran() { static int ranx = 654123789; return ranx += ranx<<2|1; }
struct Node {
Node *ch[2];
int val, sz, r;
}memo[N*50], *nill;
struct TREAP
{
Node *root;
} treap[N<<2];
int tot;
struct SEG
{
int l, r, mid;
} seg[N<<2];
void up(Node *o) {
if (o==nill) return ;
o->sz = o->ch[0]->sz + o->ch[1]->sz + 1;
}
void New_node(Node *&o,int val=0) {
o = &memo[tot++];
o->ch[0] = o->ch[1] = nill;
o->sz = 1;
o->r = ran();
o->val = val;
up(o);
}
void l_rotate(Node *&o)
{
Node *b=o->ch[1];
o->ch[1]=b->ch[0];
b->ch[0]=o;
o=b;
b=o->ch[0];
b->sz=b->ch[0]->sz+b->ch[1]->sz+1;
o->sz=o->ch[0]->sz+o->ch[1]->sz+1;
}
void r_rotate(Node *&o)
{
Node *b=o->ch[0];
o->ch[0]=b->ch[1];
b->ch[1]=o;
o=b;
b=o->ch[1];
b->sz=b->ch[0]->sz+b->ch[1]->sz+1;
o->sz=o->ch[0]->sz+o->ch[1]->sz+1;
}
void ins(Node *&o,int val) {
if(o==nill){
New_node(o,val);
return;
}else if(val<o->val){
ins(o->ch[0],val);
if(o->ch[0]->r < o->r){
r_rotate(o);
}
}else{
ins(o->ch[1],val);
if(o->ch[1]->r < o->r){
l_rotate(o);
}
}
up(o);
}
void del(Node *&o,int val) {
if(val==o->val){
if(o->ch[0]==nill||o->ch[1]==nill){
Node *t=o;
if(o->ch[1]==nill){
o=o->ch[0];
}else{
o=o->ch[1];
}
t=nill;
}else{
if(o->ch[0]->r<o->ch[1]->r){
r_rotate(o);
del(o->ch[1],val);
}else{
l_rotate(o);
del(o->ch[0],val);
}
}
}else if(val<o->val){
del(o->ch[0],val);
}else{
del(o->ch[1],val);
}
up(o);
}
int getpos(Node *&o,int va)
{
if(o==nill){
return 0;
}
if(va<o->val) return getpos(o->ch[0],va);
return o->ch[0]->sz+1+getpos(o->ch[1],va);
}
void build(int l,int r,int rt)
{
seg[rt].l=l, seg[rt].r=r, seg[rt].mid=(l+r)>>1;
int i, p;
treap[rt].root=nill;
for(i=l;i<=r;i++){
ins(treap[rt].root,a[i]);
}
if(l==r) return;
build(l,seg[rt].mid,rt<<1);
build(seg[rt].mid+1,r,rt<<1|1);
}
int query(int l,int r,int rt,int va)
{
if(l<=seg[rt].l&&seg[rt].r<=r){
return getpos(treap[rt].root,va);
}
int ret=0;
if(l<=seg[rt].mid) ret+=query(l,r,rt<<1,va);
if(r>seg[rt].mid) ret+=query(l,r,rt<<1|1,va);
return ret;
}
void update(int id,int rt,int va1,int va2)
{
del(treap[rt].root,va1);
ins(treap[rt].root,va2);
if(seg[rt].l==seg[rt].r){
return;
}
if(id<=seg[rt].mid) update(id,rt<<1,va1,va2);
else update(id,rt<<1|1,va1,va2);
}
int main() {
int n, m, i, j, p, q, x;
char s[33];
while(~scanf("%d%d",&n,&m)) {
tot=0;
for(i=1;i<=n;i++) scanf("%d",&a[i]);
New_node(nill);
nill->sz=0;
build(1,n,1);
while(m--){
scanf("%s",s);
if(s[0]=='M'){
scanf("%d%d",&p,&x);
update(p,1,a[p],x);
a[p]=x;
}else{
scanf("%d%d%d",&p,&q,&x);
printf("%d\n",query(p,q,1,x));
}
}
}
return 0;
}