//Pranet Verma
#include <bits/stdc++.h>
using namespace std;
typedef struct Node* pnode;
struct Node
{
pnode l,r;
int prior,cnt,val,mx;
}tree[100005],*pool=tree,*null=tree;
pnode init(int v)
{
++pool;
pool->prior=rand();
pool->val=v;
pool->l=null;
pool->r=null;
pool->cnt=0;
pool->mx=v;
return pool;
}
void update(pnode u)
{
if(u!=null)
{
u->cnt=1+u->l->cnt+u->r->cnt;
u->mx=max(u->val,max(u->l->mx,u->r->mx));
}
}
void split(pnode u,pnode &l,pnode &r,int key,int add)
{
if(u==null)
l=r=null;
else
{
int rank=add+1+u->l->cnt;
if(rank<=key)
{
split(u->r,u->r,r,key,rank);
l=u;
}
else
{
split(u->l,l,u->l,key,add);
r=u;
}
}
update(u);
}
void merge(pnode &u,pnode l,pnode r)
{
if(l==null)
u=r;
else if(r==null)
u=l;
else
{
if(l->prior > r->prior)
{
merge(l->r,l->r,r);
u=l;
}
else
{
merge(r->l,l,r->l);
u=r;
}
}
update(u);
}
int query(pnode &u,int l,int r,int x,int y)
{
if(x>r || y<l)
return -2e9;
if(x<=l && r<=y)
return u->mx;
int rank=l+u->l->cnt;
int ret=-2e9;
if(x<=rank && rank<=y)
ret=u->val;
ret=max(ret,query(u->l,l,rank-1,x,y));
ret=max(ret,query(u->r,rank+1,r,x,y));
return ret;
}
int main()
{
null->l=null->r=null;
null->val=null->mx=-2e9;
null->cnt=0;
int m;
scanf("%d\n",&m);
pnode root=null;
int have=0;
while(m--)
{
char ty;
int x,y;
scanf("%c %d %d\n",&ty,&x,&y);
if(ty=='A')
{
pnode l,r;
split(root,l,r,y-1,0);
merge(l,l,init(x));
merge(root,l,r);
++have;
}
else if(ty=='Q')
printf("%d\n",query(root,1,have,x,y));
else
assert(0);
}
}
Ly9QcmFuZXQgVmVybWEKI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnR5cGVkZWYgc3RydWN0IE5vZGUqIHBub2RlOwpzdHJ1Y3QgTm9kZQp7CiAgICBwbm9kZSBsLHI7CiAgICBpbnQgcHJpb3IsY250LHZhbCxteDsKfXRyZWVbMTAwMDA1XSwqcG9vbD10cmVlLCpudWxsPXRyZWU7CnBub2RlIGluaXQoaW50IHYpCnsKICAgICsrcG9vbDsKICAgIHBvb2wtPnByaW9yPXJhbmQoKTsKICAgIHBvb2wtPnZhbD12OwogICAgcG9vbC0+bD1udWxsOwogICAgcG9vbC0+cj1udWxsOwogICAgcG9vbC0+Y250PTA7CiAgICBwb29sLT5teD12OwogICAgcmV0dXJuIHBvb2w7Cn0Kdm9pZCB1cGRhdGUocG5vZGUgdSkKewogICAgaWYodSE9bnVsbCkKICAgIHsKICAgICAgICB1LT5jbnQ9MSt1LT5sLT5jbnQrdS0+ci0+Y250OwogICAgICAgIHUtPm14PW1heCh1LT52YWwsbWF4KHUtPmwtPm14LHUtPnItPm14KSk7CiAgICB9Cn0Kdm9pZCBzcGxpdChwbm9kZSB1LHBub2RlICZsLHBub2RlICZyLGludCBrZXksaW50IGFkZCkKewogICAgaWYodT09bnVsbCkKICAgICAgICBsPXI9bnVsbDsKICAgIGVsc2UKICAgIHsKICAgICAgICBpbnQgcmFuaz1hZGQrMSt1LT5sLT5jbnQ7CiAgICAgICAgaWYocmFuazw9a2V5KQogICAgICAgIHsKICAgICAgICAgICAgc3BsaXQodS0+cix1LT5yLHIsa2V5LHJhbmspOwogICAgICAgICAgICBsPXU7CiAgICAgICAgfQogICAgICAgIGVsc2UKICAgICAgICB7CiAgICAgICAgICAgIHNwbGl0KHUtPmwsbCx1LT5sLGtleSxhZGQpOwogICAgICAgICAgICByPXU7CiAgICAgICAgfQogICAgfQogICAgdXBkYXRlKHUpOwp9CnZvaWQgbWVyZ2UocG5vZGUgJnUscG5vZGUgbCxwbm9kZSByKQp7CiAgICBpZihsPT1udWxsKQogICAgICAgIHU9cjsKICAgIGVsc2UgaWYocj09bnVsbCkKICAgICAgICB1PWw7CiAgICBlbHNlCiAgICB7CiAgICAgICAgaWYobC0+cHJpb3IgPiByLT5wcmlvcikKICAgICAgICB7CiAgICAgICAgICAgIG1lcmdlKGwtPnIsbC0+cixyKTsKICAgICAgICAgICAgdT1sOwogICAgICAgIH0KICAgICAgICBlbHNlCiAgICAgICAgewogICAgICAgICAgICBtZXJnZShyLT5sLGwsci0+bCk7CiAgICAgICAgICAgIHU9cjsKICAgICAgICB9CiAgICB9CiAgICB1cGRhdGUodSk7Cn0KaW50IHF1ZXJ5KHBub2RlICZ1LGludCBsLGludCByLGludCB4LGludCB5KQp7CiAgICBpZih4PnIgfHwgeTxsKQogICAgICAgIHJldHVybiAtMmU5OwogICAgaWYoeDw9bCAmJiByPD15KQogICAgICAgIHJldHVybiB1LT5teDsKICAgIGludCByYW5rPWwrdS0+bC0+Y250OwogICAgaW50IHJldD0tMmU5OwogICAgaWYoeDw9cmFuayAmJiByYW5rPD15KQogICAgICAgIHJldD11LT52YWw7CiAgICByZXQ9bWF4KHJldCxxdWVyeSh1LT5sLGwscmFuay0xLHgseSkpOwogICAgcmV0PW1heChyZXQscXVlcnkodS0+cixyYW5rKzEscix4LHkpKTsKICAgIHJldHVybiByZXQ7Cn0KaW50IG1haW4oKQp7CiAgICBudWxsLT5sPW51bGwtPnI9bnVsbDsKICAgIG51bGwtPnZhbD1udWxsLT5teD0tMmU5OwogICAgbnVsbC0+Y250PTA7CiAgICBpbnQgbTsKICAgIHNjYW5mKCIlZFxuIiwmbSk7CiAgICBwbm9kZSByb290PW51bGw7CiAgICBpbnQgaGF2ZT0wOwogICAgd2hpbGUobS0tKQogICAgewogICAgICAgIGNoYXIgdHk7CiAgICAgICAgaW50IHgseTsKICAgICAgICBzY2FuZigiJWMgJWQgJWRcbiIsJnR5LCZ4LCZ5KTsKICAgICAgICBpZih0eT09J0EnKQogICAgICAgIHsKICAgICAgICAgICAgcG5vZGUgbCxyOwogICAgICAgICAgICBzcGxpdChyb290LGwscix5LTEsMCk7CiAgICAgICAgICAgIG1lcmdlKGwsbCxpbml0KHgpKTsKICAgICAgICAgICAgbWVyZ2Uocm9vdCxsLHIpOwogICAgICAgICAgICArK2hhdmU7CiAgICAgICAgfQogICAgICAgIGVsc2UgaWYodHk9PSdRJykKICAgICAgICAgICAgcHJpbnRmKCIlZFxuIixxdWVyeShyb290LDEsaGF2ZSx4LHkpKTsKICAgICAgICBlbHNlCiAgICAgICAgICAgIGFzc2VydCgwKTsKICAgIH0KfQ==