Pagini recente » Cod sursa (job #2556944) | Cod sursa (job #647921) | Cod sursa (job #2187981) | Cod sursa (job #1628642) | Cod sursa (job #1849527)
#include <iostream>
#include <fstream>
#define dim 200005
using namespace std;
ifstream f ("heapuri.in");
ofstream g ("heapuri.out");
int heap[dim],poz[dim],vec[dim],n,i,caz,x,nr,lung;
void muta_sus(int x)
{
while (x / 2 && vec[heap[x]] < vec[heap[x / 2]])
{
swap(heap[x], heap[x/2]);
poz[heap[x]] = x;
poz[heap[x/2]] = x / 2;
x /= 2;
}
}
void muta_jos(int x)
{
int y;
while (x != y)
{
y = x;
if (y*2 <= nr && vec[heap[x]] > vec[heap[y * 2]])
x *= 2;
if (y*2+1 <= nr && vec[heap[x]] > vec[heap[y * 2 + 1]])
x = y*2+1;
swap(heap[x], heap[y]);
poz[heap[x]] = x;
poz[heap[y]] = y;
}
}
int main()
{
int val;
f >> n;
for(i = 1; i <= n; i++)
{
f >> caz;
switch(caz)
{
case 1:
f >> x;
vec[++lung] = x;
heap[++nr] = lung;
poz[lung] = nr;
muta_sus(nr);
break;
case 2:
f >> x;
vec[x] = -1;
muta_sus(poz[x]);
poz[heap[1]] = 0;
swap(heap[1],heap[nr]);
nr--;
poz[heap[1]] = 1;
if(nr > 1)
muta_jos(1);
break;
default:
g << vec[heap[1]] << '\n';
break;
}
}
return 0;
}