Cod sursa(job #2825577)

Utilizator betybety bety bety Data 4 ianuarie 2022 21:02:25
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.39 kb
#include <bits/stdc++.h>
using namespace std;
ifstream in("secv8.in");
ofstream out("secv8.out");
struct nod
{
    nod* l=NULL;
    nod* r=NULL;
    bool rv;
    int val;
    int pri;
    int w;
    nod(int x): val(x),pri(rand()),w(1),rv(0){};
    void upd();
};
char tip;
nod* root=0;
int tst,trs,a,b;
int cnt (nod* t)
{return t?t->w:0;}
void nod::upd()
{
    w=1+cnt(l)+cnt(r);
    if(rv)
    {
        swap(l,r);
        if(l)l->rv^=1;
        if(r)r->rv^=1;
        rv=0;
    }
}
pair<nod*,nod*> split(nod* t,int poz)
{
    if(t==NULL)
        return {NULL,NULL};
    t->upd();
    if(poz<=cnt(t->l))
    {
        auto r=split(t->l,poz);
        t->l=r.second,t->upd();
        return {r.first,t};
    }
    else
    {
        auto r=split(t->r,poz-cnt(t->l)-1);
        t->r=r.first,t->upd();
        return {t,r.second};
    }
}
nod* join(nod* l,nod* r)
{
    if(!l)return r;
    if(!r)return l;
    l->upd(),r->upd();
    if(l->pri>r->pri)
    {
        l->r=join(l->r,r),l->upd();
        return l;
    }
    else
    {
        r->l=join(l,r->l);
        r->upd();
        return r;
    }
}
void afis(nod* t)
{
    if(t==0)
        return;
    t->upd();
    afis(t->l);
    out<<t->val<<' ';
    afis(t->r);
}
nod* add(nod* t,int x,int poz)
{
    auto aux=split(t,poz-1);
    nod* aux2=new nod(x);
    return join(join(aux.first,aux2),aux.second);
}
nod* dell(nod* t,int l,int r)
{
    auto aux=split(t,r);
    auto aux2=split(aux.first,l-1);
    delete(aux2.second);
    return join(aux2.first,aux.second);
}
nod* rev(nod* t,int l,int r)
{
    auto aux=split(t,r);
    auto aux2=split(aux.first,l-1);
    aux2.second->rv^=1;
    return join(join(aux2.first,aux2.second),aux.second);
}
int get(nod* t,int poz)
{
    t->upd();
    if(poz-1==cnt(t->l)) return t->val;
    if(poz<=cnt(t->l)) return get(t->l,poz);
    return get(t->r,poz-cnt(t->l)-1);
}
int main()
{
    ios_base::sync_with_stdio(false);
    in.tie(0),out.tie(0);
    in>>tst>>trs;
    while(tst--)
    {
        in>>tip;
        if(tip=='I')
            in>>a>>b,
            root=add(root,b,a);
        else if(tip=='A')
            in>>a,
            out<<get(root,a)<<'\n';
        else if(tip=='R')
            in>>a>>b,
            root=rev(root,a,b);
        else in>>a>>b,
            root=dell(root,a,b);
    }
    afis(root);
    return 0;
}