Pagini recente » Cod sursa (job #1669566) | Cod sursa (job #348788) | Cod sursa (job #1583189) | Cod sursa (job #1873224) | Cod sursa (job #2191683)
#include <fstream>
#include <vector>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int N,k,l,C[1000001];
vector <int> v,nr;
void ad_heap(int x)
{
v.push_back(x);
k=v.size()-1;
l=l+1;
nr.push_back(l);
C[nr[nr.size()-1]]=nr.size()-1;
while(v[k]<v[k/2])
{
swap(v[k/2],v[k]);
swap(nr[k/2],nr[k]);
C[nr[k]]=k;
C[nr[k/2]]=k/2;
k=k/2;
}
}
void up(int x)
{
while(v[x]<v[x/2])
{
swap(v[x/2],v[x]);
swap(nr[x/2],nr[x]);
C[nr[x]]=x;
C[nr[x/2]]=x/2;
x=x/2;
}
}
void down(int x)
{ int poz=x;
if(2*x<v.size())
if(v[x]>v[2*x])
poz=2*x;
if(2*x+1<v.size())
if(v[2*x+1]<v[poz])
poz=2*x+1;
if(poz==x)
return;
swap(v[x],v[poz]);
swap(nr[x],nr[poz]);
C[nr[x]]=x;
C[nr[poz]]=poz;
down(poz);
}
void delet(int poz)
{
swap(v[poz],v[v.size()-1]);
swap(nr[poz],nr[v.size()-1]);
nr.pop_back();
v.pop_back();
int aux=poz;
up(poz);
if(aux==poz)
down(poz);
}
int main()
{
int x,y;
f>>N;
for(int i=1; i<=N; i++)
{
f>>x;
if(x==1)
{
f>>y;
ad_heap(y);
}
else if(x==2)
{
f>>y;
delet(C[y]);
}
else g<<v[0]<<endl;
}
return 0;
}