Cod sursa(job #3269809)

Utilizator Alex_BerbescuBerbescu Alexandru Alex_Berbescu Data 20 ianuarie 2025 20:59:14
Problema Secv8 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.97 kb
#pragma GCC optimize("O3")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
const int dim = 1e5 + 55;
ifstream fin("secv8.in");
ofstream fout("secv8.out");

struct treap
{
    int key, priority, rev, cnt;
    treap *st, *dr;
    treap(int key, int priority, int rev, int cnt, treap *st, treap *dr)
    {
        this->key = key, this->priority = priority, this->rev = rev, this->cnt = cnt, this->st = st, this->dr = dr;
    }
}*rad, *gol;
void init(treap*&nod)
{
    if(nod->rev)
    {
        swap(nod->st, nod->dr);
        nod->rev ^= 1;
        nod->st->rev ^= 1;
        nod->dr->rev ^= 1;
    }
}
void update(treap*&nod)
{
    if(nod != gol)
        nod->cnt = nod->st->cnt + nod->dr->cnt + 1;
}
void rotate_left(treap *&nod)
{
    init(nod->st);
    treap *t = nod->st;
    nod->st = t->dr;
    t->dr = nod;
    nod = t;
    update(nod->dr);
    update(nod);

}
void rotate_right(treap*&nod)
{
    init(nod->dr);
    treap *t = nod->dr;
    nod->dr = t->st;
    t->st = nod;
    nod = t;
    update(nod->st);
    update(nod);
}
void balance(treap*&nod)
{
    init(nod);
    if(nod->st->priority > nod->priority)
        rotate_left(nod);
    else
        rotate_right(nod);
}
int cauta(treap*nod, int poz)
{
    init(nod);
    if(nod->st->cnt >= poz)
    {
        return cauta(nod->st, poz);
    }
    if(nod->st->cnt + 1 < poz)
    {
        return cauta(nod->dr, poz - nod->st->cnt - 1);
    }
    return nod->key;
}
void insertu(treap*&nod, int key, int priority, int poz)
{
    if(nod ==gol)
    {
        nod = new treap(key, priority, 0, 1, gol, gol);
        return;
    }
    init(nod);
    if(nod->st->cnt + 1 >= poz)
        insertu(nod->st, key, priority, poz);
    else
        insertu(nod->dr, key, priority, poz - nod->st->cnt - 1);

    update(nod);
    balance(nod);
}
void eraseu(treap *&nod, int poz)
{
    if(nod == gol)
        return;
    init(nod);
    if(nod->st->cnt >= poz)
        eraseu(nod->st, poz);
        else if (nod->st->cnt + 1 < poz)
            eraseu(nod->dr, poz - nod->st->cnt - 1);
        else
        {

            if(nod->st == gol && nod->dr == gol)
            {
                delete nod, nod = gol;
            }
            else
            {
                (nod->st->priority > nod->dr->priority) ? rotate_left(nod) : rotate_right(nod);
                eraseu(nod, poz);
            }
        }
    update(nod);
}

void split(treap *&nod, treap *&lt, treap *&rt, int poz)
{

    insertu(nod, 0, 1000000000, poz);

    lt= nod->st;
    rt = nod->dr;
    delete nod;
    nod = gol;
}

void join(treap *&nod, treap*&lt, treap*&rt)
{

    nod = new treap(0, 1000000000, 0, lt->cnt + rt->cnt + 1, lt, rt);
    eraseu(nod, lt->cnt + 1);
}

void afis(treap *nod)
{

    if(nod == gol)
        return;
    init(nod);
    afis(nod->st);
    fout << nod->key << '\n';
    afis(nod->dr);
}

void start()
{

    srand(unsigned(time(0)));
    rad = gol = new treap(0, 0, 0, 0, NULL, NULL);
    gol->st = gol->dr = gol;
}
int n, a, b;
char c;
bool op;
int32_t main(int argc, char * argv[])
{
   start();
   fin >> n >> op;
   while(n--)
   {
       fin >> c;
       if(c == 'I')
       {
           fin >> a >> b;
           insertu(rad, b, rand() % 1000000000 + 1, a);
       }
       if(c == 'A')
       {
           fin >> a;
           fout << cauta(rad, a) << '\n';
       }
       if(c == 'R')
       {
           fin >> a >> b;
           treap *x, *y, *z, *t;
           split(rad, x, y, b + 1);
           split(x, z, t, a);
           z->rev ^= 1;
           join(x, z, t);
           join(rad, x, y);

       }
       if(c == 'D')
       {
           fin >> a >> b;
           treap *x , *y, *z, *t;
           split(rad, x, y, b + 1);
           split(x, z, t, a);
           join(rad, z, y);
       }
   }
   afis(rad);



    return 0;
}