Pagini recente » Cod sursa (job #2630711) | Cod sursa (job #514347) | Cod sursa (job #2378827) | Cod sursa (job #3185048) | Cod sursa (job #2521281)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int q, tip, x, l, nr, H[200005], a[200005], pos[200005];
void Insert(int x)
{
while(x/2 && a[H[x]]<a[H[x/2]])
{
swap(H[x], H[x/2]);
pos[H[x]]=x;
pos[H[x/2]]=x/2;
x/=2;
}
}
void cut(int x)
{
int y=0;
while(x!=y)
{
y=x;
if(y*2<=l && a[H[x]]>a[H[y*2]])
x=y*2;
if(y*2+1<=l && a[H[x]]>a[H[y*2+1]])
x=y*2+1;
swap(H[x], H[y]);
pos[H[x]]=x;
pos[H[y]] = y;
}
}
int main()
{
fin >> q;
while(q--)
{
fin >> tip;
if(tip==1)
{
fin >> x;
a[++nr]=x;
H[++l]=nr;
pos[nr]=l;
Insert(l);
}
if(tip==2)
{
fin >> x;
a[x]=-1;
Insert(pos[x]);
pos[H[1]]=0;
H[1]=H[l--];
pos[H[1]]=1;
if(l>1)
cut(1);
}
if(tip==3)
fout << a[H[1]] << "\n";
}
return 0;
}