Pagini recente » Cod sursa (job #2904106) | Cod sursa (job #1874607) | Cod sursa (job #1297300) | Cod sursa (job #1090809) | Cod sursa (job #781229)
Cod sursa(job #781229)
#include <fstream>
#define lim 200002
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int cod,v[lim],i,x,pos[lim],H[3*lim],N,n;
inline int father(int nod)
{
return nod/2;
}
inline int left_son(int nod)
{
return 2*nod;
}
inline int right_son(int nod)
{
return 2*nod+1;
}
int minim()
{
return v[H[1]];
}
void sift(int k) {
int son;
son = k;
// Alege un fiu mai mic ca tatal.
if(v[H[left_son(k)]]<v[H[son]] && left_son(k)<=N)
son = left_son(k);
if (right_son(k) <= N && v[H[right_son(k)]] < v[H[son]])
son = right_son(k);
if(son!=k)
{
swap(pos[H[son]],pos[H[k]]);
swap(H[k],H[son]);
sift(son);
}
}
void percolate(int K)
{
while (K >1 && v[H[K]] < v[H[father(K)]])
{
swap(pos[H[K]],pos[H[father(K)]]);
swap(H[K],H[father(K)]);
K=father(K);
}
}
void cut(int K) {
swap(pos[H[K]],pos[H[N]]);
swap(H[K],H[N]);
--N;
sift(K);
}
void insert(int K)
{
H[++N] = K;
pos[K]=N;
percolate(pos[K]);
}
int main()
{
N=0;
int cnt=0;
f>>n;
for(int i=1; i<=n ; i++)
{
f>>cod;
if(cod==1)
{
f>>v[++cnt];
insert(cnt);
}
if(cod==2)
{
f>>x;
cut(pos[x]);
}
if (cod==3)
{
g<<v[H[1]]<<endl;
}
}
}