Pagini recente » Cod sursa (job #58373) | Cod sursa (job #2664399) | Cod sursa (job #2751923) | Cod sursa (job #1653381) | Cod sursa (job #2689702)
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int amount_of_numbers = 1;
int k = 1; // index of heap
ll heap[200005];
int positions[200005]; // positions[i] - the position in the heap for the ith entered number;
int posinheap[200005]; // posinheap[i] - the number of entrance in the ith position in the heap;
void insert(int number){
heap[k] = number;
posinheap[k] = amount_of_numbers;
positions[amount_of_numbers] = k;
int index = k;
while(index != 1){
if(heap[index/2] > heap[index]){
swap(heap[index/2],heap[index]);
swap(positions[amount_of_numbers], positions[posinheap[index/2]]);
swap(posinheap[index/2], posinheap[index]);
index /= 2;
} else{
break;
}
}
k++;
amount_of_numbers++;
}
int get_min(){
return heap[1];
}
void pop_position(int position){
int index = positions[position];
k--;
heap[positions[position]] = heap[k];
heap[k] = 0;
swap(positions[position], positions[posinheap[k]]);
swap(posinheap[k], posinheap[positions[posinheap[k]]]);
position = index;
while(position < k) {
if(heap[position*2] > heap[position*2+1] && position*2+1 < k) {
if(heap[position] > heap[position*2+1]) {
swap(heap[position], heap[position*2+1]);
swap(positions[posinheap[position]], positions[posinheap[position*2+1]]);
swap(posinheap[position*2+1], posinheap[position]);
position = position*2+1;
} else {
break;
}
} else {
if(heap[position] > heap[position*2] && position*2 < k) {
swap(heap[position], heap[position*2]);
swap(positions[posinheap[position]], positions[posinheap[position*2]]);
swap(posinheap[position*2], posinheap[position]);
position = position*2;
} else {
break;
}
}
}
}
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int main(){
int n;
ll i,op,arg;
fin>>n;
for(i = 0; i < n; i++){
fin>>op;
if (op<3){
fin>>arg;
if(op == 1){
insert(arg);
} else {
pop_position(arg);
}
} else {
fout<<get_min()<<"\n";
}
/*
for(int j = 1; j < amount_of_numbers; j++) {
cout<<positions[j];
}
cout<<"\n";
for(int j = 1; j < k; j++) {
cout<<heap[j];
}
cout<<"\n";
for(int j = 1; j < k; j++) {
cout<<posinheap[j];
}
cout<<"\n";
*/
}
}