Cod sursa(job #2254575)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 5 octombrie 2018 16:28:41
Problema Secv8 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.83 kb
#include <bits/stdc++.h>

using namespace std;

FILE *fi, *fout;

typedef struct Treap *A;

Treap *NIL;

struct Treap {
    Treap *left, *right;
    int val, prio;
    int sz;
    bool lazy;
    Treap(int _val) {
        left = right = NIL;
        val = _val;
        prio = rand();
        sz = 1;
        lazy = 0;
    }
}*root;

inline void refresh(Treap *nod) {
    nod -> sz = nod -> left -> sz + nod -> right -> sz + 1;
}

inline void prop(Treap *nod) {
    if(nod -> lazy) {
        swap(nod -> left, nod -> right);
    }
    nod -> left -> lazy ^= nod -> lazy;
    nod -> right -> lazy ^= nod -> lazy;
    nod -> lazy = 0;
}

int acces(Treap *nod, int pos) {
    prop(nod);
    if(pos <= nod -> left -> sz) {
        return acces(nod -> left, pos);
    }
    if(pos == nod -> left -> sz + 1) {
        return nod -> val;
    }
    return acces(nod -> right, pos - nod -> left -> sz - 1);
}


Treap *join(Treap *a, Treap *b) {
    if(a == NIL) {
        return b;
    }
    if(b == NIL) {
        return a;
    }
    prop(a);
    prop(b);
    if(a -> prio >= b -> prio) {
        a -> right = join(a -> right, b);
        refresh(a);
        return a;
    }
    b -> left = join(a, b -> left);
    refresh(b);
    return b;
}

pair <Treap *, Treap *> split(Treap *nod, int pos) {
    if(nod == NIL) {
        return {NIL, NIL};
    }
    prop(nod);
    if(nod -> left -> sz >= pos) {
        pair <Treap *, Treap *> aux = split(nod -> left, pos);
        nod -> left = aux.second;
        refresh(nod);
        return {aux.first, nod};
    }
    if(nod -> left -> sz + 1 == pos) {
        pair <Treap *, Treap *> aux = {nod, nod -> right};
        nod -> right = NIL;
        refresh(nod);
        return aux;
    }
    pair <Treap *, Treap *> aux = split(nod -> right, pos - nod -> left -> sz - 1);
    nod -> right = aux.first;
    refresh(nod);
    return {nod, aux.second};
}

Treap *ins(Treap *nod, int pos, int val) {
    pair <Treap *, Treap *> aux = split(nod, pos - 1);
    Treap *cur = new Treap(val);
    return join(aux.first, join(cur, aux.second));
}

Treap *del(Treap *nod, int l, int r) {
    pair <Treap *, Treap *> aux1 = split(nod, r);
    pair <Treap *, Treap *> aux2 = split(aux1.first, l - 1);
    if(aux2.second != NIL) {
        delete aux2.second;
    }
    return join(aux2.first, aux1.second);
}

Treap *rev(Treap *nod, int l, int r) {
    pair <Treap *, Treap *> aux1 = split(nod, r);
    pair <Treap *, Treap *> aux2 = split(aux1.first, l - 1);
    if(aux2.second != NIL) {
        aux2.second -> lazy ^= 1;
    }
    return join(join(aux2.first, aux2.second), aux1.second);
}

void dfs(Treap *nod) {
    if(nod == NIL) {
        return ;
    }
    prop(nod);
    dfs(nod -> left);
    fprintf(fout,"%d " ,nod -> val);
    dfs(nod -> right);
}

int main() {
    int n, type;
    fi = fopen("secv8.in" ,"r");
    fout = fopen("secv8.out" ,"w");
    srand(time(NULL));
    fscanf(fi,"%d %d " ,&n,&type);
    NIL = new Treap(0);
    NIL -> left = NIL -> right = NULL;
    NIL -> sz = 0;
    NIL -> lazy = 0;
    root = NIL;
    while(n > 0) {
        n--;
        char ch;
        fscanf(fi,"%c " ,&ch);
        if(ch == 'I') {
            int pos, x;
            fscanf(fi,"%d %d " ,&pos,&x);
            root = ins(root, pos, x);
        }
        if(ch == 'A') {
            int pos;
            fscanf(fi,"%d " ,&pos);
            fprintf(fout,"%d\n" ,acces(root, pos));
        }
        if(ch == 'R') {
            int l, r;
            fscanf(fi,"%d %d " ,&l,&r);
            root = rev(root, l, r);
        }
        if(ch == 'D') {
            int l, r;
            fscanf(fi,"%d %d " ,&l,&r);
            root = del(root, l, r);
        }
    }
    dfs(root);
    fclose(fi);
    fclose(fout);
    return 0;
}