Pagini recente » Cod sursa (job #3216631) | Cod sursa (job #1958748) | Cod sursa (job #15852) | Cod sursa (job #462887) | Cod sursa (job #2047812)
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
const int Nmax = 200001;
int n, lg, v[Nmax], poz[Nmax], heap[Nmax];
int father(int k) {
return k / 2;
}
int left_son(int k) {
return k * 2;
}
int right_son(int k) {
return k * 2 + 1;
}
void add(int Heap[Nmax], int n, int k) {
while(k > 1 && heap[k] < heap[father(k)]) {
swap(heap[k], heap[father(k)]);
poz[heap[k]] = k;
poz[heap[father(k)]] = father(k);
k = father(k);
}
}
void sterge(int Heap[Nmax], int n, int k) {
if(father(k) >= 1 && Heap[k] < Heap[father(k)])
add(Heap, n, k);
else {
int son;
do {
son = 0;
if(left_son(k) <= n) {
son = left_son(k);
if(right_son(k) <= n && right_son(k) < son)
son = right_son(k);
}
if(son) {
swap(Heap[k], Heap[son]);
poz[Heap[k]] = k;
poz[Heap[son]] = son;
k = son;
}
}while(son);
}
}
int main()
{
int i, x, val, pozitie;
f>>n;
for(i = 1; i <= n; i++) {
f>>x;
if(x == 1 || x == 2) f>>val;
if(x == 1) {
v[++lg] = heap[lg] = val; poz[heap[lg]] = lg;
add(heap, lg, lg);
} else if(x == 2) {
pozitie = poz[v[val]];
heap[pozitie] = heap[lg];
poz[heap[pozitie]] = pozitie;
heap[lg--] = 0;
sterge(heap, lg, pozitie);
} else
g<<heap[1]<<'\n';
}
return 0;
}