Pagini recente » Cod sursa (job #3041505) | Cod sursa (job #647020) | oji2015 | Cod sursa (job #1858937) | Cod sursa (job #1376573)
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int kNMax = 200010;
int n, lungime, heap[kNMax], val[kNMax], poz[kNMax];
void UpHeap(int nod) {
while (nod / 2 && val[heap[nod / 2]] > val[heap[nod]]) {
swap(heap[nod], heap[nod / 2]);
poz[heap[nod]] = nod;
poz[heap[nod / 2]] = nod / 2;
nod /= 2;
}
}
void DownHeap(int nod) {
int k = 0;
while (nod != k) {
k = nod;
if (2 * k <= lungime && val[heap[nod]] > val[heap[2 * k]])
nod = 2 * k;
if (2 * k + 1 <= lungime && val[heap[nod]] > val[heap[2 * k + 1]])
nod = 2 * k + 1;
swap(heap[nod], heap[k]);
poz[heap[nod]] = nod;
poz[heap[k]] = k;
}
}
int main() {
int m, tip, x;
in >> m;
while (m--) {
in >> tip;
if (tip == 1) {
in >> x;
val[++n] = x;
heap[++lungime] = n;
poz[n] = lungime;
UpHeap(lungime);
}
if (tip == 2) {
in >> x;
val[x] = -1;
UpHeap(poz[x]);
poz[heap[1]] = 0;
heap[1] = heap[lungime--];
poz[heap[1]] = 1;
DownHeap(1);
}
if(tip == 3)
out << val[heap[1]] << '\n';
}
in.close();
out.close();
return 0;
}