Pagini recente » Cod sursa (job #2195524) | Cod sursa (job #335962) | Cod sursa (job #2057736) | Cod sursa (job #191232) | Cod sursa (job #1457134)
#include <fstream>
#define NMAX 200005
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int heap[NMAX], pos[NMAX], v[NMAX], n = 0, nrquiz, type, value, l = 0;
int left_son(int k)
{
return 2*k;
}
int right_son(int k)
{
return 2*k + 1;
}
int father(int k)
{
return k/2;
}
void change(int a, int b)
{
int aux;
aux = heap[a];
heap[a] = heap[b];
heap[b] = aux;
pos[heap[a]] = a;
pos[heap[b]] = b;
}
void go_down(int k)
{
int aux;
do
{
aux = 0;
if (left_son(k) <= n)
{
aux = left_son(k);
if (right_son(k) <= n && v[heap[aux]] > v[heap[right_son(k)]])
aux = right_son(k);
if (v[heap[k]] < v[heap[left_son(k)]] && v[heap[k]] < v[heap[right_son(k)]])
aux = 0;
if (aux)
{
change(k, aux);
k = aux;
}
}
} while (aux);
}
void go_up(int k)
{
while (k > 1 && v[heap[k]] < v[heap[father(k)]])
{
change(k, father(k));
k = father(k);
}
}
void insert_heap(int x)
{
n ++;
heap[n] = x;
pos[x] = n;
go_up(n);
}
void delete_heap(int k)
{
change(k, n);
n --;
go_down(k);
}
int main()
{
f >> nrquiz;
while(nrquiz)
{
nrquiz -- ;
f >> type;
if (type == 1)
{
f >> value;
v[++l] = value;
insert_heap(l);
}
if (type == 2)
{
f >> value;
delete_heap(pos[value]);
}
if (type == 3)
g << v[heap[1]] << '\n';
}
return 0;
}