Pagini recente » Cod sursa (job #67716) | Cod sursa (job #2126386) | Cod sursa (job #2619628) | Cod sursa (job #2511469) | Cod sursa (job #1816539)
// heapuri.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <vector>
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int N;
int heap_size;
int heap[200005];
int id_to_pos[200005];
int v[200005];
void heap_swap(int idx1, int idx2) {
int id1 = heap[idx1];
int id2 = heap[idx2];
heap[idx1] = id2;
heap[idx2] = id1;
id_to_pos[id1] = idx2;
id_to_pos[id2] = idx1;
}
void pushUp(int idx) {
while (idx > 1 && v[heap[idx]] < v[heap[idx / 2]]) {
heap_swap(idx, idx / 2);
idx = idx / 2;
}
}
void pushDown(int idx) {
int minIdx = idx;
if (2 * idx <= heap_size && v[heap[2 * idx]] < v[heap[idx]]) {
minIdx = 2 * idx;
}
if (2 * idx + 1 <= heap_size && v[heap[2 * idx + 1]] < v[heap[minIdx]]) {
minIdx = 2 * idx + 1;
}
if (minIdx != idx) {
heap_swap(minIdx, idx);
pushDown(minIdx);
}
}
void insert(int value, int id) {
heap[++heap_size] = id;
id_to_pos[id] = heap_size;
pushUp(heap_size);
}
void remove(int id) {
int posToDelete = id_to_pos[id];
id_to_pos[id] = 0;
heap_swap(posToDelete, heap_size--);
pushDown(posToDelete);
}
int main()
{
fin >> N;
int nodeCount = 0;
for (int i = 0; i < N; ++i) {
int op, val;
fin >> op;
if (op == 3) {
fout << v[heap[1]] << "\n";
}
else {
fin >> val;
if (op == 1) {
v[++nodeCount] = val;
insert(val, nodeCount);
}
else {
remove(val);
}
}
}
return 0;
}