Pagini recente » Cod sursa (job #816178) | Cod sursa (job #3174794) | Cod sursa (job #1492877) | Cod sursa (job #1280060) | Cod sursa (job #2404152)
#include <iostream>
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int MAX_HEAP_SIZE = 10000;
typedef struct {
int H[MAX_HEAP_SIZE];
int N = 0;
inline int father(int nod) {
return nod >> 1;
}
inline int son_left(int nod) {
return nod << 1;
}
inline int son_right(int nod) {
return (nod << 1) + 1;
}
void sift_down(int K) {
int son;
do {
son = 0;
if (son_left(K) <= N) {
son = son_left(K);
if(son_right(K) <= N && H[son_right(K)] < H[son])
son = son_right(K);
}
if(H[son] >= H[K])
son = 0;
if(son) {
swap(H[K], H[son]);
K = son;
}
} while(son);
}
void sift_up(int K) {
int key = H[K];
while(K > 0 && key < H[father(K)]) {
H[K] = H[father(K)];
K = father(K);
}
H[K] = key;
}
void insert(int key) {
H[++N] = key;
sift_up(N);
}
void cut(int K) { //Al catelea nod sa fie taiat
H[K] = H[N];
N--;
if(K > 0 && H[K] < H[father(K)])
sift_up(K);
else
sift_down(K);
}
void heapify()
{
for(int i=N/2;i>0;i--)
sift_down(i);
}
void afis() {
for(int i = 1; i <= N; i++)
cout << H[i] << " ";
cout << endl;
}
int min() {
return H[1];
}
int search(int Key) {
for(int i = 1; i <= N; i++)
if(H[i] == Key)
return i;
}
void add(int key)
{
H[++N]=key;
}
} Heap;
int main() {
Heap X;
int A[200001];
int j = 0;
int N, tip, x;
in >> N;
for(int i = 1; i <= N; i++) {
in >> tip;
if(tip == 1) {
in >> x;
X.add(x);
++j;
A[j] = x;
} else if(tip == 2) {
in >> x;
X.cut(X.search(A[x]));
} else {X.heapify();
out << X.min() << endl;
}
}
return 0;
}