Cod sursa(job #2045906)

Utilizator retrogradLucian Bicsi retrograd Data 23 octombrie 2017 00:32:27
Problema Secv8 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.65 kb
#include <bits/stdc++.h>

using namespace std;

namespace Treap {
    struct Node {
        int val, pri;
        int left = 0, right = 0, subsize = 0, lazy = 0;
        Node(int val, int pri) : val(val), pri(pri) {}
    };
    vector<Node> T(1, Node(-1, -1));

    int get_key(int node) { 
        return T[T[node].left].subsize; 
        // return T[node].val; (A)
    }

    int pull(int node) {
        if (node == 0) return 0;

        const int& left = T[node].left;
        const int& right = T[node].right;

        T[node].subsize = T[left].subsize + T[right].subsize + 1;
        return node;
    }

    int push(int node) {
        int& lazy = T[node].lazy;
        if (node == 0 or lazy == 0) return node;

        swap(T[node].left, T[node].right);

        T[T[node].left].lazy ^= lazy;
        T[T[node].right].lazy ^= lazy;
        lazy = 0;
        return node;
    }

    // Splits into < key and >= key
    pair<int, int> Split(int node, int key) {
        push(node);
        if (node == 0) return {0, 0};

        int l, r;
        if (get_key(node) < key) {              /* (B) */
            tie(l, r) = Split(T[node].right, key-get_key(node)-1); 
            T[node].right = l;
            return {pull(node), r};
        } else {
            tie(l, r) = Split(T[node].left, key);
            T[node].left = r;
            return {l, pull(node)};
        }
    }

    // node1 < node2 is REQUIRED
    int Join(int node1, int node2) {
        push(node1); push(node2);
        if (!node1) return node2;
        if (!node2) return node1;

        if (T[node1].pri > T[node2].pri) {
            T[node1].right = Join(T[node1].right, node2);
            return pull(node1);
        } else {
            T[node2].left = Join(node1, T[node2].left);
            return pull(node2);
        }
    }

    int Single(int value) {
        int node = T.size();
        T.push_back(Node(value, rand()));
        return pull(node);
    }

    tuple<int, int, int> Slice(int node, int b, int e) {
        int l, m, r;
        tie(m, r) = Split(node, e);
        tie(l, m) = Split(m, b);
        return make_tuple(l, m, r);
    }

    int Find(int node, int key) {
        int l, m, r;
        tie(l, m, r) = Slice(node, key, key + 1);
        assert(node == Join(l, Join(m, r)));
        return T[m].val;
    }

    int Insert(int node, int key, int value) {
        int l, r, m = Single(value);
        tie(l, r) = Split(node, key);
        return Join(l, Join(m, r));
    }

    int Reverse(int node) {
        T[node].lazy ^= 1;
        return push(node);
    }

    void Dump(int node) {
        push(node);
        if (node == 0) return;
            
        Dump(T[node].left);
        cout << T[node].val << " ";
        Dump(T[node].right);
    }
}

int main() {
    freopen("secv8.in", "r", stdin);
    freopen("secv8.out", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m; cin >> n >> m;
    srand(time(0));

    int treap = 0;
    while (n--) {
        char t; cin >> t;
        if (t == 'I') {
            int k, e; cin >> k >> e;
            treap = Treap::Insert(treap, k - 1, e);
        }
        if (t == 'A') {
            int k; cin >> k;
            cout << Treap::Find(treap, k - 1) << '\n';
        }
        if (t == 'R') {
            int i, j, l, m, r; 
            cin >> i >> j;
            tie(l, m, r) = Treap::Slice(treap, i - 1, j);
            m = Treap::Reverse(m);
            assert(treap == Treap::Join(l, Treap::Join(m, r)));
        }
        if (t == 'D') {
            int i, j, l, m, r; 
            cin >> i >> j;
            tie(l, m, r) = Treap::Slice(treap, i - 1, j);
            treap = Treap::Join(l, r);
        }
    }
    Treap::Dump(treap);
}