Pagini recente » Cod sursa (job #2659004) | Cod sursa (job #434399) | Cod sursa (job #987380) | Cod sursa (job #711074) | Cod sursa (job #2692962)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
const int DIM = 200000;
int n, heap[DIM + 1], poz[DIM + 1],iPoz[DIM + 1],idx;
inline int Left(int x)
{
return x * 2;
}
inline int Right(int x)
{
return x * 2 + 1;
}
inline int Parent(int x)
{
return x / 2;
}
void HeapifyUp(int p)
{
int t = Parent(p);
while (p > 1 && heap[p] < heap[t])
{
swap(heap[p], heap[t]);
swap(poz[iPoz[p]],poz[iPoz[t]]);
swap(iPoz[p], iPoz[t]);
p = t;
t = Parent(p);
}
}
void HeapifyDown(int p)
{
int st, dr, minn = p;
st = Left(p);
dr = Right(p);
if (st <= n && heap[st] < heap[minn])
minn = st;
if (dr <= n && heap[dr] < heap[minn])
minn = dr;
if (minn != p)
{
swap(heap[minn], heap[p]);
swap(poz[iPoz[p]], poz[iPoz[minn]]);
swap(iPoz[p], iPoz[minn]);
p = minn;
HeapifyDown(p);
}
}
inline void Insert(int x)
{
heap[++n] = x;
idx++;
poz[idx] = n;
iPoz[n] = idx;
HeapifyUp(n);
}
inline void Delete(int x)
{
heap[poz[x]] = heap[n];
poz[iPoz[n]] = poz[x];
iPoz[poz[x]] = iPoz[n];
n--;
HeapifyUp(poz[x]);
HeapifyDown(poz[x]);
}
inline int Top()
{
return heap[1];
}
int main()
{
int m,op, x;
fin >> m;
for (int i = 1; i <= m; i++)
{
fin >> op;
if (op == 1)
{
fin >> x;
Insert(x);
}
else if (op == 2)
{
fin >> x;
Delete(x);
}
else fout << Top() << "\n";
}
return 0;
}