Pagini recente » Cod sursa (job #1672467) | Cod sursa (job #2024910) | Cod sursa (job #2786973) | Cod sursa (job #882276) | Cod sursa (job #237837)
Cod sursa(job #237837)
#include <fstream>
#include <vector>
using namespace std;
const int Max_Heap_Size=2000001;
typedef int Heap[Max_Heap_Size];
int n,op,x;
Heap H;
int noduri=0;
fstream fin ("heapuri.in",ios::in);
fstream fout("heapuri.out",ios::out);
inline int father(int nod){
return nod/2;
}
inline int left_son(int nod){
return 2*nod;
}
inline int right_son(int nod){
return 2*nod+1;
}
void insert(int x){
int nod=++noduri;
int aux;
H[nod]=x;
while(father(nod) && (H[father(nod)]<x)){
aux=H[nod];
H[nod]=H[father(nod)];
H[father(nod)]=aux;
nod=father(nod);
}
}
void del(int x){
int nod=noduri,aux;
noduri--;
H[x]=H[nod];
/*H[x]=0;*/
nod=x;
if (H[father(nod)]<H[nod]){
while(father(nod) && (H[father(nod)]<H[nod])){
aux=H[nod];
H[nod]=H[father(nod)];
H[father(nod)]=aux;
nod=father(nod);
}
}
else{
while(left_son(nod) && (H[left_son(nod)]>H[nod] || H[right_son(nod)]>H[nod])){
if (H[left_son(nod)]>H[nod]){
aux=H[left_son(nod)];
H[left_son(nod)]=H[nod];
H[nod]=aux;
nod=left_son(nod);
}
else{
aux=H[right_son(nod)];
H[right_son(nod)]=H[nod];
H[nod]=aux;
nod=right_son(nod);
}
}
}
}
void afis(){
fout<<0;
}
int main(){
fin>>n;
for (int i=1;i<=n;i++){
fin>>op;
switch (op){
case 1 :fin>>x;
insert(x);
break;
case 2 :fin>>x;
del(x);
break;
case 3 :afis();
break;
}
}
}