Pagini recente » Cod sursa (job #2477872) | Cod sursa (job #24544) | Cod sursa (job #316612) | Cod sursa (job #507988) | Cod sursa (job #2853973)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <vector>
#include <climits>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
struct
{
int nr, ind;
}heap[100000];
int left_son(int nod)
{
return nod * 2;
}
int father(int nod)
{
return nod / 2;
}
int right_son(int nod)
{
return nod * 2 + 1;
}
void percolate(int n, int nod)
{
int key = heap[n]. nr;
int ind = heap[n].ind;
while (nod > 1 && key <= heap[father(nod)].nr)
{
heap[nod].nr = heap[father(nod)].nr;
heap[nod].ind = heap[father(nod)].ind;
nod = father(nod);
}
heap[nod].nr = key;
heap[nod].ind = ind;
}
void sift(int n, int nod)
{
int son = 1;
while (son != 0)
{
son = 0;
if (left_son(nod) <= n)
{
son = left_son(nod);
if (right_son(nod) <= n && heap[right_son(nod)].nr < heap[son].nr)
{
son = right_son(nod);
}
if (heap[son].nr >= heap[nod].nr)
{
son = 0;
}
}
if (son != 0)
{
swap(heap[son], heap[nod]);
nod = son;
}
}
}
int main()
{
int n = 0, m, p, ind = 0, nr;
f >> m;
for (int i = 1; i <= m; i++)
{
f >> p;
if (p == 1)
{
f >> nr;
ind++;
heap[++n].nr = nr;
heap[n].ind = ind;
percolate(n, n);
}
else
{
if (p == 2)
{
int i;
f >> nr;
for (i = 1; i <= n; i++)
{
if (heap[i].ind == nr)
{
break;
}
}
heap[i].nr = heap[n].nr;
heap[i].ind = heap[n].ind;
n--;
if (i > 1 && heap[i].nr < heap[father(i)].nr)
{
percolate(n, i);
}
else
{
sift(n, i);
}
}
else
{
g << heap[1].nr << "\n";
}
}
}
return 0;
}