Pagini recente » Cod sursa (job #7640) | Cod sursa (job #1874866) | Cod sursa (job #2504003) | Cod sursa (job #2380246) | Cod sursa (job #2135362)
#include <fstream>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
const int MAXN = 2e5;
int N, L, NR;
int heap[MAXN + 10], v[MAXN + 10], poz[MAXN + 10];
int father(int nod){
return nod / 2;
}
int left_son(int nod){
return nod * 2;
}
int right_son(int nod){
return nod * 2 + 1;
}
void upHeap(int nod){
while (father(nod)){
if (v[heap[nod]] < v[heap[father(nod)]]){
swap(heap[nod], heap[father(nod)]);
swap(poz[heap[nod]], poz[heap[father(nod)]]);
nod = father(nod);
}
else
return;
}
}
void downHeap(int nod){
while (true){
if (v[heap[nod]] > v[heap[right_son(nod)]] && right_son(nod) <= L){
swap(heap[nod], heap[right_son(nod)]);
swap(poz[heap[nod]], poz[heap[right_son(nod)]]);
nod = right_son(nod);
}
else if(v[heap[nod]] > v[heap[left_son(nod)]] && left_son(nod) <= L){
swap(heap[nod], heap[left_son(nod)]);
swap(poz[heap[nod]], poz[heap[left_son(nod)]]);
nod = left_son(nod);
}
else
return;
}
}
int main(){
int ceva;
in >> ceva;
while (ceva){
int cod;
in >> cod;
int x;
if (cod == 1){
in >> x;
v[++NR] = x;
poz[NR] = ++L;
heap[poz[NR]] = NR;
upHeap(L);
}
if (cod == 2){
in >> x;
heap[poz[x]] = heap[L];
int aux = heap[L];
swap(poz[x], poz[heap[L]]);
heap[poz[x]] = 0;
poz[x] = 0;
v[x] = -1;
--L;
upHeap(poz[aux]);
downHeap(poz[aux]);
}
if (cod == 3){
out << v[heap[1]] << '\n';
}
ceva --;
}
return 0;
}