Cod sursa(job #2672904)

Utilizator OvidRata Ovidiu Ovid Data 15 noiembrie 2020 13:03:09
Problema Schi Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.77 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

int t, n, m, k, a[300010], q, l, r;
ifstream fin("schi.in"); ofstream fout("schi.out");
#define cin fin
#define cout fout


struct it{
    int c, sz, p;
    it *l, *r;
    it(int p, int c){
        this->p=p;
        this->c=c;
        this->l=NULL;
        this->r=NULL;
        this->sz=1;
    }
}*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 merge(it*& t, it*l, it*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){l=r=0; return;}

    int cr=szof(t->l)+1+add;

    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*newt=new it(rand(), c);
    it* n1;
    split(t, t, n1, pos-1, 0);
    merge(t, t, newt);
    merge(t, t, n1);
}


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


int32_t main(){
INIT
srand((unsigned)(time(0)));

cin>>n;
for(int i=1; i<=n; i++){
    int x;
    cin>>x;
    insert(R, x, i);
}
out(R);

return 0;
}