Pagini recente » Cod sursa (job #1790560) | Cod sursa (job #625111) | Cod sursa (job #877483) | Cod sursa (job #700813) | Cod sursa (job #3252825)
//
// main.cpp
// heap
//
// Created by Andrada Minca on 31.10.2024.
//
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n,c,v,p,i,siz,k,poz[200005];
struct hp
{
int v;
int p;
} heap[200005];
void myswap(int a, int b)
{
swap(heap[a],heap[b]);
swap(poz[heap[a].p],poz[heap[b].p]);
}
void uper(int p)
{
while(heap[p].v<heap[p/2].v&&p>1)
{
myswap(p,p/2);
p=p/2;
}
}
void downy(int p)
{
while(p*2<=siz)
{
int t=p*2;
if(p*2<siz && heap[p*2+1].v<heap[p*2].v)t++;
if(heap[p].v>heap[t].v){myswap(p,t);p=t;}
else break;
}
}
void add(int val,int p)
{
siz++;
heap[siz]={val,p};
poz[p]=siz;
uper(siz);
}
void del(int p)
{
if(poz[p]==siz)siz--;
else
{
int x=poz[p];
myswap(poz[p],siz);
siz--;
uper(x);
downy(x);
}
}
int query()
{
return heap[1].v;
}
int main() {
fin>>n;
for(i=1;i<=n;i++)
{
fin>>c;
if(c==1)
{
fin>>v;
add(v,++k);
}
if(c==2)
{
fin>>p;
del(p);
}
if(c==3)
{
fout<<query()<<'\n';
}
}
return 0;
}