Pagini recente » Cod sursa (job #361664) | Cod sursa (job #702590) | Cod sursa (job #541755) | Cod sursa (job #2984769) | Cod sursa (job #794518)
Cod sursa(job #794518)
#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 schimba(int i,int j)
{
int aux;
aux=heap[i];
heap[i]=heap[j];
heap[j]=aux;
poz[heap[i]]=i;
poz[heap[j]]=j;
}
void urca(int p)
{
while(p>1 && v[heap[p]] < v[heap[p/2]])
{
schimba(p,p/2);
p/=2;
}
}
void coboara(int p)
{
int fs = 2*p;
int fd = 2*p + 1;
int bun = p;
int aux;
if(fs<=lung && v[heap[fs]] < v[heap[bun]])
bun = fs;
if(fd<=lung && v[heap[fd]] < v[heap[bun]])
bun = fd;
if(bun!=p)
{
schimba(p,bun);
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(lung);
}
if(op==2)
{
in>>x;
sterge(poz[x]);
}
if(op==3)
{
out<<v[heap[1]]<<"\n";
}
}
}