Pagini recente » Cod sursa (job #553370) | Cod sursa (job #1044262) | Cod sursa (job #1948643) | Cod sursa (job #2104496) | Cod sursa (job #3233553)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n;
vector <int> pos;
vector <pair<int, int> > heap;
void move_up(int index)
{
pair<int, int> copie = heap[index];
while (index > 0 && copie.first < heap[index / 2].first)
{
pos[heap[index/2].second] = index;
heap[index] = heap[index / 2];
index /= 2;
}
pos[heap.size() - 1] = index;
heap[index] = copie;
}
void move_down(int index)
{
int fiu;
do {
fiu = 0;
if (2 * index < heap.size()) {
fiu = 2 * index;
if (index * 2 + 1 < heap.size() && heap[index * 2 + 1].first < heap[2 * index].first) {
fiu = 2 * index + 1;
}
if (heap[fiu].first >= heap[index].first) {
fiu = 0;
}
}
if (fiu) {
swap(pos[heap[index].second], pos[heap[fiu].second]);
swap(heap[index], heap[fiu]);
index = fiu;
}
} while (fiu);
}
void insert(int x)
{
int hsize = heap.size();
heap.push_back(make_pair(x,hsize));
pos.push_back(x);
move_up(heap.size() - 1);
}
void sterge(int index)
{
int hsize = heap.size();
heap[index] = heap[hsize - 1];
pos[heap[hsize-1].second] = index;
heap.pop_back();
if (index > 0 && heap[index] < heap[index / 2])
{
move_up(index);
}
else {
move_down(index);
}
}
int minim() {
return heap[1].first;
}
void afisare()
{
for (int i = 1; i < heap.size(); ++i)
{
cout << heap[i].first << " " << heap[i].second << " ";
}
cout << '\n';
}
int main()
{
fin >> n;
pos.push_back(0);
heap.push_back(make_pair(0,0));
while (n--)
{
int c;
fin >> c;
if (c == 1) {
int x;
fin >> x;
insert(x);
}
else if (c == 2) {
int x;
fin >> x;
sterge(pos[x]);
}
else {
fout << minim() << '\n';
}
//afisare();
}
}