Pagini recente » Arhiva de probleme | Cod sursa (job #1544771) | Diferente pentru documentatie/rating intre reviziile 24 si 20 | Arbori de intervale si aplicatii in geometria computationala | Cod sursa (job #1701445)
#include <fstream>
#include <cstdlib>
using namespace std;
const int N = 1 + 2e5, inf = 0x3f3f3f3f;
struct Nod{
int val, P;
Nod *st, *dr;
Nod() : val(0), P(0) {
st = dr = this;
}
Nod(int val, int P, Nod* st, Nod* dr) : val(val), P(P), st(st), dr(dr) {}
} *nil = new Nod();
class Treap {
Nod *root;
void rotate_left(Nod* &x){
Nod* y = x -> st;
x -> st = y -> dr;
y -> dr = x;
x = y;
}
void rotate_right(Nod* &x){
Nod* y = x -> dr;
x -> dr = y -> st;
y -> st = x;
x = y;
}
void balance(Nod* &x){
if (x -> st -> P > x -> P)
rotate_left(x);
if (x -> dr -> P > x -> P)
rotate_right(x);
}
Nod* &search(Nod* &p, int x) {
if (p == nil)
return nil;
if (p -> val == x)
return p;
return search(x < p -> val ? p -> st : p -> dr, x);
}
void insert(Nod* &p, Nod* nod) {
if (p == nil) {
p = nod;
return;
}
if (p -> val == nod -> val)
return;
insert(nod -> val <= p -> val ? p -> st : p -> dr, nod);
balance(p);
}
public:
Treap(Nod* root = nil) : root(root) {}
Nod* &search(int x) {
return search(root, x);
}
Nod* find(int x){
Nod* p = search(root, x);
return p != nil ? p : NULL;
}
void insert(int x){
insert(root, new Nod(x, rand() + 1, nil, nil));
}
void erase(Nod* &p){
if (p == nil)
return;
if (p -> st == nil || p -> dr == nil){
Nod* aux = p;
p = (p -> st == nil ? p -> dr : p -> st);
delete aux;
return;
}
if (p -> st -> P > p -> dr -> P){
rotate_left(p);
erase(p -> dr);
} else {
rotate_right(p);
erase(p -> st);
}
balance(p);
}
void erase(int x) {
erase(search(root, x));
}
void join(Treap& A, Treap& B) {
root = new Nod(0, 1, A.root, B.root);
erase(root);
}
void split(Treap& A, Treap& B, int prag) {
insert(root, new Nod(prag, inf, nil, nil));
A.root = root -> st;
B.root = root -> dr;
}
int min(){
Nod* p = root;
while (p -> st != nil)
p = p -> st;
return p -> val;
}
};
int v[N];
int main(){
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int n, t, x;
Treap T;
in >> n;
while (n--){
in >> t;
if ( t != 3 ) in >> x;
if (t == 1) {
T.insert(x);
v[++v[0]] = x;
} else if ( t == 2 )
T.erase( v[x] );
else
out << T.min() << '\n';
}
return 0;
}