Pagini recente » Cod sursa (job #3204943) | Istoria paginii runda/oni_clasa10_2022-2016/clasament | Cod sursa (job #1838809) | Cod sursa (job #1135861) | Cod sursa (job #2744445)
#include<iostream>
#include<fstream>
using namespace std;
ifstream cit("heapuri.in");
ofstream afis("heapuri.out");
#define maxim 200010
int l, nr;
int heap[maxim], poz[maxim], elem[maxim];
void introd_val(int x){
while((x/2) && (elem[heap[x]] < elem[heap[x/2]])){
swap(heap[x], heap[x/2]);
poz[heap[x]] = x;
poz[heap[x/2]] = x/2;
x/=2;
}
}
void sterg_val(int x){
int y = 0;
while(x != y){
y = x;
if((y*2 <= l) && (elem[heap[x]] > elem[heap[y*2]])){
x = y*2;
}
if((y*2+1 <= l) && (elem[heap[x]] > elem[heap[y*2+1]])){
x = y*2+1;
}
swap(heap[x], heap[y]);
poz[heap[x]] = x;
poz[heap[y]] = y;
}
}
int main()
{
int cod, x, n;
cit>>n;
for(int i = 0; i < n; i++){
cit>>cod;
if(cod < 3){
cit>>x;
}
if(cod == 1){
l++;
nr++;
elem[nr] = x;
heap[l] = nr;
poz[nr] = l;
introd_val(x);
}
else if(cod == 2){
elem[x] = -1;
introd_val(poz[x]);
poz[heap[1]] = 0;
heap[1] = heap[l--];
poz[heap[1]] = 1;
if(l>1){
sterg_val(1);
}
}
else if(cod == 3){
afis<<elem[heap[1]]<<"\n";
}
}
return 0;
}