Pagini recente » Cod sursa (job #3149044) | Cod sursa (job #2602244) | Cod sursa (job #645800) | Cod sursa (job #1299687) | Cod sursa (job #2618607)
#include <bits/stdc++.h>
using namespace std;
ifstream in("heapuri.in");
ofstream out("heapuri.out");
// 1- Insereaza X
// 2 - Sterge al X-lea element intrat in multime
// 3 - Afisare minim
class Heap // Min Heap
{
private:
vector<pair<int,int> > heap;
void down(int i)
{
int smallest = i;
if( 2*i< heap.size() && heap[smallest].first>heap[2*i].first)
smallest = 2*i;
if(2*i+1 < heap.size() &&heap[smallest].first> heap[2*i+1].first)
smallest = 2*i+1;
if(smallest!=i)
{
swap(heap[smallest],heap[i]);
down(smallest);
}
}
void up(int i)
{
pair<int,int> key = heap[i];
while(i>1 && heap[i/2].first>key.first)
{
heap[i] = heap[i/2];
i=i/2;
}
heap[i]=key;
}
public:
Heap()
{
heap.push_back({2e9,0});
}
void add(int x)
{
heap.push_back({x,heap.size()});
up(heap.size()-1);
}
void erase_ord(int ord)
{
for(auto i=1;i<heap.size();i++)
if(heap[i].second == ord)
{
swap(heap[i],heap[heap.size()-1]);
heap.pop_back();
down(i);
}
}
int const get_min() const
{
return heap[1].first;
}
void afis()
{ cout<<"Start afisare\n";
for(auto &i:heap)
cout<<i.first<<" "<<i.second<<'\n';
cout<<"End afisare\n";
}
};
int main()
{ Heap heap;
int N,x,y;
in>>N;
while(N--)
{ //heap.afis();
in>>x;
if(x==1)
{
in>>y;
heap.add(y);
}
else
if(x==2)
{
in>>y;
heap.erase_ord(y);
}
else
out<<heap.get_min()<<'\n';
}
return 0;
}