Pagini recente » Cod sursa (job #2470848) | Cod sursa (job #2334851) | Cod sursa (job #2812544) | Cod sursa (job #1265964) | Cod sursa (job #2952720)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int heap[200010],op,x,cnt,p[200010],pos,n,i,sch;
set<int>s;
void inserare(int val,int pozitie)
{
heap[++cnt]=val;
p[cnt]=pozitie;
pos=cnt;
while(pos>1 && heap[pos]<heap[pos/2])
{
swap(heap[pos],heap[pos/2]);
swap(p[pos],p[pos/2]);
pos=pos/2;
}
}
void stergere()
{
heap[1]=heap[cnt];
p[1]=p[cnt];
heap[cnt]=p[cnt]=0;
cnt--;
pos=1;
while(pos*2<=cnt)
{
sch=pos;
if(heap[2*pos]<heap[pos])
sch=2*pos;
if(2*pos+1<=cnt && heap[2*pos+1]<heap[sch])
sch=2*pos+1;
if(sch==pos)
break;
swap(heap[pos],heap[sch]);
swap(p[pos],p[sch]);
pos=sch;
}
}
int main()
{
fin>>n;
while(n--)
{
fin>>op;
if(op==1)
{
i++;
fin>>x;
inserare(x,i);
}
else if(op==2)
{
fin>>x;
s.insert(x);
while(s.size() && s.find(p[1])!=s.end())
stergere();
}
else if(op==3)
fout<<heap[1]<<'\n';
}
return 0;
}