Pagini recente » Cod sursa (job #115451) | Cod sursa (job #1688451) | Cod sursa (job #1679186) | Cod sursa (job #1763199) | Cod sursa (job #1754185)
#include<bits/stdc++.h>
#define NMAX 200001
#define swapp(x, y) { int tmp = x; x = y; y = tmp; }
using namespace std;
int N, operation, element, heap[NMAX], heapPz[NMAX], numbers[NMAX], counter = 0;
void heapInsert(int nrcrt, int element) {
heap[++heap[0]] = nrcrt;
int position = heapPz[nrcrt] = heap[0];
while(position / 2 >= 1 && numbers[heap[position / 2]] > element) {
swapp(heap[position / 2], heap[position]);
swapp(heapPz[heap[position / 2]], heapPz[heap[position]]);
position >>= 1;
}
}
void heapDelete(int nrcrt) {
if (heap[0] == 1) {
--heap[0];
return;
}
swapp(heap[heapPz[nrcrt]], heap[heap[0]]);
heapPz[heap[heapPz[nrcrt]]] = heapPz[nrcrt];
--heap[0];
int position = heapPz[nrcrt], newPosition = -1;
while(true) {
int left = position * 2, right = left + 1;
newPosition = position;
if (left <= heap[0] && numbers[heap[left]] < numbers[heap[newPosition]]) newPosition = left;
if (right <= heap[0] && numbers[heap[right]] < numbers[heap[newPosition]]) newPosition = right;
if (position != newPosition) {
swapp(heap[newPosition], heap[position]);
heapPz[heap[newPosition]] = newPosition;
heapPz[heap[position]] = position;
} else break;
}
}
int main() {
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
scanf("%d", &N);
for (int it = 1; it <= N; ++it) {
scanf("%d", &operation);
switch(operation) {
case 1:
scanf("%d", &numbers[++counter]);
heapInsert(counter, numbers[counter]);
break;
case 2:
scanf("%d", &element);
heapDelete(element);
break;
case 3:
printf("%d\n", numbers[heap[1]]);
break;
}
}
return 0;
}