Pagini recente » Cod sursa (job #1054520) | Cod sursa (job #1166983) | Cod sursa (job #3177150) | Cod sursa (job #784971) | Cod sursa (job #653547)
Cod sursa(job #653547)
#include <fstream>
#include<iostream>
using namespace std;
int n, l, nr;
int a[200010], heap[200010], poz[200010];
void insereaza(int x)
{
int aux;
while (x>>1 && a[heap[x]]<a[heap[x>>1]])
{
aux=heap[x];
heap[x]=heap[x>>1];
heap[x>>1] = aux;
poz[heap[x]] = x;
poz[heap[x>>1]] = x>>1;
x=x>>1;
}
}
void sterge(int x)
{
int aux, y = 0;
while (x != y)
{
y = x;
if ((y<<1)<=l && a[heap[x]]>a[heap[y<<1]])
x=y<<1;
if ((y<<1)+1<=l && a[heap[x]]>a[heap[(y<<1)+1]])
x=(y<<1)+1;
aux = heap[x];
heap[x] = heap[y];
heap[y] = aux;
poz[heap[x]] = x;
poz[heap[y]] = y;
}
}
int main()
{
ifstream f("heapuri.in");
ofstream g("heapuri.out");
f>>n;
int i,x,lol;
for (i=1; i<=n; i++)
{
for(int j=1;j<=l;j++){
cout<<a[heap[j]]<<" ";
}
cout<<"\n";
f>>x;
if(x<3)
f>>lol;
if (x==1)
{
l++;
nr++;
a[nr] = lol;
heap[l] = nr;
poz[nr] = l;
insereaza(l);
}
if (x==2)
{
a[lol] = -1;
insereaza(poz[lol]);
poz[heap[1]] = 0;
heap[1] = heap[l--];
poz[heap[1]] = 1;
if (l>1)
sterge(1);
}
if (x==3)
g<<a[heap[1]]<<"\n";
}
return 0;
}