Pagini recente » Cod sursa (job #2455938) | Cod sursa (job #1596559) | Cod sursa (job #1202603) | Cod sursa (job #272338) | Cod sursa (job #1447713)
#include <cstdio>
#include <cassert>
#include <algorithm>
using namespace std;
#define _submit
#ifdef _submit
#define InFile "heapuri.in"
#define OtFile "heapuri.ou"
#else
#define InFile "fis.in"
#define OtFile "fis.out"
#endif
#define MAXN 200010
int heap[MAXN], posInHeap[MAXN], elemNr[MAXN];
int heapSz = 0; int nrElem = 0;
void heapInsert(int x) {
int curPos = ++heapSz;
int curElem = ++nrElem;
heap[curPos] = x;
posInHeap[curElem] = curPos;
elemNr[curPos] = curElem;
while (curPos > 1 && (heap[curPos >> 1] > heap[curPos])) {
swap(posInHeap[curElem], posInHeap[elemNr[curPos >> 1]]);
swap(elemNr[curPos >> 1], elemNr[curPos]);
swap(heap[curPos >> 1], heap[curPos]);
curPos >>= 1;
}
}
void heapDelete(int nr) {
int posHp = posInHeap[nr];
swap(posInHeap[nr], posInHeap[elemNr[heapSz]]);
swap(elemNr[heapSz], elemNr[posHp]);
swap(heap[posHp], heap[heapSz]);
heapSz--;
int curPos = posHp;
while (curPos > 1 && (heap[curPos >> 1] > heap[curPos])) {
swap(posInHeap[elemNr[curPos]], posInHeap[elemNr[curPos >> 1]]);
swap(elemNr[curPos >> 1], elemNr[curPos]);
swap(heap[curPos >> 1], heap[curPos]);
curPos >>= 1;
}
while (curPos <= (heapSz >> 1)) {
if (heap[curPos] > heap[curPos << 1]) {
swap(posInHeap[elemNr[curPos]], posInHeap[elemNr[curPos << 1]]);
swap(elemNr[curPos << 1], elemNr[curPos]);
swap(heap[curPos << 1], heap[curPos]);
curPos <<= 1;
}
else if (heap[curPos] > heap[(curPos << 1) + 1]) {
swap(posInHeap[elemNr[curPos]], posInHeap[elemNr[(curPos << 1) + 1]]);
swap(elemNr[(curPos << 1) + 1], elemNr[curPos]);
swap(heap[(curPos << 1) + 1], heap[curPos]);
curPos = (curPos << 1) + 1;
}
else
break;
}
}
int heapTop() {
return heap[1];
}
int main() {
assert(freopen(InFile, "r", stdin));
assert(freopen(OtFile, "w", stdout));
int N;
scanf("%d", &N);
while (N--) {
int op; int b;
scanf("%d", &op);
switch (op) {
case 1:
scanf("%d", &b);
heapInsert(b);
break;
case 2:
scanf("%d", &b);
heapDelete(b);
break;
case 3:
printf("%d\n", heapTop());
break;
default:
break;
}
}
return 0;
}