Pagini recente » Cod sursa (job #3197690) | Cod sursa (job #2885727) | Cod sursa (job #2351585) | Cod sursa (job #125927) | Cod sursa (job #2894496)
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define FILE "heapuri"
using namespace std;
ifstream fin(FILE".in");
ofstream fout(FILE".out");
pair<int,int> heap[200005];
int n,x,op,size;
void urca(int x){
int pos = x;
while(pos > 0){
if(heap[pos/2].first > heap[pos].first){
swap(heap[pos/2],heap[pos]);
pos /= 2;
}
else{
break;
}
}
}
void heapify(int i)
{
int smallest = i;
int l = 2 * i;
int r = 2 * i + 1;
if (l < size && heap[l] < heap[smallest])
smallest = l;
if (r < size && heap[r] < heap[smallest])
smallest = r;
if (smallest != i)
{
swap(heap[i], heap[smallest]);
heapify(smallest);
}
}
void insert(int x){
int pos = size+1;
pair<int,int> xp = {x, size+1};
heap[pos] = xp;
urca(pos);
}
void remove(int x){
int pos=0;
for(int i = 1; i <= size; ++i){
if(heap[i].second == x)
pos = i;
}
heap[pos] = heap[size];
heap[size].first = 0;
heap[size].second = 0;
urca(pos);
heapify(pos);
}
int main(){
fin >> n;
ios::sync_with_stdio(false);
for(int i = 0; i < n; ++i){
fin >> op;
switch (op){
case 1:
fin >> x;
insert(x);
size++;
break;
case 2:
fin >> x;
remove(x);
size--;
break;
case 3:
fout << heap[1].first << '\n';
break;
default:
break;
}
for(int i = 1; i <= size; ++i){
cout << heap[i].first << ' ';
}
cout << '\n';
}
}