Pagini recente » Autentificare | Monitorul de evaluare | Gruparea testelor | Monitorul de evaluare | Cod sursa (job #1213364)
#include <iostream>
#include <stdio.h>
using namespace std;
int a[200000 + 100],heapSize,poz[200000 + 100],heapPoz[200000 + 100];
const int maxim = 1000000000 + 1;
void swap(int i,int j){
int aux = a[i];
a[i] = a[j];
a[j] = aux;
aux = poz[i];
poz[i] = poz[j];
poz[j] = aux;
heapPoz[poz[i]] = i;
heapPoz[poz[j]] = j;
}
void min_heapify(int n){
int l = (n << 1) + 1, r = ((n + 1) << 1),min = 0;
if(l < heapSize && a[l] < a[n])
min = l;
else
min = n;
if(r < heapSize && a[r] < a[min])
min = r;
if(min != n){
swap(n,min);
min_heapify(min);
}
}
void maintain_min_heap(int l){
while(l){
l = (l & 1) == 0 ? (l >> 1) - 1 : (l >> 1);
min_heapify(l);
}
}
void delete_element(int x){
a[heapPoz[x - 1]] = maxim;
int elt;
if((elt = (heapPoz[x - 1] << 1) + 1) < heapSize)
maintain_min_heap(elt);
}
void add(int elt){
a[heapSize++] = elt;
}
int main(){
freopen("heapuri.in","r",stdin);
freopen("heapuri.out","w",stdout);
int n,c = 0;
scanf("%d",&n);
for(int i = 0; i < n; i++){
int query,elt;
scanf("%d",&query);
if(query == 1){
scanf("%d",&elt);
poz[heapSize] = heapSize;
heapPoz[heapSize] = heapSize;
add(elt);
maintain_min_heap(heapSize - 1);
}
if(query == 2){
scanf("%d",&elt);
delete_element(elt);
}
if(query == 3)
printf("%d\n",a[0]);
}
return 0;
}