Pagini recente » Cod sursa (job #3030972) | Cod sursa (job #437554) | Cod sursa (job #1789717) | Rating Cristiana (cristiana24) | Cod sursa (job #2124600)
#include<fstream>
#include<vector>
using namespace std;
struct numbers {
int value;
int *poz;
};
vector<numbers> heap;
int time[1000000], t = 0;
void swap(int poz1, int poz2) {
int aux = heap[poz1].value;
heap[poz1].value = heap[poz2].value;
heap[poz2].value = aux;
aux = *heap[poz1].poz;
*heap[poz1].poz = *heap[poz2].poz;
*heap[poz2].poz = aux;
int *p = heap[poz1].poz;
heap[poz1].poz = heap[poz2].poz;
heap[poz2].poz = p;
}
void heapify(int i) {
int n = heap.size();
if (n <= 1) return;
int j = i * 2 + 1;
if (i * 2 + 2 < n && heap[i * 2 + 2].value < heap[j].value)
j = i * 2 + 2;
if (heap[i].value > heap[j].value)
swap(i, j);
if (j <= n / 2 - 1) heapify(j);
}
void heapAdd(int x) {
int j = heap.size();
numbers temp;
time[t]=j;
temp.poz = &time[t++];
temp.value = x;
heap.push_back(temp);
if (j >= 1) {
swap(0, j);
heapify(0);
}
}
void heapDel(int i) {
int j = heap.size() - 1;
if (j == time[i])
heap.pop_back();
else {
swap(time[i], j);
heap.pop_back();
if (time[j]*2+1 < j) heapify(time[j]);
}
}
int main() {
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int N;
in >> N;
for(;N;--N)
{
short command;
in >> command;
switch (command)
{
case 1:
int nr;
in >> nr;
heapAdd(nr);
break;
case 2:
int pozt;
in >> pozt;
heapDel(pozt-1);
break;
case 3:
out << heap[0].value <<"\n";
default:
break;
}
}
}