Pagini recente » Profil dornescuvlad | Cod sursa (job #442024) | Profil dornescuvlad | Cod sursa (job #95871) | Cod sursa (job #1672914)
#include<bits/stdc++.h>
#define L(x) T[(x)].ls
#define Ln(x) T[T[(x)].ls]
#define R(x) T[(x)].rs
#define Rn(x) T[T[(x)].ls]
#define W(x) T[(x)].w
#define S(x) T[(x)].size
using namespace std;
const int MAXN=1000000;
const int INF=2147483647;
const int NINF=-1<<31;
const int ERR=-1;
int root=0;
int sz,n;
struct Node{
int size,ls,rs,w;
Node():size(0),ls(0),rs(0),w(0) {}
};
Node T[MAXN*3];
void RR(int &s) {
int k=L(s);
L(s)=R(k);
R(k)=s;
S(k)=S(s);
S(s)=S(L(s))+S(R(s))+1;
s=k;
}
void LR(int &s) {
int k=R(s);
R(s)=L(k);
L(k)=s;
S(k)=S(s);
S(s)=S(L(s))+S(R(s))+1;
s=k;
}
void Maintain(int &s,bool op) { //if op opt R(s)
if (s == 0)return;
if(!op) {
if(S(R(s))<S(L(L(s))))RR(s);
else if(S(R(s))<S(R(L(s)))) {
LR(L(s));
RR(s);
} else return;
} else {
if(S(L(s))<S(R(R(s))))LR(s);
else if(S(L(s))<S(L(R(s)))) {
RR(R(s));
LR(s);
} else return;
}
Maintain(L(s),false);
Maintain(R(s),true);
Maintain(s,false);
Maintain(s,true);
}
void Insert(int &s,int v) {
if(s==0) {
s=++sz;
W(s)=v;
S(s)=1;
return;
}
S(s)++;
if(W(s)>v) Insert(L(s),v);
else Insert(R(s),v);
Maintain(s,(v>=W(s)));
}
int Delete(int &s,int v){
S(s)--;
if( (v==W(s)) || (v<W(s)&&L(s)==0)||(v>W(s)&&R(s)==0) ){
int ans=W(s);
if(!(L(s)*R(s)))s=L(s)+R(s);
else W(s)=Delete(L(s),W(s)+1);
return ans;
} else {
if(W(s)>v) return Delete(L(s),v);
return Delete(R(s),v);
}
}
int Search(int s,int v) {
if(s==0)return ERR;
if(W(s)==v)return s;
if(W(s)>v)return Search(L(s),v);
return Search(R(s),v);
}
int Select(int s,int k) {
int Lt=S(L(s));
if(k==Lt) return W(s);
if(Lt>k) return Select(L(s),k);
return Select(R(s),k-Lt-1);
}
int Rank(int s,int v) {
int Lt=S(L(s));
if(W(s)==v)return Lt;
if(W(s)>v) return Rank(L(s),v);
return Rank(R(s),v)+Lt+1;
}
#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();
while (T--) {
int op = readInt(), x = readInt();
if (op == 1) {
if (Search(root, x) == ERR) {
Insert(root, x);
}
} else if (op == 2) {
if (Search(root, x) != ERR) {
Delete(root, x);
}
} else {
putchar((Search(root, x) != ERR) + '0');
putchar('\n');
}
}
return 0;
}