Pagini recente » Cod sursa (job #1759224) | Cod sursa (job #1147284) | Cod sursa (job #18321) | Cod sursa (job #935913) | Cod sursa (job #2901336)
#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
ifstream is("heapuri.in");
ofstream os("heapuri.out");
#define nrmax 200005
int poz[nrmax], ord, aux[nrmax], heap[nrmax], n, nr_op;
int mx, l, r, op, val, s1;
void heapify( int i){
if(heap[i]<heap[i /2] && i>1){
swap(poz[ord], poz[aux[i /2]]);
swap(heap[i], heap[i /2]);
swap(aux[i], aux[i /2]);
heapify(i/2);
}
}
void heapifyTop(int i){
mx = i;
l = 2 * mx ;
r = 2 * mx + 1;
if(l<=n && heap[l] < heap[mx])
mx = l;
if(r<=n && heap[r]<heap[mx])
mx = r;
if(mx != i){
swap(poz[aux[i]], poz[aux[mx]]);
swap(heap[i], heap[mx]);
swap(aux[i], aux[mx]);
heapifyTop(mx);
}
}
void inserare(int elem){
heap[n] = elem;
heapify(n);
}
void stergere(int k){
s1 = poz[k];
heap[poz[k]] = heap[n];
swap(poz[k], poz[aux[n]]);
swap(aux[s1], aux[n]);
n--;
heapifyTop(1);
}
int main()
{
is>>nr_op;
for(int i =0; i<nr_op;i++){
is>>op;
if(op==3)
{
os<<heap[1];
os<<endl;
}
if(op==1)
{
is>>val;
ord++;
n++;
poz[ord] = n;
aux[n] = ord;
inserare(val);
}
if(op==2)
{
is>>val;
stergere(val);
}
}
return 0;
}