Cod sursa(job #1923295)

Utilizator Andrei1998Andrei Constantinescu Andrei1998 Data 10 martie 2017 22:08:06
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 4.69 kb
#include <iostream>
#include <cstdlib>
#include <cctype>
#include <ctime>
#include <utility>
#include <random>
#include <chrono>

using namespace std;
using namespace chrono;

class InputReader {
public:
    InputReader(const char *file_name) {
        in = fopen(file_name, "r");
        cursor = 0;
        fread(buffer, 1, SIZE, in);
    }

    template <typename T>
    InputReader& operator>>(T &nr) {
        while (!isdigit(buffer[cursor]))
            advance();

        nr = 0;
        while (isdigit(buffer[cursor])) {
            nr *= 10;
            nr += buffer[cursor] - '0';
            advance();
        }
        return (*this);
    }

private:
    FILE *in;
    static const int SIZE = (1 << 17);
    int cursor;
    char buffer[SIZE];

    void advance() {
        ++ cursor;
        if (cursor == SIZE) {
            cursor = 0;
            fread(buffer, 1, SIZE, in);
        }
    }
};

class OutputWriter {
public:
    OutputWriter(const char *file_name) {
        out = fopen(file_name, "w");
        cursor = 0;
    }

    ~OutputWriter() {
        flush();
        fclose(out);
    }

    OutputWriter& operator<<(int nr) {
        int digits[10];
        int cnt = 0;
        do {
            digits[cnt ++] = nr % 10;
            nr /= 10;
        } while (nr);
        for (int i = cnt - 1; i >= 0; -- i)
            add(digits[i] + '0');
        return (*this);
    }

    OutputWriter& operator<<(char ch) {
        add(ch);
        return (*this);
    }

    void add(char ch) {
        if (cursor == SIZE)
            flush();
        buffer[cursor ++] = ch;
    }

    void flush() {
        fwrite(buffer, 1, cursor, out);
        cursor = 0;
    }
private:
    FILE *out;
    static const int SIZE = (1 << 17);
    int cursor;
    char buffer[SIZE];
};

struct Treap {
    int key, pr;

    Treap *left, *right;
    Treap(int _key = 0, int _pr = 0, Treap *_left = NULL, Treap *_right = NULL):
        key(_key), pr(_pr), left(_left), right(_right) {}
} *nil, *root;

pair <Treap*, Treap*> split(Treap *t, int key) {
    if (t == nil)
        return make_pair(nil, nil);

    pair <Treap*, Treap*> aux;
    if (key < t -> key) {
        aux = split(t -> left, key);
        t -> left = aux.second;
        aux.second = t;
    }
    else {
        aux = split(t -> right, key);
        t -> right = aux.first;
        aux.first = t;
    }
    return aux;
}

Treap* join(Treap *l, Treap *r) {
    if (l == nil)
        return r;
    if (r == nil)
        return l;

    if (l -> pr > r -> pr) {
        l -> right = join(l -> right, r);
        return l;
    }
    else {
        r -> left = join(l, r -> left);
        return r;
    }
}

void insert(Treap *&t, int key, int pr) {
    if (pr > t -> pr) {
        pair <Treap*, Treap*> aux = split(t, key);
        t = new Treap(key, pr, aux.first, aux.second);
    }
    else if (key < t -> key)
        insert(t -> left, key, pr);
    else
        insert(t -> right, key, pr);
}

void erase(Treap* &t, int key) {
    if (t == nil)
        ;
    else if (key == t -> key) {
        Treap *aux = t;
        t = join(t -> left, t -> right);
        //delete aux;
    }
    else if (key < t -> key)
        erase(t -> left, key);
    else
        erase(t -> right, key);
}

bool find(Treap *t, int key) {
    if (t == nil)
        return false;
    else if (key == t -> key)
        return true;
    else if (key < t -> key)
        return find(t -> left, key);
    else
        return find(t -> right, key);
}

/*void dump(Treap *t) {
    if (t == nil)
        return ;
    dump(t -> left);
    cout << t -> key << ' ';
    dump(t -> right);
}

void Print(Treap *t) {
    cout << "Printing: ";
    dump(t);
    cout << "#" << endl;
}*/

int main()
{
    //freopen("hashuri.in", "r", stdin);
    InputReader cin("hashuri.in");
    OutputWriter cout("hashuri.out");

    auto now = system_clock::now();
    auto duration = now.time_since_epoch();
    srand(duration_cast<milliseconds>(duration).count());

    nil = new Treap(-1, -1);
    nil -> left = nil -> right = nil;
    root = nil;

    int M = 0;
    cin >> M;

    while (M --) {
        int type;
        cin >> type;

        if (type == 1) {
            int key;
            cin >> key;
            int pr = rand();
            insert(root, key, pr);
        }
        else if (type == 2) {
            int key;
            cin >> key;
            erase(root, key);
        }
        else {
            int key;
            cin >> key;
            cout << find(root, key) << '\n';
        }
        //Print(root);
    }
    return 0;
}