Cod sursa(job #1538648)

Utilizator SilviuIIon Silviu SilviuI Data 29 noiembrie 2015 16:21:50
Problema Secv8 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.6 kb
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <algorithm>
#define inf 0x3f3f3f3f
#define mod 666013
using namespace std;
struct treap{
    int cnt,key,priority; bool rev;
    treap *left,*right;
    treap() {
        key=priority=0; cnt=1;
        rev=false; left=right=NULL;
    }
};
int n,i,llll,x,y; char s[110],c;
treap *root,*nil;
inline void update(treap* &a)
{
    if (a==nil) return;
    if (a->rev) {
        a->rev=false;
        swap(a->left,a->right);
        a->left->rev^=true;
        a->right->rev^=true;
    }
}
inline int csize(treap* a)
{
    if (a==nil) return 0; else
        return a->cnt;
}
inline int _count(treap* &a)
{
    if (a==nil) a->cnt=0; else
       a->cnt=csize(a->left)+csize(a->right)+1;
}
void rotleft(treap* &a)
{
    treap* aux=a->left;
    a->left=aux->right; aux->right=a;
    a=aux;
    _count(a); _count(a->left); _count(a->right);
}
void rotright(treap* &a)
{
    treap* aux=a->right;
    a->right=aux->left; aux->left=a;
    a=aux;
    _count(a); _count(a->left); _count(a->right);
}
void balance(treap* &a)
{
    update(a); update(a->left); update(a->right);
    if (a->left->priority>a->priority) rotleft(a); else
        if (a->right->priority>a->priority) rotright(a);
}
void insert_treap(treap* &a,int pos,int key,int priority)
{
    update(a);
    if (a==nil) {
        a=new treap; a->key=key; a->priority=priority;
        a->left=a->right=nil; return;
    }
    int v=a->left->cnt+1;
    if (pos<=v) insert_treap(a->left,pos,key,priority); else
        insert_treap(a->right,pos-v,key,priority);
    balance(a); _count(a);
}
void erase_treap(treap* &a,int pos)
{
    update(a); update(a->left); update(a->right);
    int v=a->left->cnt+1;
    if (pos<v) erase_treap(a->left,pos); else
        if (pos>v) erase_treap(a->right,pos-v); else
        {
            if (a->left==nil && a->right==nil) {
                delete a; a=nil; return;
            }
            if (a->left->priority>a->right->priority) rotleft(a); else
                rotright(a);
            erase_treap(a,pos);
        }
    _count(a);
}
int findtreap(treap* &a,int pos)
{
    update(a);
    int v=a->left->cnt+1;
    if (pos<v) return findtreap(a->left,pos); else
        if (pos>v) return findtreap(a->right,pos-v); else
            return a->key;
}
void split(treap* &a,treap* &b,treap* &c,int pos)
{
    insert_treap(a,pos+1,-1,inf);
    b=a->left; c=a->right;
    delete a;
}
void join(treap* &a,treap* &b,treap* &c)
{
    a=new treap; a->left=b; a->right=c; a->key=-1; a->priority=inf;
    _count(a); erase_treap(a,b->cnt+1);
}
void print(treap* &a)
{
    update(a);
    if (a==nil) return;
    print(a->left);
    printf("%d ",a->key);
    print(a->right);
}
int main() {
freopen("secv8.in","r",stdin);
freopen("secv8.out","w",stdout);
scanf("%d %d ",&n,&llll);
srand(time(0)); nil=new treap;
nil->cnt=0; root=nil;
for (i=1;i<=n;i++) {
    scanf("%c ",&c);
    if (c=='I') {
        scanf("%d %d ",&x,&y);
        insert_treap(root,x,y,rand()%mod);
    } else
    if (c=='R') {
        scanf("%d %d ",&x,&y);
        treap *t1,*t2,*t3,*tt;
        split(root,t1,tt,x-1);
        split(tt,t2,t3,y-x+1);
        t2->rev=true;
        join(tt,t1,t2);
        join(root,tt,t3);
    } else
    if (c=='A') {
        scanf("%d ",&x);
        printf("%d\n",findtreap(root,x));
    } else
    if (c=='D') {
        scanf("%d %d ",&x,&y);
        treap *t1,*t2,*t3,*tt;
        split(root,t1,tt,x-1);
        split(tt,t2,t3,y-x+1);
        join(root,t1,t3);
    }
}
print(root);
return 0;
}