Pagini recente » Cod sursa (job #1682276) | preONI 2005 (Runda 3) | Cod sursa (job #1013432) | Cod sursa (job #1094718) | Cod sursa (job #1017072)
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct node{
int value;
int index;
} NODE;
int n;
int heapSize = 0;
NODE heap[200001];
int a[200001];
int currentPos[200001];
void addElement(int x, int addedIndex){
a[addedIndex] = x;
heapSize++;
int currentLocation = heapSize;
while (heap[currentLocation/2].value > x && currentLocation >1){
currentPos[heap[currentLocation/2].index] = currentLocation;
heap[currentLocation] = heap[currentLocation/2];
currentLocation = currentLocation/2;
}
NODE nod;
nod.value = x;
nod.index = addedIndex;
heap[currentLocation] = nod;
currentPos[addedIndex] = currentLocation;
}
void deleteElement(int elementIndex){
int heapLocation = currentPos[elementIndex];
NODE node = heap[heapSize];
heapSize--;
int currentLocation = heapLocation;
while (currentLocation<heapSize && ((currentLocation*2 <= heapSize && node.value > heap[currentLocation*2].value )
|| (currentLocation*2 +1 <=heapSize && node.value > heap[currentLocation*2 +1].value) ) ){
if (currentLocation*2 <= heapSize && currentLocation*2+1 <= heapSize){
if (heap[currentLocation*2].value < heap[currentLocation*2 +1].value){
heap[currentLocation] = heap[currentLocation*2];
currentPos[heap[currentLocation].index] = currentLocation;
currentLocation = currentLocation*2;
} else {
heap[currentLocation] = heap[currentLocation*2+1];
currentPos[heap[currentLocation].index] = currentLocation;
currentLocation = currentLocation*2 + 1;
}
} else if (currentLocation*2 <= heapSize){
heap[currentLocation] = heap[currentLocation*2];
currentPos[heap[currentLocation].index] = currentLocation;
currentLocation = currentLocation*2;
} else {
heap[currentLocation] = heap[currentLocation*2+1];
currentPos[heap[currentLocation].index] = currentLocation;
currentLocation = currentLocation*2 + 1;
}
}
heap[currentLocation] = node;
currentPos[heap[currentLocation].index] = currentLocation;
}
int main()
{
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
scanf("%d", &n);
int addedNumber = 1;
int op, x;
for (int i=1; i<=n; i++){
scanf("%d", &op);
if (op == 1){
scanf("%d", &x);
addElement(x, addedNumber);
addedNumber++;
}
else if (op == 2){
scanf("%d", &x);
deleteElement(x);
} else {
printf("%d\n", heap[1].value);
}
}
return 0;
}