Pagini recente » Cod sursa (job #1653286) | Cod sursa (job #1326250) | Cod sursa (job #559928) | Cod sursa (job #2488880) | Cod sursa (job #2197074)
#include <bits/stdc++.h>
#define Dad(x) (x/2)
#define RightSon(x) (x*2+1)
#define LeftSon(x) (x*2)
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int heap[200005],a[200005],v[200005];
int n,m,obt,nr;
void Insert(int x)
{
heap[++nr]=x;
v[x]=nr;
int y=nr;
while(Dad(x)&&a[heap[y]]<a[heap[Dad(y)]])
{
swap(v[heap[y]],v[heap[Dad(y)]]);
swap(heap[y],heap[Dad(y)]);
y=Dad(y);
}
}
void Erase(int x)
{
swap(heap[x],heap[nr--]);
while((RightSon(x)<=nr&&a[heap[RightSon(x)]]<=a[heap[x]])||
(LeftSon(x)<=nr&&a[heap[LeftSon(x)]]<=a[heap[x]]))
{
if(a[heap[RightSon(x)]]>=a[heap[LeftSon(x)]]||RightSon(x)>nr)
{
swap(v[heap[x]],v[heap[LeftSon(x)]]);
swap(heap[x],heap[LeftSon(x)]);
x=LeftSon(x);
}
else
{
swap(v[heap[x]],v[heap[RightSon(x)]]);
swap(heap[x],heap[RightSon(x)]);
x=RightSon(x);
}
}
}
int main()
{
int x,obt;
fin>>m;
for(int i=1;i<=m;i++)
{
fin>>obt;
switch(obt)
{
case 1:
fin>>x;
a[++n]=x;
Insert(n);
break;
case 2:
fin>>x;
Erase(v[x]);
break;
case 3:
fout<<a[heap[1]]<<"\n";
break;
}
/*for(int i=1;i<=nr;i++)
{
fout<<heap[i]<<" ";
}fout<<"\n";*/
}
return 0;
}