Pagini recente » Cod sursa (job #479799) | Cod sursa (job #2852380) | Cod sursa (job #1802605) | Cod sursa (job #2510634) | Cod sursa (job #3183021)
#include <fstream>
#include <vector>
#include <map>
using namespace std;
ifstream cin("heapuri.in");
ofstream cout("heapuri.out");
class Heap
{
private:
vector<int> heap;
map<int, int> pos;
map<int, int> inversePos;
vector<int> timeInside;
void Swap(int i, int j)
{
swap(heap[i], heap[j]);
pos[inversePos[j]] = i;
pos[inversePos[i]] = j;
swap(inversePos[i], inversePos[j]);
}
void HeapifyDown(int i)
{
int left = 2 * i;
int right = 2 * i + 1;
int minPos = i;
if(left < heap.size() && heap[left] < heap[minPos])
minPos = left;
if(right < heap.size() && heap[right] < heap[minPos])
minPos = right;
if(minPos != i)
{
Swap(i, minPos);
HeapifyDown(minPos);
}
}
void HeapifyUp(int i)
{
while(i >= 1 && heap[i] < heap[i / 2])
{
Swap(i / 2, i);
i /= 2;
}
}
public:
Heap()
{
heap.push_back(0);
timeInside.push_back(0);
}
void Insert(int x)
{
heap.push_back(x);
pos[x] = heap.size() - 1;
inversePos[heap.size() - 1] = x;
timeInside.push_back(x);
HeapifyUp(pos[x]);
}
void Delete(int when)
{
int x = timeInside[when];
int posX = pos[x];
Swap(posX, heap.size() - 1);
heap.pop_back();
HeapifyUp(posX);
HeapifyDown(posX);
pos.erase(x);
inversePos.erase(heap.size() - 1);
}
int Top()
{
return heap[1];
}
};
int n;
Heap H;
int main()
{
cin >> n;
for(int i = 1; i <= n; i++)
{
int type;
cin >> type;
if(type == 1)
{
int x;
cin >> x;
H.Insert(x);
}
else if(type == 2)
{
int x;
cin >> x;
H.Delete(x);
}
else
cout << H.Top() << '\n';
}
return 0;
}