Cod sursa(job #1737027)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 3 august 2016 04:39:29
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.29 kb
#include <bits/stdc++.h>

#define INF 0x3f3f3f3f
using namespace std;

class Treap{
public:
    Treap *st,*dr;
    int info, priority, weight;
    Treap(){
        st = dr = 0;
        info = priority = weight = 0;
    }
    Treap(Treap *left, Treap *right, int priority,int info, int weight) {
        this->st = left;
        this->dr = right;
        this->priority = priority;
        this->info = info;
        this->weight = weight;
    }
}*Root, *Neal;

void update(Treap *&T){
    T->weight = T->st->weight + T->dr->weight + 1;
}

void rotateLeft(Treap *&T){
    Treap *aux = T->dr;
    T->dr = aux->st;
    aux->st = T;
    T = aux;

    update(T->st);
    update(T);
}

void rotateRight(Treap *&T){
    Treap *aux = T->st;
    T->st = aux->dr;
    aux->dr = T;
    T = aux;

    update(T->dr);
    update(T);
}

void balance(Treap *&T){
    if(T->priority < T->st->priority)
        rotateRight(T);
    else
        if(T->priority < T->dr->priority)
            rotateLeft(T);
}

bool isIn(Treap *T, int k){
    if(T == Neal) /// :D
        return false;
    if(T->info == k)
        return true;
    if(k < T->info)
        return isIn(T->st, k);
    return isIn(T->dr,k);
}

int Kth(Treap *T,int k){
    if(k - T->st->weight == 1)
        return T->info;
    if(T->st->weight < k)
        return Kth(T->dr, k - T->st->weight - 1);
    if(T->st->weight >= k)
        return Kth(T->st, k);

}

void Insert(Treap *&T, int k, int priority){
    if(T == Neal){
        T = new Treap(Neal, Neal, priority, k, 1);
        return;
    }
    if(k < T->info)
        Insert(T->st, k, priority);
    else
        Insert(T->dr, k, priority);
    T->weight += 1;
    balance(T);
}

void Delete(Treap *&T, int k){
    if(k < T->info)
        Delete(T->st, k);
    else
        if(k > T->info)
            Delete(T->dr, k);
    else{
        if(T->st == Neal && T->dr == Neal){
            delete T;
            T = Neal;
            return;
        }
        if(T->st->priority > T->dr->priority) {
            rotateRight(T);
            Delete(T->dr, k);
        }
        else {
            rotateLeft(T);
            Delete(T->st, k);
        }
    }
}

#define DIM 66613
char buffer[DIM];
int poz = DIM - 1;

void scan(int &A)
{
    A = 0;
    while('0' > buffer[poz] || buffer[poz] > '9')
        if(++poz == DIM) fread(buffer,1,DIM,stdin),poz = 0;
    while('0' <= buffer[poz] && buffer[poz] <= '9')
    {
        A = A * 10 + buffer[poz] - 48;
        if(++poz == DIM)
            fread(buffer,1,DIM,stdin),poz = 0;
    }
}



int main()
{
    freopen("hashuri.in","r",stdin);
    freopen("hashuri.out","w",stdout);
    cin.sync_with_stdio(false);

    srand(time(0));
    Neal = new Treap(0, 0, -INF, -INF, 0);
    Neal->st = Neal;
    Neal->dr = Neal;

    Root = Neal;
    int N,op,x;
    scan(N);
    for(int i = 0; i < N; ++i){
        scan(op);scan(x);
        if(op == 1){
            if(isIn(Root, x))
                continue;
            Insert(Root, x, rand());
            continue;
        }
        if(op == 2){
            if(!isIn(Root, x))
                continue;
            Delete(Root, x);
            continue;
        }
        printf("%d\n",isIn(Root, x));
    }

    return 0;
}