Cod sursa(job #1672926)

Utilizator Ionut.popescuLiviu Rebreanu Ionut.popescu Data 3 aprilie 2016 12:01:22
Problema Hashuri Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 3.05 kb
#include<bits/stdc++.h>

const int inf=~0U>>1;
struct node
{
	int key,size;
	node*c[2];
	node(int _key,int _size,node*_c)
	:key(_key),size(_size)
	{c[0]=c[1]=_c;}
	void rz(){size=c[0]->size+c[1]->size+1;}
}TNull(0,0,0),*Null=&TNull;
class SBT
{
	node*root;
	int top;
	void rot(node*&t,bool d)
	{
		node*p=t->c[d];
		t->c[d]=p->c[!d];
		p->c[!d]=t;
		t->rz();p->rz();
		t=p;
	}
	void maintain(node*&t,bool d)
	{
		if(t==Null) return;
		node*p=t->c[d];
		if(p->c[d]->size>t->c[!d]->size)
			rot(t,d);
		else if(p->c[!d]->size>t->c[!d]->size)
			{
				rot(p,!d);
				rot(t,d);
			}
		else return;
		maintain(t->c[0],0);
		maintain(t->c[1],1);
		maintain(t,0);
		maintain(t,1);
	}
	void insert(node*&t,int x)
	{
		if(t==Null)
		{t=new node(x,1,Null);return;}
		if(t->key==x) return;
		bool d=x>t->key;
		insert(t->c[d],x);
		maintain(t,d);
		t->rz();
	}
	void remove(node*&t,int x)
	{
		if(t==Null) return;
		int d;
		if(t->key==x)
		{
			if(t->c[1]==Null)
			{delete t;t=t->c[0];return;}
			if(t->c[0]==Null)
			{delete t;t=t->c[1];return;}
			node*p=t->c[1];while(p->c[0]!=Null)p=p->c[0];
			t->key=p->key;
			remove(t->c[1],p->key);d=1;
		}
		else
		{
			d=x>t->key;
			remove(t->c[d],x);
		}
		maintain(t,1-d);
		t->rz();
	}
	int select(node*t,int k)
	{
		int r=t->c[0]->size;
		if(k==r) return t->key;
		if(k<r) return select(t->c[0],k);
		return select(t->c[1],k-r-1);
	}
	int rank(node*t,int x)
	{
		int r=t->c[0]->size;
		if(x==t->key) return r;
		if(x<t->key) return rank(t->c[0],x);
		return r+1+rank(t->c[1],x);
	}
	bool search(node*t, int x)
	{
	    if (t == Null)
	        return false;
        if (t->key == x)
            return true;
        if (t->key > x)
            return search(t->c[0], x);
        return search(t->c[1], x);
	}
	public:
	SBT()
	{
		Null->c[0]=Null->c[1]=Null;
	 	root=Null;
	}
	void Insert(int x)
	{
		insert(root,x);
	}
	bool Search(int x)
	{
	    return search(root, x);
	}
	void Remove(int x)
	{
		remove(root,x);
	}
	int Select(int k)
	{
		if(k>root->size) return inf;
		return select(root,k-1);
	}
	int Rank(int x)
	{
		return rank(root,x);
	}
	int size(){return root->size;}
};

#define BUFF 252587
char buff[BUFF];
int pos = BUFF;

inline char getChar() {
    if (pos == BUFF) {
        fread(buff, 1, BUFF, stdin);
        pos = 0;
    }
    return buff[pos++];
}

inline int readInt() {
    int q = 0;
    char c;
    do {
        c = getChar();
    } while (!isdigit(c));
    do {
        q = (10 * q) + (c - '0');
        c = getChar();
    } while (isdigit(c));
    return q;
}

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

    int T = readInt();
    SBT *Q = new SBT;

    while (T--) {
        int op = readInt(), x = readInt();
        if (op == 1) {
            Q->Insert(x);
        } else if (op == 2) {
            Q->Remove(x);
        } else {
            putchar(Q->Search(x) + '0');
            putchar('\n');
        }
    }
	return 0;
}