Pagini recente » Cod sursa (job #3229155) | Cod sursa (job #2713641) | Cod sursa (job #1837412) | Cod sursa (job #3291563) | Cod sursa (job #3236650)
#include <iostream>
#include <fstream>
#include <map>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int nmax = 2e5+10;
typedef int heap[nmax];
heap h;
int n = 0,numar_q,operation,value;
int deleted[nmax],numbers[nmax];
map<int,int> mp;
inline int father(int nod){
return nod/2;
};
inline int left_son(int nod){
return nod*2;
};
inline int right_son(int nod){
return nod*2 + 1;
}
inline int minimum_value(heap h){
return h[1];
}
void sift(heap h,int n,int k){
int son = 0;
do{
if(left_son(k) <= n){
son = left_son(k);
if(right_son(k) <= n && h[right_son(k)] < h[left_son(k)]){
son = right_son(k);
};
if(h[son] >= h[k]){
son = 0;
}
};
if(son){
swap(h[son],h[k]);
k = son;
}
}while(son);
};
void percolate(heap h,int n,int k){
int value = h[k];
while(k > 1 && h[father(k)] > value){
h[k] = h[father(k)];
k = father(k);
};
h[k] = value;
}
void erase_root(heap h,int &n){
h[1] = h[n--];
sift(h,n,1);
}
void insert(heap h,int &n,int value){
h[++n] = value;
percolate(h,n,n);
}
int main(){
fin >> numar_q;
int index = 0;
for(int i = 1; i <=numar_q; i++){
fin >> operation;
if(operation == 1){
fin >> value;
insert(h,n,value);
numbers[++index] = value;
}else if(operation == 2){
fin >> value;
mp[numbers[value]] = 1;
}else{
while(mp[minimum_value(h)]){
erase_root(h,n);
}
fout << minimum_value(h) << '\n';
}
};
return 0;
}