Pagini recente » Cod sursa (job #2619756) | Cod sursa (job #607245) | Cod sursa (job #2366556) | Cod sursa (job #1989203) | Cod sursa (job #2639203)
/*
H[] - heap cu indicii elementelor (memorate in v)
P[] - P[i] reprezinta pozitia elem cu indicele in in heap */
#include <iostream>
#include <fstream>
#define DIM 200010
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int N, heap_length;
int v[DIM], H[DIM], P[DIM];
int father(int k){
return k/2;
}
int left_son(int k){
return 2*k;
}
int right_son(int k){
return 2*k+1;
}
void upheap(int H[], int k){
while(k > 1 && v[H[father(k)]] > v[H[k]]){
swap(H[father(k)], H[k]);
swap(P[H[father(k)]],P[H[k]]);
k = father(k);
}
}
void sift(int H[], int k){
int son = 0;
do{
if(left_son(k) <= heap_length)
son = left_son(k);
if(right_son(k) <= heap_length && v[H[right_son(k)]] < v[H[left_son(k)]])
son = right_son(k);
if(v[H[k]] <= v[H[son]])
son = 0;
if(son){
swap(H[k], H[son]);
swap(P[H[k]], P[H[son]]);
k = son;
}
}
while(son);
}
void op1(int x){
v[++v[0]] = x;
H[++heap_length] = v[0];
P[H[heap_length]] = heap_length;
upheap(H,heap_length);
}
void op2(int x){
v[x] = -1;
H[P[x]] = H[heap_length];
P[H[heap_length]] = P[x];
heap_length--;
if(P[x] > 1 && v[H[P[x]]] < v[H[father(P[x])]])
upheap(H, P[x]);
else
sift(H, P[x]);
}
int op3(){
return v[H[1]];
}
int main()
{
f>>N;
int op,x;
for(int i=1; i<=N; i++){
f>>op;
if(op == 1 || op == 2){
f>>x;
if(op == 1)
op1(x);
else
op2(x);
}
else
g<<op3()<<"\n";
}
}