Cod sursa(job #2670902)

Utilizator OvidRata Ovidiu Ovid Data 10 noiembrie 2020 21:43:40
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.03 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


struct it{
    int c, p, sz;
    bool b;
    it* l, *r;

    it(int p, int c, it*l, it*r){
        this->c=c;
        this->p=p;
        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;
}


void push(it*&t){
    if( (!t) || (!t->b) ){return;}

    t->b=false;
    swap(t->r, t->l);
    if(t->l){t->l->b^=true;}
    if(t->r){t->r->b^=true;}
}



void merge(it *&t, it *l, it*r){
    push(l);
    recalc(l);
    push(r);
    recalc(r);
    if((!l) ||(!r)){
        t=(l)?(l):(r); 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);
}



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





void insert(int pos,int c, it*&t){
it *t1, *newt;
newt=new it(rand(), c, NULL, NULL );
recalc(newt);
split(t, t, t1, pos, 0);
merge(t, t, newt);
merge(t, t, t1);
}


void erase(int l, int r, it*&t){

it*t1, *t2;
split(t, t, t1, l, 0);
split(t1, t1, t2, r-l+1, 0);
merge(t, t, t2);

}



void reverse(int l, int r, it*&t){

it*t1, *t2;
split(t, t, t1, l, 0);
split(t1, t1, t2, r-l+1, 0);
t1->b^=true;
merge(t1, t1, t2);
merge(t, t, t1);
}



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



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




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

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