Pagini recente » Cod sursa (job #1171845) | Cod sursa (job #1234560) | Cod sursa (job #1050294) | Cod sursa (job #2234313) | Cod sursa (job #1651260)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int H[200010],N,M,A[200010],a;
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 sift(int K) {
int son;
do {
son = 0;
// Alege pe cel mai mic fiu
if (left_son(K) <= N) {
son = left_son(K);
if (right_son(K) <= N && H[right_son(K)] < H[left_son(K)]) {
son = right_son(K);
}
//daca fiul este mai mare ca tatal atunci nu interchimbam
if (H[son] >= H[K]) {
son = 0;
}
}
if (son) { // altfel facem interchimbare
swap(H[K], H[son]); // Functie STL ce interschimba H[K] cu H[son].
K = son; // si ne coborim mai jos
}
} while (son);
}
void percolate(int K) {
int key = H[K];
// ne urcam sus numai in cazul in care nodul curent nu este radacina
// si este mai mic ca tatal
while ((K > 1) && (key < H[father(K)])) {
H[K] = H[father(K)];
K = father(K);
}
H[K] = key;
}
void cut(int K) {
H[K] = H[N];
--N;
if ((K > 1) && (H[K] < H[father(K)])) {
percolate(K);
} else {
sift(K);
}
}
void insert(int key) {
H[++N] = key;
percolate(N);
}
int find_index(int val){
for(int i = 1;i<=N;i++) if(H[i] == val) return i;
return -1;
}
int main(){
fin >> M;
for(int i = 0,x,y;i<M;i++){
fin >> x;
if(x == 1){
fin >> A[++a];
insert(A[a]);
}else
if(x == 2){
fin >> y;
cut(find_index(A[y]));
}else{
fout << H[1]<<'\n';
}
}
return 0;
}