Pagini recente » Borderou de evaluare (job #696868) | Borderou de evaluare (job #1143179) | Borderou de evaluare (job #1466395) | Borderou de evaluare (job #1570019) | Cod sursa (job #2201698)
#include <iostream>
#include <fstream>
using namespace std;
const int MAX_HEAP_SIZE = 200005;
int ord[MAX_HEAP_SIZE],M;
struct elem {int val,posinord;};
typedef elem Heap[MAX_HEAP_SIZE];
inline int father(int nod)
{
return nod / 2;
}
inline int left_son(int nod)
{
return nod * 2;
}
inline int right_son(int nod)
{
return nod * 2 + 1;
}
inline int mini(Heap H)
{
return H[1].val;
}
void sift(Heap H, int N, int K)
{
int son;
do
{
son = 0;
// Alege un fiu mai mare ca tatal.
if (left_son(K) <= N)
{
son = left_son(K);
if (right_son(K) <= N && H[right_son(K)].val < H[left_son(K)].val)
{
son = right_son(K);
}
if (H[son].val >= H[K].val)
{
son = 0;
}
}
if (son)
{
swap(ord[H[K].posinord],ord[H[son].posinord]);
swap(H[K], H[son]); // Functie STL ce interschimba H[K] cu H[son].
K = son;
}
}
while (son);
}
void percolate(Heap H, int N, int K)
{
elem key = H[K];
while ((K > 1) && (key.val < H[father(K)].val))
{
H[K] = H[father(K)];
ord[H[K].posinord] = K;
K = father(K);
}
H[K] = key;
ord[H[K].posinord] = K;
}
void hinsert(Heap H, int& N, int key)
{
H[++N].val = key;
++M;
H[N].posinord = M;
ord[M]=N;
percolate(H, N, N);
}
void cut(Heap H, int& N, int K)
{
H[K] = H[N];
ord[H[K].posinord] = ord[H[N].posinord];
ord[H[N].posinord] = -1;
--N;
if ((K > 1) && (H[K].val < H[father(K)].val))
{
percolate(H, N, K);
}
else
{
sift(H, N, K);
}
}
int main()
{
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
Heap H;
int nrin,n=0;
fin>>nrin;
for (int var=0; var<nrin; var++)
{
int x,cer;
fin>>cer;
if (cer==3)
fout<<mini(H)<<'\n';
else
{
fin>>x;
if (cer==1)
hinsert(H,n,x);
if (cer==2)
cut(H,n,ord[x]);
}
}
return 0;
}