Pagini recente » Cod sursa (job #1213782) | Cod sursa (job #860702) | Cod sursa (job #1852167) | Cod sursa (job #1390437) | Cod sursa (job #1987020)
#include <iostream>
#include <fstream>
using namespace std;
const int nMax = 200005;
pair<int, int> Heap[nMax]; //first = value, .second = added time
int n;
int t;
int pos[nMax];
inline void SwapNodes(int a, int b)
{
swap(Heap[a], Heap[b]);
swap(pos[Heap[a].second], pos[Heap[b].second]);
}
void Insert(int x)
{
Heap[++n].first = x;
Heap[n].second = ++t;
int nod = n;
int father;
pos[Heap[n].second] = nod;
while(nod > 1)
{
father = nod / 2;
if(Heap[father].first > Heap[nod].first)
{
SwapNodes(father, nod);
nod /= 2;
}
else
break;
}
}
void Delete(int nr)
{
int nod = pos[nr];
Heap[nod].first = 0;
int father;
while(nod > 1)
{
father = nod / 2;
SwapNodes(father, nod);
nod /= 2;
}
SwapNodes(1, n);
--n;
nod = 1;
int next;
while(2 * nod <= n)
{
if(2 * nod + 1 > n || Heap[2 * nod].first < Heap[2 * nod + 1].first)
next = 2 * nod;
else
next = 2 * nod + 1;
SwapNodes(next, nod);
nod = next;
}
}
inline int Minim()
{
return Heap[1].first;
}
int main()
{
ifstream in("heapuri.in");
ofstream out("heapuri.out");
int t;
in >> t;
int cod, x;
for(int i = 1; i <= t; ++i)
{
in >> cod;
if(cod == 1)
{
in >> x;
Insert(x);
}
else if(cod == 2)
{
in >> x;
Delete(x);
}
else
out << Minim() << "\n";
}
in.close();
out.close();
return 0;
}