Pagini recente » Cod sursa (job #2810881) | Cod sursa (job #2402762) | Cod sursa (job #2416343) | Cod sursa (job #464808) | Cod sursa (job #794516)
Cod sursa(job #794516)
#include<fstream>
using namespace std;
int n,u,lung;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int v[200001],heap[200001],poz[200001];
void urca(int p)
{
int aux;
while(p>1 && v[heap[p]] < v[heap[p/2]])
{
aux = heap[p];
heap[p] = heap[p/2];
heap[p/2] = aux;
poz[heap[p]]=p;
poz[heap[p/2]]=p/2;
p/=2;
}
}
void coboara(int p)
{
int fs = 2*p;
int fd = 2*p + 1;
int bun = p;
int aux;
if(fs<=u && v[heap[fs]] < v[heap[bun]])
bun = fs;
if(fd<=u && v[heap[fd]] < v[heap[bun]])
bun = fd;
if(bun!=p)
{
aux = heap[p];
heap[p] = heap[bun];
heap[bun] = aux;
poz[heap[p]]=bun;
poz[heap[bun]]=p;
coboara(bun);
}
}
void sterge(int pozitie)
{
heap[pozitie]=heap[lung];
lung--;
poz[heap[pozitie]] = pozitie;
urca(pozitie);
coboara(pozitie);
}
int main()
{
int op,x;
in>>n;
for(int i=1;i<=n;i++)
{
in>>op;
if(op==1)
{
in>>v[++u];
lung++;
heap[lung] = u;
poz[u] = lung;
urca(u);
}
if(op==2)
{
in>>x;
sterge(poz[x]);
}
if(op==3)
{
out<<v[heap[1]]<<"\n";
}
}
}