Pagini recente » Cod sursa (job #615169) | Cod sursa (job #2256422) | Cod sursa (job #2242939) | Cod sursa (job #2258022) | Cod sursa (job #2592254)
#include <iostream>
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
const int NMAX = 200010;
int Heap[NMAX], Val[NMAX], Poz[NMAX], N, i;
void Swap(int x, int y)
{
swap(Heap[x], Heap[y]);
swap(Poz[Heap[x]], Poz[Heap[y]]);
}
bool cmp(int x, int y)
{
return x < y;
}
void UpHeap(int x)
{
int Father = x / 2;
if(Father && cmp(Val[Heap[x]],Val[Heap[Father]]))
{
Swap(x, Father);
UpHeap(Father);
}
}
void DownHeap(int x)
{
long long Son = x * 2;
if(Son + 1 <= N && cmp(Val[Heap[Son + 1]], Val[Heap[Son]]))
Son += 1;
if(Son <= N && cmp(Val[Heap[Son]], Val[Heap[x]]))
{
Swap(Son, x);
DownHeap(Son);
}
}
void Insert(int x)
{
i += 1;
Val[i] = x;
N += 1;
Heap[N] = i;
Poz[i] = N;
UpHeap(N);
}
void Erase(int x)
{
Swap(x,N);
N -= 1;
DownHeap(x);
}
int main()
{
int q, x, t;
f >> q;
while(q --)
{
f >> t;
if(t == 1)
{
f >> x;
Insert(x);
}
else if(t == 2)
{
f >> x;
Erase(Poz[x]);
}
else g << Val[Heap[1]] << '\n';
}
return 0;
}