Pagini recente » Cod sursa (job #1596914) | Cod sursa (job #513660) | Cod sursa (job #1521282) | Cod sursa (job #1058483) | Cod sursa (job #1172231)
#include <fstream>
#include <iostream>
#include <algorithm>
#define dim 200001
using namespace std;
typedef int Heap[dim];
int Hp[dim]; //cand a fost adaugat elem de pe poz i din heap
int pH[dim]; // poz in heap al elem adaugat al i-lea
int a,b,N=0,j=0,n;
Heap H;
inline int father(int K){
return (K >> 1);
}
inline int left_son(int K){
return (K << 1);
}
inline int right_son(int K){
return ((K << 1) + 1);
}
inline int max(Heap& H){
return H[1];
}
void sift(Heap& H,int& N,int K){
int son=0;
do{
int son = 0;
if(left_son(K) <= N){
son = left_son(K);
if((right_son(K) <= N) && (H[son] > H[right_son(K)])){
son = right_son(K);
}
if(H[son] >= H[K]){
son = 0;
}
}
if(son){
swap(H[K],H[son]);
swap(pH[Hp[K]],pH[Hp[son]]);
swap(Hp[K],Hp[son]);
K = son;
}
}while(son);
}
void percolate(Heap& H,int& N,int K){
int key = H[K];
int ord = Hp[K];
while((K > 1) && (key < H[father(K)])){
H[K] = H[father(K)];
pH[Hp[father(K)]] = K;
Hp[K] = Hp[father(K)];
K = father(K);
}
H[K] = key;
Hp[K] = ord;
pH[ord] = K;
}
void cut(Heap& H,int& N,int K){
H[K] = H[N];
pH[Hp[N]] = K;
Hp[K] = Hp[N--];
if((K > 1) && (H[father(K)] > H[K])){
percolate(H,N,K);
}
else{
sift(H,N,K);
}
}
void insert(Heap& H,int& N,int key,int& j){
H[++N] = key;
pH[j] = N;
Hp[N] = j;
percolate(H,N,N);
}
int main(){
ifstream f("heapuri.in");
ofstream g("heapuri.out");
f >> n;
for(int i =1 ; i <= n; i++){
f >> a;
switch(a){
case 1: {
f >> b;
insert(H,N,b,++j);
break;
}
case 2: {
f >> b;
cut(H,N,pH[b]);
break;
}
case 3: {
g << max(H) << "\n";
break;
}
}
}
}