Pagini recente » Cod sursa (job #3227165) | Cod sursa (job #2563029) | Cod sursa (job #560567) | Cod sursa (job #3254397) | Cod sursa (job #1097750)
#include <fstream>
#include <iostream>
#define maxheapsize 200010
using namespace std;
ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");
typedef long Heap[maxheapsize];
Heap H;
long Nr, ORDER[maxheapsize], cnt1, cnt2;
inline long father (long nod)
{
return nod / 2;
}
inline long l_son (long nod)
{
return nod * 2;
}
inline long r_son (long nod)
{
return nod * 2 + 1;
}
/*void sift (Heap H, long N, long K)
{
long son;
do
{
son = 0;
if (l_son(K) <= N)
{
son = l_son(K);
if (r_son(K) <= N && H[r_son(K)] < H[l_son(K)])
son = r_son(K);
if (H[son] >= H[K])
son = 0;
}
if (son)
{
swap (H[K], H[son]);
K = son;
}
}while (son);
}
void percolate (Heap H, long N, long K)
{
long key = H[K];
while ((K > 1) && (key < H[father(K)]))
{
H[K] = H[father(K)];
K = father(K);
}
H[K] = key;
}
void build_heap (Heap H, long N)
{
for (long i = N / 2; i > 0; --i)
sift(H, N, i);
}*/
void insertkey (Heap H, long& N, long key)
{
H[++N] = key;
long K = N, K1 = N;
++cnt2;
ORDER[cnt2] = K;
/*for (int j = 1; j <= cnt2; ++j)
fout << ORDER[j] << " ";
fout << "\n";
for (int j = 1; j <= cnt1; ++j)
fout << H[j] << " ";
fout << "\n\n";*/
while ((K > 1) && (key < H[father(K)]))
{
//swap (ORDER[K], ORDER[father(K)]);
H[K] = H[father(K)];
ORDER[K1] = father(K);
ORDER[father(K)] = K;
K = father(K);
}
H[K] = key;
//ORDER[K] = K;
/*for (int j = 1; j <= cnt2; ++j)
fout << ORDER[j] << " ";
fout << "\n";
for (int j = 1; j <= cnt1; ++j)
fout << H[j] << " ";
fout << "\n\n";*/
//ORDER[cnt2] = K;
}
void cut (Heap H, long& N, long K)
{
H[K] = H[N];
--N;
long K1 = K;
/*for (int j = 1; j <= cnt1; ++j)
fout << H[j] << " ";
fout << "\n\n";*/
if ((K > 1) && (H[K] < H[father(K)]))
{
long key = H[K];
while ((K > 1) && (key < H[father(K)]))
{
//swap (ORDER[K], ORDER[father(K)]);
H[K] = H[father(K)];
ORDER[K1] = father(K);
ORDER[father(K)] = K;
K = father(K);
}
H[K] = key;
}
else
{
long son;
do
{
son = 0;
if (l_son(K) <= N)
{
son = l_son(K);
if (r_son(K) <= N && H[r_son(K)] < H[l_son(K)])
son = r_son(K);
if (H[son] >= H[K])
son = 0;
}
if (son)
{
//swap (ORDER[K], ORDER[son]);
swap (H[K], H[son]);
ORDER[K] = son;
ORDER[son] = K;
K = son;
}
}while (son);
}
/*for (int j = 1; j <= cnt2; ++j)
fout << ORDER[j] << " ";
fout << "\n";
for (int j = 1; j <= cnt1; ++j)
fout << H[j] << " ";
fout << "\n\n";*/
}
/*void heapsort (Heap H, long N)
{
build_heap (H, N);
for (long i = N; i >= 2; --i)
{
swap (H[1], H[i]);
sift (H, i - 1, 1);
}
}*/
long search_nod (long nod)
{
for (long i = 1; i <= cnt2; ++i)
if (ORDER[i] == nod)
return i;
}
int main()
{
long i;
int x;
long y;
fin >> Nr;
/*for (long i = 1; i <= N; ++i)
fin >> H[i];
//fout << "5";
// fout << H[1];
heapsort(H, N);
for (long i = 1; i <= N; ++i)
fout << H[i] << " ";
fout << "\n";
cut(H, N, 1);
heapsort(H, N);
for (long i = 1; i <= N; ++i)
fout << H[i] << " ";
//fout << "\n5";*/
for (i = 1; i <= Nr; ++i)
{
/*for (int j = 1; j <= cnt2; ++j)
fout << ORDER[j] << " ";
fout << "\n";*/
fin >> x;
if (x == 1)
{
fin >> y;
insertkey(H, cnt1, y);
}
else if (x == 2)
{
fin >> y;
cut(H, cnt1, ORDER[y]);
}
else
fout << H[1] << "\n";
}
return 0;
}