Pagini recente » Cod sursa (job #7306) | Cod sursa (job #2740530) | Cod sursa (job #2971029) | Cod sursa (job #115612) | Cod sursa (job #2324444)
#include <bits/stdc++.h>
using namespace std;
const int MAX_HEAP_SIZE = 16384;
typedef pair < int , int > Heap[MAX_HEAP_SIZE];
Heap H;
int n, pos;
inline int father(int nod)
{
return nod / 2;
}
inline int left_son(int nod)
{
return nod * 2;
}
inline int right_son(int nod)
{
return nod * 2 + 1;
}
inline int min(Heap H)
{
return H[1].first;
}
void sift(Heap H, int n, int k)
{
while(k <= n && (H[k].first > H[left_son(k)].first || H[k].first > H[right_son(k)].first))
{
if(H[left_son(k)].first < H[right_son(k)].first)
swap(H[k], H[left_son(k)]), k = left_son(k);
else
swap(H[k], H[right_son(k)]), k = right_son(k);
}
}
void percolate(Heap H, int n, int k)
{
while(k >= 1 && (H[k].first < H[father(k)].first))
{
swap(H[k], H[father(k)]);
k = father(k);
}
}
void cut(Heap H, int &n, int k)
{
H[k] = H[n];
n--;
if(k > 1 && H[k].first > H[father(k)].first)
percolate(H, n, k);
else
sift(H, n, k);
}
void insert(Heap H, int &n, int x, int pos)
{
H[++n] = {x, pos};
percolate(H, n, n);
}
int main()
{
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int q; f >> q;
while(q--)
{
int c, nr; f >> c;
if(c < 3)
{
f >> nr;
if(c == 1)
insert(H, n, nr, ++pos);
else
{
for(int i = 1; i <= n; i++)
if(H[i].second == nr)
cut(H, n, i);
}
}
else
g << min(H) << '\n';
}
f.close();
g.close();
return 0;
}