Pagini recente » Cod sursa (job #681083) | Cod sursa (job #2371545) | Cod sursa (job #2396213) | Cod sursa (job #351892) | Cod sursa (job #1217444)
#include <fstream>
using namespace std;
int N,L,K, heap[1000000], a[200100], pos[200100];
void heap_swap(int pos_1, int pos_2){
int aux;
pos[heap[pos_1]]=pos_2;
pos[heap[pos_2]]=pos_1;
aux=heap[pos_1];
heap[pos_1]=heap[pos_2];
heap[pos_2]=aux;
}
void ins(int x){
a[++K]=x; pos[K]=++L; heap[L]=K;
int p=L;
while (p/2 && a[heap[p]]<a[heap[p/2]]){
heap_swap(p,p/2);
p=p/2;
}
}
void del(int x){
int p=pos[x];
heap[p]=heap[L--];
pos[heap[p]]=p;
while (p*2+1<=L && (a[heap[p]]>a[heap[p*2]] || a[heap[p]]>a[heap[p*2+1]]))
if (a[heap[p*2]]<a[heap[p*2+1]]){
heap_swap(p,p*2);
p=p*2;
}
else{
heap_swap(p,p*2+1);
p=p*2+1;
}
if (p*2<=L && a[heap[p]]>a[heap[p*2]])
heap_swap(p,p*2);
}
int main(){
ifstream in("heapuri.in");
ofstream out("heapuri.out");
in >> N;
int i,op,x;
for (i=1; i<=N; i++){
in >> op;
if (op==1){
in >> x;
ins(x);
}
else if (op==2){
in >> x;
del(x);
}
else
out << a[heap[1]] << "\n";
}
return 0;
}