Pagini recente » Cod sursa (job #3217426) | Cod sursa (job #1308765) | Cod sursa (job #2627413) | Cod sursa (job #2740847) | Cod sursa (job #1849500)
#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 pozz)
{
if(pozz == 1)
return;
if(vec[heap[pozz / 2]] > vec[heap[pozz]])
swap(heap[pozz / 2],heap[pozz]),swap(poz[heap[pozz / 2]],poz[heap[pozz]]),muta_sus(pozz / 2);
}
void muta_jos(int pozz)
{
while(true)
{
if(2*pozz > nr)
break;
else if(vec[heap[pozz]] > vec[heap[2*pozz]])
swap(heap[pozz],heap[2*pozz]),swap(poz[heap[pozz * 2]],poz[heap[pozz]]),pozz *= 2;
else if(vec[heap[pozz]] > vec[heap[2*pozz + 1]])
swap(heap[pozz * 2 + 1],heap[pozz]),swap(poz[heap[pozz * 2 + 1]],poz[heap[pozz]]),pozz = pozz * 2 + 1;
else break;
}
}
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]);
swap(heap[1],heap[nr]);
poz[heap[1]] = 1;
nr--;
muta_jos(1);
break;
default:
g << vec[heap[1]] << '\n';
break;
}
}
return 0;
}