Pagini recente » Cod sursa (job #2874188) | Cod sursa (job #219090) | Cod sursa (job #1441487) | Rating Minuti Vladut Stefan (MinutiVlad) | Cod sursa (job #2287977)
#include <fstream>
using namespace std;
const int maxN=2e5+1;
int n,sz,tm;
int ord[maxN];
int heap[maxN],where[maxN];
void insertHeap(int x){
heap[++sz]=x;
ord[sz]=++tm;
where[tm]=sz;
int nod=sz;
while(nod>1 && heap[nod]<heap[nod/2]){
swap(heap[nod],heap[nod/2]);
swap(ord[nod],ord[nod/2]);
where[ord[nod]]=nod;
where[ord[nod/2]]=nod/2;
nod/=2;
}
}
void popHeap(int nod){
swap(heap[nod],heap[sz]);
swap(ord[nod],ord[sz]);
where[ord[nod]]=nod;
sz--;
int aux=0;
while(nod!=aux){
aux=nod;
if(2*aux <= sz && heap[nod]>heap[2*aux])
nod=2*aux;
if(2*aux+1 <= sz && heap[nod]>heap[2*aux+1])
nod=2*aux+1;
swap(heap[nod],heap[aux]);
swap(ord[nod],ord[aux]);
where[ord[nod]]=nod;
where[ord[aux]]=aux;
}
//sau urca
while(nod>1 && heap[nod]<heap[nod/2]){
swap(heap[nod],heap[nod/2]);
swap(ord[nod],ord[nod/2]);
where[ord[nod]]=nod;
where[ord[nod/2]]=nod/2;
nod/=2;
}
}
int main(){
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
cin>>n;
for(int i=0;i<n;i++){
int op;
cin>>op;
if(op==1){
int x;
cin>>x;
insertHeap(x);
} else if(op==2){
int x;
cin>>x;
popHeap(where[x]);
} else{
cout<<heap[1]<<'\n';
}
}
return 0;
}