Cod sursa(job #2672667)

Utilizator OvidRata Ovidiu Ovid Data 14 noiembrie 2020 12:56:38
Problema Secv8 Scor 95
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.05 kb
#include<bits/stdc++.h>
using namespace std;
#define INIT  ios_base :: sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define ll long long
#define pii pair<int, int>
#define count_bits __builtin_popcount
#define int ll


ifstream fin("secv8.in"); ofstream fout("secv8.out");
#define cin fin
#define cout fout



int q, b0;

struct it{
    int c, p, sz;
    bool b;

    it *l, *r;

    it(int p, int c, it*l, it*r){
        this->p=p;
        this->c=c;
        this->l=l;
        this->r=r;
        this->b=false;
    }



}*R;



int szof(it* t){
    if(!t){return 0;}
    return t->sz;
}

void recalc(it*&t){
    if(!t){return;}
    t->sz=szof(t->l)+szof(t->r)+1;
}



it* create(int p, int c, it*l, it*r){
    it*t=new it(p, c, l, r);
    recalc(t);
    return t;
}



void push(it*t){
if( (!t) || (!t->b) ){return;}
t->b=false;
swap(t->l, t->r);
if(t->l){t->l->b^=true;}
if(t->r){t->r->b^=true;}
return;
}


void merge(it*&t, it*l, it*r){
    push(l);
    push(r);
    if((!l)||(!r)){
        t=(l)?(l):(r);
        recalc(t);
        return;
    }

    if(l->p>r->p){
        merge(l->r, l->r, r), t=l;
    }
    else{
        merge(r->l, l, r->l), t=r;
    }
    recalc(t);
    return;
}




void split(it*t, it*&l, it*&r, int pos, int add){
    if(!t){r=l=0; return;}
    push(t);
    int cr=add+szof(t->l)+1;
    if(pos<cr){
        split(t->l, l, t->l, pos, add), r=t;
    }
    else{
        split(t->r, t->r, r, pos, cr), l=t;
    }
    recalc(t);
}




void insert(it*& t, int pos, int c){
    it* nt=create(rand(), c, NULL, NULL);
    it *n1;
    split(t, t, n1, pos-1, 0);
    merge(t, t, nt);
    merge(t, t, n1);
}

void erase(it*& t, int l, int r){
    it *n1, *n2;
    split(t, t, n1, l-1, 0);
    split(n1, n1, n2, r-l+1, 0);
    merge(t, t, n2);
}


void reverse(it*&t, int l,int r){
    it *n1, *n2;
    split(t, t, n1, l-1, 0);
    split(n1, n1, n2, r-l+1, 0);
    n1->b^=true;
    merge(n1, n1, n2);
    merge(t, t, n1);
}


int access(it*& t, int pos){
    it *n1, *n2;
    split(t, t, n1, pos-1, 0);
    split(n1, n1, n2, 1, 0);
    int res=n1->c;
    merge(n1, n1, n2);
    merge(t, t, n1);
    return res;
}



void out(it*t){
    if(!t){return;}
    push(t);
    out(t->l);
    cout<<t->c<<" ";
    out(t->r);
}



int32_t main(){
INIT
srand( (unsigned)(time(0)) );
cin>>q>>b0;

while(q--){
    char o; int p, c, l, r;
    cin>>o;
    switch(o){
        case 'I':{
            cin>>p>>c;
            insert(R, p, c);
        }break;
        case 'D':{
            cin>>l>>r;
            erase(R, l, r);
        }break;
        case 'R':{
            cin>>l>>r;
            reverse(R, l, r);
        }break;
        case 'A':{
            cin>>p;
            cout<<access(R, p)<<"\n"<<flush;
        } break;
    }
}
out(R);




return 0;
}