Pagini recente » Cod sursa (job #1044987) | Cod sursa (job #1392241) | Cod sursa (job #2428193) | Cod sursa (job #737343) | Cod sursa (job #1492257)
#include <fstream>
#define MaxN 200005
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int heap[MaxN], N, heap_size, op, crono_to_poz[MaxN], poz_to_crono[MaxN], crono_count;
void heapswap(int x, int y) {
int temp = heap[x];
heap[x] = heap[y];
heap[y] = temp;
}
void pushUp(int index) {
while (index > 1 && heap[index >> 1] > heap[index]) {
heapswap(index >> 1, index);
int cronoCurrent = poz_to_crono[index];
int cronoParent = poz_to_crono[index >> 1];
poz_to_crono[index] = cronoParent;
poz_to_crono[index >> 1] = cronoCurrent;
crono_to_poz[cronoCurrent] = index >> 1;
crono_to_poz[cronoParent] = index;
index = index >> 1;
}
}
void pushDown(int position) {
int minPosition = position;
if (2 * position <= heap_size && heap[2 * position] < heap[position]) {
minPosition = 2 * position;
}
if (2 * position + 1 <= heap_size && heap[2 * position + 1] < heap[minPosition]) {
minPosition = 2 * position + 1;
}
if (minPosition != position) {
heapswap(position, minPosition);
int cronoCurrent = poz_to_crono[position];
int cronoMin = poz_to_crono[minPosition];
poz_to_crono[position] = cronoMin;
poz_to_crono[minPosition] = cronoCurrent;
crono_to_poz[cronoCurrent] = minPosition;
crono_to_poz[cronoMin] = position;
pushDown(minPosition);
}
}
int main()
{
fin >> N;
for (int n = 1; n <= N; ++n) {
fin >> op;
if (op == 1) {
int x;
fin >> x;
crono_to_poz[++crono_count] = ++heap_size;
poz_to_crono[heap_size] = crono_count;
heap[heap_size] = x;
pushUp(heap_size);
} else if (op == 2) {
int cronoToDelete;
fin >> cronoToDelete;
int posToDelete = crono_to_poz[cronoToDelete];
heapswap(posToDelete, heap_size);
poz_to_crono[posToDelete] = poz_to_crono[heap_size];
crono_to_poz[poz_to_crono[heap_size]] = posToDelete;
--heap_size;
pushDown(posToDelete);
} else {
fout << heap[1] << '\n';
}
}
return 0;
}