Pagini recente » Cod sursa (job #343032) | Cod sursa (job #1317952) | Cod sursa (job #2394446) | Cod sursa (job #264593) | Cod sursa (job #2289930)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int NMAX = 200000, oo = 10000000;
struct Nod{int val; int cod;} Heap[NMAX + 5];
int Poz[NMAX + 5], T, K, ct;///pozitia in heap a nodului i este Poz[i]
///implementare operatii heap
int sonl(int i) {
return ((2 * i <= K) ? 2 * i : i);
}
int sonr(int i) {
return ((2 * i + 1 <= K) ? 2 * i + 1 : i);
}
void Up(int i) {
while(i > 1 && Heap[i].val < Heap[i / 2].val) {
swap(Poz[Heap[i].cod], Poz[Heap[i / 2].cod]);
swap(Heap[i], Heap[i / 2]);
i /= 2;
}
}
void Down(int i)
{
int next = 1;
while(next)
{
next = 0;
if(2 * i <= K && Heap[i].val > Heap[2 * i].val)
next = 2 * i;
if(2 * i + 1 <= K && Heap[i].val > Heap[2 * i + 1].val && Heap[2 * i].val > Heap[2 * i + 1].val)
next = 2 * i + 1;
if(next) {
swap(Poz[Heap[next].cod], Poz[Heap[i].cod]);
swap(Heap[i], Heap[next]);
i = next;
}
}
}
void AddNod(Nod N)
{
Heap[++K] = N, Poz[N.cod] = K;
Up(K);
}
void DeleteNod(int nume)
{
int i = Poz[nume];
Poz[Heap[K].cod] = Poz[Heap[i].cod];
Heap[i] = Heap[K--];
Up(i);
Down(i);
}
int main()
{
fin >> T;
while(T--) {
int op, x;
fin >> op;
if(op == 3)
fout << Heap[1].val << '\n';
if(op == 2) {
fin >> x;
DeleteNod(x);
}
if(op == 1) {
fin >> x;
AddNod({x, ++ct});
}
}
fin.close();
fout.close();
return 0;
}