Cod sursa(job #2482242)

Utilizator NicolaalexandraNicola Alexandra Mihaela Nicolaalexandra Data 27 octombrie 2019 22:15:34
Problema Secv8 Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.16 kb
#include <fstream>
#include <stdlib.h>
#include <ctime>
#define INF 2000000000
using namespace std;

ifstream fin ("secv8.in");
ofstream fout ("secv8.out");
struct treap{
    int val;
    int priority;
    int nr;
    int lazy;
    treap *left;
    treap *right;
    treap (int val, int priority, int nr, int lazy, treap *left, treap *right){
        this->val = val;
        this->priority = priority;
        this->nr = nr;
        this->lazy = lazy;
        this->left = left;
        this->right = right;
    }};
treap *rad,*nil;
int get_random (){
    return 1LL*rand()*rand()%(INF-1)+1;
}
void update (treap *&rad){
    if (rad == nil)
        rad->nr = 0;
    else {
        rad->nr = rad->left->nr + rad->right->nr + 1;
        /// acum lazy ul
        if (rad->lazy){
            if (rad->left != nil)
                rad->left->lazy = 1-rad->left->lazy;
            if (rad->right != nil)
                rad->right->lazy = 1-rad->right->lazy;
            /// acum trebuie sa interschimb fiii
            swap (rad->left,rad->right);
            rad->lazy = 0;
        }}}
pair <treap*,treap*> split (treap *&rad, int poz){
    update (rad); /// mereu trb sa fac lazy ul
    pair <treap*, treap*> ans;
    if (rad == nil)
        return make_pair(nil,nil);
    if (rad->left->nr + 1 > poz){ /// o sa ma duc in stanga
        ans = split (rad->left,poz);
        rad->left = ans.second;
        ans.second = rad;
    } else {
        ans = split (rad->right,poz - rad->left->nr - 1);
        rad->right = ans.first;
        ans.first = rad;
    }
    update (rad);
    return ans;
}
treap* join (treap *&rad1, treap *&rad2){
    update (rad1);
    update (rad2);
    if (rad1 == nil)
        return rad2;
    if (rad2 == nil)
        return rad1;
    if (rad1->priority > rad2->priority){
        /// pastrez radacina rad1 si fac join intre subarborele drept al lui rad1 si rad2
        /// si treapul rezultat e tot fiul drept a lui rad1
        rad1->right = join (rad1->right,rad2);
        update (rad1);
        return rad1;
    } else {
        rad2->left = join (rad2->left,rad1);
        update (rad2);
        return rad2;
    }
    return nil;
}
void _insert (treap *&rad, int poz, int val){
    pair <treap*,treap*> aux = split (rad,poz-1);
    rad = new treap (val,get_random(),0,0,aux.first,aux.second);
    update (rad);
}
void access (treap *&rad, int nr){
    update (rad);
    pair <treap*,treap*> aux2 = split (rad,nr);
    pair <treap*,treap*> aux1 = split (aux2.first,nr-1);
    fout<<aux1.second->val<<"\n";
    aux2.first = join (aux1.first,aux1.second);
    rad = join (aux2.first,aux2.second);
}
void _delete (treap *&rad, int x, int y){
    /// dau split de doua ori si unesc cele doua bucati
    pair <treap*, treap*> aux2 = split (rad,y);
    pair <treap*, treap*> aux1 = split (aux2.first,x-1);
    /// acum trb sa le dau join
    rad = join (aux1.first,aux2.second);
    update (rad);
}

void _reverse (treap *&rad, int x, int y){
    /// trb sa aflu bucata din mijloc
    pair <treap*, treap*> aux2 = split (rad,y);
    pair <treap*, treap*> aux1 = split (aux2.first,x-1);
    /// trb sa marchez in lazy
    aux1.second->lazy = 1;
    /// acum trebuie sa le unesc la loc
    aux2.first = join (aux1.first, aux1.second);
    rad = join (aux2.first,aux2.second);
    update (rad);
}
void dfs (treap *&rad){
    update (rad);
    if (rad == nil)
        return;
    dfs (rad->left);
    fout<<rad->val<<" ";
    dfs (rad->right);
    return;
}
int main (){

    srand(time(0));
    rad = nil = new treap (0,0,0,0,NULL,NULL);

    int t,x,y,poz,ok;
    fin>>t>>ok;
    for (;t--;){
        char op; fin>>op;
        if (op == 'I'){
            fin>>poz>>x;
            _insert (rad,poz,x);
            continue;
        }
        if (op == 'A'){
            fin>>x;
            access(rad,x);
            continue;
        }
        if (op == 'R'){
            fin>>x>>y;
            _reverse (rad,x,y);
            //dfs (rad);
            //fout<<"\n";
            continue;
        }
        fin>>x>>y;
        _delete (rad,x,y);
    }
    dfs (rad);


    return 0;
}