Pagini recente » Cod sursa (job #801556) | Cod sursa (job #29811) | Cod sursa (job #2233785) | Cod sursa (job #738023) | Cod sursa (job #1206905)
#include<iostream>
#include<fstream>
using namespace std;
#define max 200010
typedef int Heap[max];
Heap H;
int pos[max];
inline int left_child(int K)
{
return K*2;
}
inline int right_child(int K)
{
return K*2 +1;
}
inline int father(int K)
{
return K/2;
}
void sift(int N,int K)
{
int son;
int p=K;
do
{
son=0;
if(right_child(K) <= N)
{
son = (H[2*K]<H[2*K+1]) ? 2*K : (2*K+1);
}
else if(left_child(K)<=N)
son=2*K;
if(son && H[son]<H[K])
{
swap(H[son],H[K]);
swap(pos[p],pos[son]);
K=son;
}
else
son=0;
}while(son);
}
void peroclate(int N,int K)
{
int p=K;
while(father(K) && H[K]<H[K/2])
{
swap(H[K],H[K/2]);
swap(pos[p],pos[K/2]);
K=K/2;
}
}
void inserare(int &N,int key)
{
H[++N]=key;
pos[N]=N;
peroclate(N,N);
}
void stergere(int &N,int K)
{
H[K]=H[N];
pos[K]=pos[N];
--N;
if(father(K) && H[K/2]>H[K])
peroclate(N,K);
else
sift(N,K);
}
int Min()
{
return H[1];
}
int main()
{
ifstream fi("heapuri.in");
ofstream fo("heapuri.out");
int i,k;
int n=0;
int t;
fi>>t;
for(int j=1;j<=t;j++)
{
fi>>i;
if(i<3)
{
fi>>k;
if(i==1)
inserare(n,k);
else
{
stergere(n,pos[k]);
}
}
else
fo<<Min()<<endl;
}
fi.close();
fo.close();
return 0;
}