Pagini recente » Cod sursa (job #3363485) | Cod sursa (job #3362235) | Cod sursa (job #3362228) | Cod sursa (job #3362222) | Cod sursa (job #3363689)
#include <iostream>
#include <fstream>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
struct MinHeap
{
static const int MAX_N = 200000;
int heap[MAX_N + 1];
int pos[MAX_N + 1];
int time[MAX_N + 1];
int n;
inline int Father(int node) { return node >> 1; }
inline int LeftSon(int node) { return node << 1; }
inline int RightSon(int node) { return node << 1 | 1; }
inline int GetPos(int time) { return pos[time]; }
inline int Min() { return heap[1]; }
void Sift(int node)
{
int son;
while((son = LeftSon(node)) <= n)
{
son = LeftSon(node);
if(RightSon(node) <= n && heap[RightSon(node)] < heap[LeftSon(node)])
son = RightSon(node);
if(heap[node] < heap[son])
break;
swap(heap[node], heap[son]);
pos[time[node]] = son;
pos[time[son]] = node;
swap(time[node], time[son]);
node = son;
}
}
void Percolate(int node)
{
while(node > 1 && heap[Father(node)] > heap[node])
{
swap(heap[node], heap[Father(node)]);
pos[time[node]] = Father(node);
pos[time[Father(node)]] = node;
swap(time[node], time[Father(node)]);
node = Father(node);
}
}
void Erase(int node)
{
heap[node] = heap[n];
time[node] = time[n];
pos[time[n]] = node;
n--;
if(node > 1 && heap[node] < heap[Father(node)])
Percolate(node);
else
Sift(node);
}
void Insert(int val)
{
static int timer = 0;
n++;
heap[n] = val;
timer++;
time[n] = timer;
pos[timer] = n;
Percolate(n);
}
};
MinHeap minHeap;
int main()
{
int q;
fin >> q;
while(q--)
{
int t, x;
fin >> t;
switch(t)
{
case 1:
fin >> x;
minHeap.Insert(x);
break;
case 2:
fin >> x;
minHeap.Erase(minHeap.GetPos(x));
break;
case 3:
fout << minHeap.Min() << '\n';
}
}
fin.close();
fout.close();
return 0;
}