Pagini recente » Cod sursa (job #2909064) | Cod sursa (job #6545) | Cod sursa (job #2273273) | Cod sursa (job #1288284) | Cod sursa (job #1856116)
#include<bits/stdc++.h>
#define NMAX 200000
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
//const int NMAX = 200 000;
int H[NMAX], pozitii[NMAX];
int n, m = 0, type;
bool leaf = false;
inline int father(int child)
{
return (child/2);
}
inline int left_son(int father)
{
return 2*father;
}
inline int right_son(int father)
{
return 2*father + 1;
}
void percolate(int key, int key_rank)
{
while( (key_rank > 1) &&( key < H[father(key_rank)] ) )
{
H[key_rank] = H[father(key_rank)];
key_rank = father(key_rank);
}
H[key_rank] = key;
//pozitii[key] = key_rank;
}
void sift(int nod)
{
int son;
do{
son = 1;
//Alege un fiu mai mic ca tatal
if(left_son(nod) <= m)
{
son = left_son(nod);
if(right_son(nod) <= m && H[right_son(nod)] < H[left_son(nod)])
son = right_son(nod);
if(H[son] >= H[nod])
son = 1;
}
if(son > 1)
{
swap(H[nod], H[son]);
nod = son;
// pozitii[H[nod]] = son;
}
}while(son > 1);
}
void push_H(int el)
{
H[++m] = el;
pozitii[el] = m;
percolate(el, pozitii[el]);
}
void pop_H(int el)
{
int rank_el = el;
H[rank_el] = H[m];
--m;
if( (rank_el > 1) && H[rank_el] < H[father(rank_el)])
percolate(H[rank_el], rank_el);
else{
sift(rank_el);
}
}
void display()
{
g<<H[1]<<" ";
}
/*
int find_where_is_y(int el)
{
for(int i = 1; i <= n; i++)
if(pozitii[i] == el) return i;
}
*/
int main()
{
f>>n;
for(int i = 1; i <= n; i++)
{
f>>type;
if(type == 1)
{
int x;
f>>x;
push_H(x);
}
if(type == 2)
{
int y;
f>>y;
pop_H(y);
}
if(type == 3)
{
display();
}
}
// display();
return 0;
}