Pagini recente » Cod sursa (job #1217781) | Cod sursa (job #2783696) | Cod sursa (job #2959026) | Cod sursa (job #2193355) | Cod sursa (job #2324458)
#include <bits/stdc++.h>
using namespace std;
const int MAX_HEAP_SIZE = 262144;
typedef int Heap[MAX_HEAP_SIZE];
Heap H;
int n, pos;
int key[MAX_HEAP_SIZE], niv[MAX_HEAP_SIZE];
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];
}
inline void swap_nodes(int x, int y)
{
swap(H[x], H[y]);
swap(key[niv[x]], key[niv[y]]);
swap(niv[x], niv[y]);
}
void sift(Heap H, int n, int k)
{
int son;
do
{
son = 0;
if(left_son(k) <= n)
{
son = left_son(k);
if(right_son(k) <= n && H[son] > H[right_son(k)])
son = right_son(k);
if(H[son] >= H[k])
son = 0;
}
if(son)
swap_nodes(k, son), k = son;
} while(son);
}
void percolate(Heap H, int n, int k)
{
while(k > 1 && (H[k] < H[father(k)]))
{
swap_nodes(k, father(k));
k = father(k);
}
}
void cut(Heap H, int &n, int k)
{
H[k] = H[n];
n--;
if(k > 1 && H[k] < H[father(k)])
percolate(H, n, k);
else
sift(H, n, k);
}
void insert(Heap H, int &n, int x, int pos)
{
H[++n] = x;
key[pos] = n;
niv[n] = 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
cut(H, n, key[nr]);
}
else
g << min(H) << '\n';
}
f.close();
g.close();
return 0;
}