Pagini recente » Cod sursa (job #773012) | Cod sursa (job #2097049) | Cod sursa (job #580085) | Cod sursa (job #524646) | Cod sursa (job #2869807)
#include <fstream>
#define maxN 200000
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
int Heap[maxN], Pozitie[maxN], Val[maxN];
int N = 0, Nr = 0;
int Peek()
{
return Val[Heap[1]];
}
int Cmp(int x, int y)
{
return Val[Heap[x]] < Val[Heap[y]];
}
void Swap(int x, int y)
{
swap(Heap[x], Heap[y]);
swap(Pozitie[Heap[x]], Pozitie[Heap[y]]);
}
void UpHeap(int x)
{
int Father = x / 2;
if(Father && Cmp(x, Father))
{
Swap(x, Father);
UpHeap(Father);
}
}
void Insert(int val)
{
N++;
Nr++;
Heap[N] = Nr;
Pozitie[Nr] = N;
Val[Nr] = val;
UpHeap(N);
}
void DownHeap(int x)
{
int Son = x * 2;
if(Son + 1 <= N && Cmp(Son+1, Son))
Son += 1;
if(Son <= N && Cmp(Son, x))
{
Swap(Son, x);
DownHeap(Son);
}
}
void Erase(int Poz)
{
Swap(Poz,N);
N--;
DownHeap(Poz);
}
void Afisare(int N)
{
for(int i = 1; i <= N; i++)
cout << Val[Heap[i]] << " ";
cout << endl;
}
int main()
{
int x, Case;
cin >> x;
for(int i = 1; i <= x; i++)
{
cin >> Case;
if(Case == 1)
{
int val;
cin >> val;
Insert(val);
}
else if(Case == 2){
int Poz;
cin >> Poz;
Erase(Pozitie[Poz]);
}
else cout << Peek() << "\n";
}
return 0;
}