Pagini recente » Cod sursa (job #320691) | Cod sursa (job #2639948) | Cod sursa (job #903639) | Cod sursa (job #2620381) | Cod sursa (job #2745115)
#include <fstream>
using namespace std;
int A[200010], heap[200010], Pos[200010];
int n,c, L, NR;
void push(int x)
{
while (x/2 && A[heap[x]]<A[heap[x/2]])
{
swap(heap[x],heap[x/2]);
Pos[heap[x]] = x;
Pos[heap[x/2]] = x/2;
x /= 2;
}
}
void pop(int x)
{
int y = 0;
while (x != y)
{
y = x;
if (y*2<=L && A[heap[x]]>A[heap[y*2]]) x = y*2;
if (y*2+1<=L && A[heap[x]]>A[heap[y*2+1]]) x = y*2+1;
swap(heap[x],heap[y]);
Pos[heap[x]] = x;
Pos[heap[y]] = y;
}
}
int main()
{
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
fin>>n;
int x;
for(int i=0;i<n;i++)
{
fin>>c;
if(c==3) fout<<A[heap[1]]<<'\n';
else{
fin>>x;
if(c==2)
{
A[x]=-1;
push(Pos[x]);;
Pos[heap[1]]=0;
heap[1]=heap[L--];
Pos[heap[1]]=1;
if(L>1)pop(1);
}
if(c==1)
{
L++;NR++;
A[NR]=x;
heap[L]=NR;
Pos[NR] = L;
push(L);
}
}
}
return 0;
}