Pagini recente » Cod sursa (job #773145) | Cod sursa (job #830031) | Cod sursa (job #2487445) | Cod sursa (job #2516056) | Cod sursa (job #2749649)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int nmax = 2e5 + 5;
int n, lst, nth, nr[nmax], hp[nmax], posHp[nmax];
int parent(int node){
return node / 2;
}
int leftNode(int node){
return 2 * node;
}
int rightNode(int node){
return 2 * node + 1;
}
void percolate(int h[], int node){
while(node > 0 && nr[h[node]] < nr[h[parent(node)]]){
posHp[h[node]] = parent(node);
posHp[h[parent(node)]] = node;
swap(h[node], h[parent(node)]);
node = parent(node);
}
}
void sift(int h[], int node){
int son;
do {
son = 0;
if(leftNode(node) <= lst){
son = leftNode(node);
if(rightNode(node) <= lst && nr[h[rightNode(node)]] < nr[h[leftNode(node)]])
son = rightNode(node);
if(nr[h[son]] >= nr[h[node]])
son = 0;
if(son){
swap(h[son], h[node]);
posHp[h[son]] = son;
posHp[h[node]] = node;
node = son;
}
}
} while(son);
}
void rmv(int h[], int xth){
int node = posHp[xth];
swap(h[node], h[lst]);
swap(posHp[h[node]], posHp[h[lst]]);
--lst;
if(node > 0 && nr[h[node]] < nr[h[parent(node)]])
percolate(h, node);
else
sift(h, node);
}
void solve(){
fin >> n;
for(int i = 1; i <= n; i++){
int cod, x;
fin >> cod;
if(cod <= 2)
fin >> x;
if(cod == 1){
nr[++nth] = x;
hp[++lst] = nth;
posHp[nth] = lst;
percolate(hp, lst);
} else if(cod == 2)
rmv(hp, x);
else
fout << nr[hp[1]] << "\n";
}
}
int main()
{
solve();
return 0;
}