Cod sursa(job #2097542)

Utilizator andreicoman299Coman Andrei andreicoman299 Data 31 decembrie 2017 19:09:01
Problema Secv8 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.05 kb
#include <bits/stdc++.h>

#define BUF_SIZE 1 << 14
char buf[BUF_SIZE];
int pbuf=BUF_SIZE;
FILE*fi,*fo;
inline char nextch(){
    if(pbuf==BUF_SIZE){
        fread(buf, BUF_SIZE, 1, fi);
        pbuf=0;
    }
    return buf[pbuf++];
}
inline int nextnum(){
    int a = 0;
    char c = nextch();
    while(!isdigit(c))
        c = nextch();
    while(isdigit(c)){
        a = a * 10 + c - '0';
        c = nextch();
    }
    return a;
}
char bufw[1 + BUF_SIZE];
int pbufw = 0;
inline void printch(char c){
    bufw[pbufw++] = c;
    if(pbufw == BUF_SIZE){
        fwrite(bufw, 1, BUF_SIZE, fo);
        pbufw = 0;
    }
}
void printnum(int a){
    if(a > 0){
        printnum(a / 10);
        printch(a % 10 + '0');
    }
}

typedef struct Treap* Tree;
typedef std::pair <Tree, Tree> PTT;
Tree NIL;
struct Treap{
    int val;
    int priority = rand();
    int sub = 1;
    bool lazy;
    Tree st = NIL, dr = NIL;

    Treap(int nval = 0){val = nval;}
    ~Treap(){if(st != NIL) delete st; if(dr != NIL) delete dr;}
};

void reset(Tree T){
    T -> sub = 1 + T -> st -> sub + T -> dr -> sub;
}

void propagate(Tree T){
    if(T == NIL) return;
    if(T -> lazy){
        std::swap(T -> st, T -> dr);
        T -> st -> lazy ^= 1;
        T -> dr -> lazy ^= 1;
        T -> lazy = 0;
    }
}

void order(Tree T){
    propagate(T);
    if(T == NIL) return;
    order(T -> st);
    printnum(T -> val);
    printch(' ');
    order(T -> dr);
}

int access(Tree T, int pos){
    propagate(T);
    if(T == NIL)
        return -1;
    if(pos <= T -> st -> sub)
        return access(T -> st, pos);
    else if(pos == T -> st -> sub + 1)
        return T -> val;
    else
        return access(T -> dr, pos - T -> st -> sub - 1);
}

Tree join(Tree A, Tree B){
    if(A == NIL) return B;
    if(B == NIL) return A;
    propagate(A);propagate(B);

    if(A -> priority >= B -> priority){
        A -> dr = join(A -> dr, B);
        reset(A);
        return A;
    }
    else{
        B -> st = join(A, B -> st);
        reset(B);
        return B;
    }
}

PTT split(Tree T, int pos){
    if(T == NIL) return {NIL, NIL};
    propagate(T);

    if(pos <= T -> st -> sub){
        PTT S = split(T -> st, pos);
        T -> st = S.second;
        reset(T);
        return {S.first, T};
    }
    else if(pos == T -> st -> sub + 1){
        PTT S = {T -> st, T};
        T -> st = NIL;
        reset(T);
        return S;
    }
    else{
        PTT S = split(T -> dr, pos - 1 - T -> st -> sub);
        T -> dr = S.first;
        reset(T);
        return {T, S.second};
    }
}

Tree insert(Tree T, int val, int pos){
    Tree newT = new Treap(val);
    PTT S = split(T, pos);
    return join(S.first, join(newT, S.second));
}
Tree erase(Tree T, int st, int dr){
    PTT x = split(T, dr + 1);
    PTT y = split(x.first, st);
    if(y.second != NIL) delete y.second;
    return join(y.first, x.second);
}
Tree rotate(Tree T, int st, int dr){
    PTT x = split(T, dr + 1);
    PTT y = split(x.first, st);
    y.second -> lazy ^= 1;
    return join(y.first, join(y.second, x.second));
}

int main(){
    fi = fopen("secv8.in","r");
    fo = fopen("secv8.out","w");

    srand(time(NULL));
    NIL = new Treap();
    NIL -> st = NIL -> dr = NIL;
    NIL -> sub = 0;
    Tree T = NIL;

    int n = nextnum(), q = nextnum();
    int con = 0;
    for(int i = 1; i <= n; i++){
        char op = nextch();
        if(op == 'I'){
            int a = nextnum(), b = nextnum();
            T = insert(T, b, a);
        }
        else if(op == 'A'){
            int a = nextnum();
            printnum(access(T, a));
            printch('\n');
        }
        else if(op == 'R'){
            int a = nextnum(), b = nextnum();
            T = rotate(T, a, b);
        }
        else{
            int a = nextnum(), b = nextnum();
            T = erase(T, a, b);
        }
    }
    order(T);
    if(pbufw)
        fwrite(bufw, 1, pbufw, fo);

    fclose(fi);
    fclose(fo);
    return 0;
}