Cod sursa(job #2416446)

Utilizator PopoviciRobertPopovici Robert PopoviciRobert Data 27 aprilie 2019 16:14:21
Problema Secv8 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 4.28 kb
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44

using namespace std;

typedef struct Node *T;
typedef pair <T, T> PTT;

T nil;

struct Node {

    Node *left, *right;

    int val, prio, sz;
    bool lazy;

    Node(int _val) {
        left = right = nil;
        val = _val, prio = rand(), sz = 1;
        lazy = 0;
    }

};


class Treap {

private:

    T root;

    inline void solve_lazy(T nod) {

        if(nod == nil) return ;

        if(nod -> lazy) {
            swap(nod -> left, nod -> right);
            nod -> left -> lazy ^= 1;
            nod -> right -> lazy ^= 1;
        }

        nod -> lazy = 0;

    }

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

    T join(T a, T b) {

        if(a == nil) {
            return b;
        }
        if(b == nil) {
            return a;
        }
        solve_lazy(a), solve_lazy(b);

        if(a -> prio > b -> prio) {

            a -> right = join(a -> right, b);
            refresh(a);
            return a;

        }
        else {

            b -> left = join(a, b -> left);
            refresh(b);
            return b;

        }

    }

    PTT split(T nod, int pos) {

        if(nod == nil) {
            return {nil, nil};
        }
        solve_lazy(nod);

        if(nod -> left -> sz >= pos) {

            PTT aux = split(nod -> left, pos);
            nod -> left = aux.second;
            refresh(nod);
            return {aux.first, nod};

        }
        else {

            PTT aux = split(nod -> right, pos - nod -> left -> sz - 1);
            nod -> right = aux.first;
            refresh(nod);
            return {nod, aux.second};

        }

    }

    inline T ins(T nod, int pos, int val) {

        PTT aux = split(nod, pos - 1);
        T cur = new Node(val);
        return join(join(aux.first, cur), aux.second);

    }

    inline int get(T nod, int pos) {

        solve_lazy(nod);

        if(nod -> left -> sz >= pos) {
            return get(nod -> left, pos);
        }

        if(nod -> left -> sz + 1 == pos) {
            return nod -> val;
        }

        return get(nod -> right, pos - nod -> left -> sz - 1);

    }

    void dfs(T nod, vector <int> &sol) {

        if(nod == nil) {
            return ;
        }

        solve_lazy(nod);

        dfs(nod -> left, sol);
        sol.push_back(nod -> val);
        dfs(nod -> right, sol);

    }

public:


    inline void ins(int pos, int val) {
        root = ins(root, pos, val);
    }

    inline int get(int pos) {
        return get(root, pos);
    }

    inline void rev(int l, int r) {

        PTT aux1 = split(root, l - 1);
        PTT aux2 = split(aux1.second, r - l + 1);

        aux2.first -> lazy ^= 1;

        root = join(join(aux1.first, aux2.first), aux2.second);

    }

    inline void del(int l, int r) {

        PTT aux1 = split(root, l - 1);
        PTT aux2 = split(aux1.second, r - l + 1);

        root = join(aux1.first, aux2.second);

    }

    inline void print(vector <int> &sol) {
        dfs(root, sol);
    }

    inline void init() {
        root = nil;
    }

};


int main() {
    ifstream cin("secv8.in");
    ofstream cout("secv8.out");
    int q, t;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    srand(time(NULL));

    nil = new Node(0);
    nil -> left = nil -> right = NULL;
    nil -> sz = 0;

    cin >> q >> t;

    Treap myT; myT.init();

    while(q > 0) {
        q--;

        char ch;
        int l, r, pos, val;

        cin >> ch;

        if(ch == 'I') {
            cin >> pos >> val;
            myT.ins(pos, val);
        }

        if(ch == 'A') {
            cin >> pos;
            cout << myT.get(pos) << "\n";
        }

        if(ch == 'R') {
            cin >> l >> r;
            myT.rev(l, r);
        }

        if(ch == 'D') {
            cin >> l >> r;
            myT.del(l, r);
        }

    }

    vector <int> sol;
    myT.print(sol);

    for(auto it : sol) {
        cout << it << " ";
    }

    cin.close();
    cout.close();
    return 0;
}