Pagini recente » Cod sursa (job #1692631) | Cod sursa (job #1329847) | Cod sursa (job #2590982) | Cod sursa (job #801059) | Cod sursa (job #985245)
Cod sursa(job #985245)
#include <fstream>
#include <algorithm>
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int M, cod, x, nr;
int N;
int Heap[200002];
int V[200002], pos[200002];
int father(int nod)
{
return nod / 2;
}
int left_son(int nod)
{
return nod * 2;
}
int right_son(int nod)
{
return nod * 2 + 1;
}
void sift(int K)
{
int son = 1;
while(son)
{
son = 0;
if (left_son(K) <= N)
{
son = left_son(K);
if (right_son(K) <= N && V[Heap[right_son(K)]] < V[Heap[left_son(K)]])
son = right_son(K);
if (V[Heap[son]] >= V[Heap[K]])
son = 0;
}
if (son)
{
swap(Heap[K], Heap[son]);
pos[Heap[K]] = K;
pos[Heap[son]] = son;
K = son;
}
}
}
void percolate(int K)
{
int key = Heap[K];
while ((K > 1) && (V[key] < V[Heap[father(K)]]))
{
Heap[K] = Heap[father(K)];
pos[Heap[K]] = K;
K = father(K);
}
Heap[K] = key;
pos[Heap[K]] = K;
}
int minim()
{
return V[Heap[1]];
}
void cut(int K)
{
Heap[K] = Heap[N];
pos[Heap[K]] = K;
--N;
if (K > 1 && V[Heap[K]] < V[Heap[father(K)]])
percolate(K);
else
sift(K);
}
void insert(int K)
{
percolate(N);
}
int main()
{
fin >> M;
for (int i = 1; i <= M; ++i)
{
fin >> cod;
if (cod == 1)
{
fin >> x;
++N;
++nr;
Heap[N] = nr;
pos[nr] = N;
V[nr] = x;
insert(x);
}
else if (cod == 2)
{
fin >> x;
cut(pos[x]);
}
else if (cod == 3)
fout << minim() << "\n";
}
fin.close();
fout.close();
return 0;
}