Cod sursa(job #795429)

Utilizator a_h1926Heidelbacher Andrei a_h1926 Data 8 octombrie 2012 19:51:18
Problema Secv8 Scor 25
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.62 kb
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <cassert>

using namespace std;

const int oo = 1000000000;

struct Treap {
    int Key, Priority, Value, Rev;
    Treap *Left, *Right;

    Treap(int Key, int Priority, int Value, Treap *Left, Treap *Right) {
        this->Key = Key, this->Priority = Priority, this->Value = Value;
        this->Left = Left, this->Right = Right;
        Rev = 0;
    }
};

Treap *R, *NIL;

inline void ReverseNode(Treap* &Node, int Depth) {
    if (Node == NIL || !Depth)
        return;
    if (Node->Left != NIL)
        Node->Left->Rev ^= Node->Rev;
    if (Node->Right != NIL)
        Node->Right->Rev ^= Node->Rev;
    if (Node->Rev == 1)
        swap(Node->Left, Node->Right), Node->Rev ^= 1;
    ReverseNode(Node->Left, Depth - 1), ReverseNode(Node->Right, Depth - 1);
}

inline void Update(Treap* &Node) {
    if (Node == NIL)
        return;
    Node->Key = Node->Left->Key + 1 + Node->Right->Key;
}

inline void RotLeft(Treap* &Node) {
    Treap *Aux = Node->Right;
    Node->Right = Node->Right->Left;
    Aux->Left = Node;
    Node = Aux;
    Update(Node->Left); Update(Node);
}

inline void RotRight(Treap* &Node) {
    Treap *Aux = Node->Left;
    Node->Left = Node->Left->Right;
    Aux->Right = Node;
    Node = Aux;
    Update(Node->Right); Update(Node);
}

inline void Balance(Treap* &Node) {
    if (Node->Left->Priority > Node->Priority)
        RotRight(Node);
    else if (Node->Right->Priority > Node->Priority)
        RotLeft(Node);
}

void Insert(Treap* &Node, int Key, int Priority, int Value) {
    if (Node == NIL) {
        Node = new Treap(1, Priority, Value, NIL, NIL);
        return;
    }
    ReverseNode(Node, 2);
    if (Key <= Node->Left->Key + 1)
        Insert(Node->Left, Key, Priority, Value);
    else
        Insert(Node->Right, Key - Node->Left->Key - 1, Priority, Value);
    Update(Node); Balance(Node);
}

void Erase(Treap* &Node, int Key) {
    if (Node == NIL)
        return;
    ReverseNode(Node, 2);
    if (Node->Left->Key + 1 == Key) {
        if (Node->Left == NIL && Node->Right == NIL) {
            delete Node; Node = NIL;
        }
        else {
            if (Node->Left->Priority > Node->Right->Priority)
                RotRight(Node);
            else
                RotLeft(Node);
            Erase(Node, Key);
        }
    }
    else {
        if (Key <= Node->Left->Key)
            Erase(Node->Left, Key);
        else
            Erase(Node->Right, Key - Node->Left->Key - 1);
    }
    Update(Node);
}

int Acces(Treap* &Node, int Key) {
    if (Node == NIL || Node->Left->Key + 1 == Key)
        return Node->Value;
    if (Key <= Node->Left->Key)
        return Acces(Node->Left, Key);
    else
        return Acces(Node->Right, Key - Node->Left->Key - 1);
}

inline Treap* Join(Treap* &Left, Treap* &Right) {
    Treap* NewR = new Treap(Left->Key + 1 + Right->Key, oo, 0, Left, Right);
    Erase(NewR, Left->Key + 1);
    return NewR;
}

inline void Split(Treap* &Root, Treap* &NewRoot, int Key) {
    Insert(Root, Key, oo, 0);
    Treap* Aux = Root;
    NewRoot = Root->Right;
    Root = Root->Left;
    delete Aux;
}

inline void Erase(Treap* &Root, int MinKey, int MaxKey) {
    Treap *A = Root, *B, *C;
    Split(A, B, MinKey); Split(B, C, MaxKey + 1 - MinKey + 1);
    Root = Join(A, C);
}

inline void Reverse(Treap* &Root, int MinKey, int MaxKey) {
    Treap *A = Root, *B, *C;
    Split(A, B, MinKey); Split(B, C, MaxKey + 1 - MinKey + 1);
    B->Rev ^= 1;
    Root = Join(A, B); Root = Join(R, C);
}

void Inorder(Treap* &Node) {
    if (Node == NIL)
        return;
    ReverseNode(Node, 2);
    Inorder(Node->Left); printf("%d ", Node->Value); Inorder(Node->Right);
}

void Initialize() {
    srand(time(0));
    R = NIL = new Treap(0, 0, 0, NULL, NULL);
}

int main() {
    Initialize();
    assert(freopen("secv8.in", "r", stdin));
    assert(freopen("secv8.out", "w", stdout));
    int M, Rev; assert(scanf("%d %d", &M, &Rev) == 2);
    for (; M > 0; --M) {
        char Type; assert(scanf("\n%c", &Type) == 1);
        if (Type == 'I') {
            int Key, X; assert(scanf(" %d %d", &Key, &X) == 2);
            Insert(R, Key, 1 + rand() % (oo - 1), X);
        }
        if (Type == 'R') {
            int MinKey, MaxKey; assert(scanf(" %d %d", &MinKey, &MaxKey) == 2);
            Reverse(R, MinKey, MaxKey);
        }
        if (Type == 'D') {
            int MinKey, MaxKey; assert(scanf(" %d %d", &MinKey, &MaxKey) == 2);
            Erase(R, MinKey, MaxKey);
        }
        if (Type == 'A') {
            int Key; assert(scanf(" %d", &Key) == 1);
            printf("%d\n", Acces(R, Key));
        }
    }
    Inorder(R); printf("\n");
    return 0;
}