Pagini recente » Cod sursa (job #3230561) | Cod sursa (job #2915655) | Cod sursa (job #2575824) | Cod sursa (job #2733794) | Cod sursa (job #3163632)
#include <bits/stdc++.h>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int n,ceva,x,op,heapSize,k,poz[200005];
struct heapp
{
int nr,ind;
} heap[200005];
void up(int p)
{
while(p>1 and heap[p].nr<heap[p/2].nr)
{
swap(heap[p],heap[p/2]);
swap(poz[heap[p].ind],poz[heap[p/2].ind]);
p=p/2;
}
}
void down(int p)
{
while(p*2<=heapSize)
{
int copil=p*2;
if(copil+1<=heapSize and heap[copil].nr>heap[copil+1].nr)
copil++;
if(heap[p].nr>heap[copil].nr)
{
swap(heap[p],heap[copil]);
swap(poz[heap[p].ind],poz[heap[copil].ind]);
p=copil;
}
else
break;
}
}
void del(int p)
{
swap(heap[heapSize],heap[p]);
swap(poz[heap[heapSize].ind],poz[heap[p].ind]);
heapSize--;
up(p);
down(p);
}
void add(int x)
{
heap[++heapSize].nr=x;
heap[heapSize].ind=++ceva;
poz[ceva]=heapSize;
up(heapSize);
}
int main()
{
f>>n;
for(int i=1; i<=n; i++)
{
f>>op;
if(op==1)
{
f>>x;
add(x);
}
if(op==2)
{
f>>x;
del(poz[x]);
}
if(op==3)
g<<heap[1].nr<<'\n';
}
return 0;
}