fork download
//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);
}
// void display(pnode u,int add=0)
// {
//     if(u)
//     {
//         int rank=add+1+sz(u->l);
//         cout<<"At "<<rank<<endl;
//         cout<<"Going left"<<endl;
//         display(u->l,add);
//         cout<<"Back at "<<rank<<endl;
//         cout<<"Going right"<<endl;
//         display(u->r,rank);
//     }
//     cout<<"Nothing"<<endl;
// }
int main()
{
    null->l=null->r=null;
    null->val=null->mx=-2e9;
    null->cnt=0;
    int m;
    scanf("%d\n",&m);
    pnode root=null;
    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);
        }
        else if(ty=='Q')
        {
            pnode A,B,C,temp;
            split(root,A,temp,x-1,0);
            split(temp,B,C,y,x-1);
            printf("%d\n",B->mx);
            merge(temp,B,C);
            merge(root,A,temp);
        }   
        else
            assert(0);
    }
}
Runtime error #stdin #stdout #stderr 0s 5808KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
prog: prog.cpp:114: int main(): Assertion `0' failed.