Pagini recente » Cod sursa (job #1742940) | Cod sursa (job #465099) | Cod sursa (job #490948) | Cod sursa (job #282428) | Cod sursa (job #3131454)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int dim = 1000000;
int value[dim], Time;
int heap[dim], len;
int pos[dim];
void UpHeap(int child) {
while (child / 2) {
int parent = child / 2;
if (value[heap[parent]] > value[heap[child]]) {
swap(heap[parent], heap[child]);
pos[heap[parent]] = parent;
pos[heap[child]] = child;
child = parent;
}
else {
return;
}
}
}
void DownHeap(int parent) {
while (parent * 2 <= len) {
int lChild = parent * 2, rChild = parent * 2 + 1, child = lChild;
if (rChild <= len && value[heap[lChild]] > value[heap[rChild]] && value[heap[parent]] > value[heap[rChild]]) {
child = rChild;
}
if (value[heap[parent]] > value[heap[child]]) {
swap(heap[parent], heap[child]);
pos[heap[parent]] = parent;
pos[heap[child]] = child;
parent = child;
}
else {
return;
}
}
}
int main() {
int nrOp;
fin >> nrOp;
while (nrOp--) {
int op;
fin >> op;
if (op == 1) {
int x;
fin >> x;
value[++Time] = x;
heap[++len] = Time;
pos[Time] = len;
UpHeap(len);
}
if (op == 2) {
int x;
fin >> x;
int ind = pos[x];
swap(heap[pos[x]], heap[len]);
pos[heap[pos[x]]] = pos[x];
pos[heap[len]] = len;
len--;
if (ind > 1 && value[heap[ind]] < value[heap[ind / 2]]) {
UpHeap(ind);
}
else {
DownHeap(ind);
}
}
if (op == 3) {
fout << value[heap[1]] << '\n';
}
}
fin.close();
fout.close();
return 0;
}