Pagini recente » Cod sursa (job #1324962) | Cod sursa (job #1943041) | Cod sursa (job #3185051) | Cod sursa (job #781462) | Cod sursa (job #1713827)
#include <algorithm>
#include <iostream>
#include <climits>
#include <cstdlib>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#define inf 10000000
#define M 666013
#define N 110
#define cin fin
#define cout fout
using namespace std;
string file = "heapuri";
ifstream fin(file + ".in");
ofstream fout(file + ".out");
int h[200000], v[200000], k, n, x, y;
void percolate(int poz){
while(poz > 1 && h[poz] < h[poz / 2]){
swap(h[poz], h[poz / 2]);
swap(v[poz], v[poz / 2]);
poz = poz / 2;
}
}
void sift(int poz, int n){
int son;
do{
son = 0;
if(poz * 2 <= n){
son = poz * 2;
if(poz * 2 + 1 <= n && h[poz * 2 + 1] < h[son]){
son = poz * 2 + 1;
}
if(h[poz] > h[son]){
swap(h[poz], h[son]);
swap(v[poz], v[son]);
poz = son;
}
else son = 0;
}
}while(son);
}
int main()
{
cin >> k;
for(int i = 1; i <= k; ++i){
cin >> x;
if(x == 1){
cin >> y;
h[ ++n ] = y;
v[n] = ++v[0];
percolate(n);
}
else if(x == 2){
cin >> y;
int j;
for(j = 1; j <= n; ++j){
if(v[j] == y)
break;
}
h[j] = h[n];
v[j] = v[n];
n--;
if(j != 1 && h[j] < h[j / 2])
percolate(j);
else
sift(j, n);
}
else if(x == 3){
cout << h[1] << '\n';
}
}
return 0;
}