Cod sursa(job #1207213)

Utilizator gabrielinelusGabriel-Robert Inelus gabrielinelus Data 12 iulie 2014 15:50:02
Problema Hashuri Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 3.07 kb
#include <cstdio>
#include <algorithm>

using namespace std;

class AVL{
public:
    int value,height;
    AVL *st,*dr;
    AVL(int value,int height,AVL *st,AVL *dr){
        this->value = value;
        this->height = height;
        this->st = st;
        this->dr = dr;
    }
}*root,*nil;

void init(){
    root = nil = new AVL(0,0,NULL,NULL);
}

void Rotate_left(AVL *&n)
{
    AVL *aux = n->st;
    n->st = aux->dr;
    n -> height = max(n->st->height,n->dr->height)+1;
    aux->dr = n;
    aux->height = max(aux->st->height,aux->dr->height) + 1;
    n = aux;
}
void Rotate_right(AVL *&n)
{
    AVL *aux = n->dr;

    n->dr = aux->st;
    n->height = max(n->st->height,n->dr->height)+1;
    aux->st = n;
    aux->height = max(aux->st->height,aux->dr->height) + 1;
    n = aux;
}

void Balance(AVL *&n)
{
    if(n->st->height > n->dr->height + 1)
        Rotate_left(n);
    else
        if(n->st->height + 1 < n->dr->height )
            Rotate_right(n);
    n->height = max(n->st->height,n->dr->height) + 1;
}

void Delete(int value,AVL *&n)
{
    if(n == nil) return;
    if(n->value > value)
        Delete(value,n->st);
    else
        if(n->value < value)
            Delete(value,n->dr);
        else
        {
            if(n->dr != nil)
            {
                Rotate_right(n);
                Delete(value,n);
            }
            else
                if(n->st != nil)
                {
                    Rotate_left(n);
                    Delete(value,n);
                }
                else
                {
                    delete n; n = nil;
                    return;
                }
        }
    Balance(n);
}

void Insert(int value,AVL *&n)
{
    if(n == nil){
        n = new AVL(value,1,nil,nil);
        return;
    }
    if(n->value > value)
        Insert(value,n->st);
    else if(n->value < value)
            Insert(value,n->dr);
         else return; /// exista deja
    Balance(n);
}

void srd(AVL *nod){
    if(nod == nil) return;
    srd(nod->st);
    printf("%d ",nod->value);
    srd(nod->dr);
}
int Search(int value,AVL *nod){
    if(nod == nil) return 0;
    if(nod->value == value) return 1;
    if(nod->value > value)
        return Search(value,nod->st);
    return Search(value,nod->dr);
}
#define DIM 666013
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);
    init();
    int N,op,x;
    scan(N);
    for(int i = 1; i <= N; ++i)
    {
        scan(op);scan(x);
        if(op == 1)
            Insert(x,root);
        if(op == 2)
            Delete(x,root);
        if(op == 3)
            printf("%d\n",Search(x,root));
    }
    return 0;
}