Pagini recente » Cod sursa (job #2379703) | Cod sursa (job #2352349) | Cod sursa (job #822140) | Cod sursa (job #2309900) | Cod sursa (job #792091)
Cod sursa(job #792091)
#include<algorithm>
#include<fstream>
using namespace std;
#define Maxim 200005
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int N,A[Maxim],Heap[Maxim],Poz[Maxim],NR,Lg;
void move_up(int X,int Max){
while(X/2 && A[Heap[X]]<A[Heap[X/2]]){
swap(Heap[X],Heap[X/2]);
Poz[Heap[X]]=X;
Poz[Heap[X/2]]=X/2;
X/=2;
}
}
void move_down(int X,int Max){
int son;
do{
son=X;
if(2*X<=Max && A[Heap[son]]>A[Heap[2*X]])
son=2*X;
if(2*X+1<=Max && A[Heap[son]]>A[Heap[2*X+1]])
son=2*X+1;
if(son!=X){
swap(Heap[X],Heap[son]);
Poz[Heap[X]]=X;
Poz[Heap[son]]=son;
X=son;
}
}while(son!=X);
}
int main(){
fin>>N;
int pos,x,y;
for(int i=1; i<=N; i++){
fin>>x;
if(x==1){
fin>>y;
NR++,Lg++;
A[NR]=y;
Heap[Lg]=NR;
Poz[NR]=Lg;
move_up(Lg,Lg);
}
if(x==2){
fin>>y;
pos=Poz[y];
Heap[pos]=Heap[Lg];
Poz[Heap[pos]]=pos;
Poz[y]=-1;
Heap[Lg--]=0;
if(pos/2 && A[Heap[pos]]<A[Heap[pos/2]])
move_up(pos,Lg);
else
move_down(pos,Lg);
}
if(x==3){
fout<<A[Heap[1]]<<"\n";
}
}
fout.close();
fin.close();
return 0;
}