Pagini recente » Cod sursa (job #2683179) | Cod sursa (job #916332) | Cod sursa (job #38977) | Cod sursa (job #685815) | Cod sursa (job #1613977)
# include <fstream>
# include <algorithm>
# define MAXN 200010
using namespace std;
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
typedef int heap[MAXN];
heap H;
int v[MAXN], n, i, t, p, x;
int Father(int n) {
return (n >> 1);
}
int Lson(int n) {
return (n << 1);
}
int Rson(int n) {
return ((n << 1) + 1);
}
void Sift(heap h, int n, int k) {
int son;
do {
son = 0;
if (Lson(k) <= n) {
son = Lson(k);
if (Rson(k) <= n && h[Rson(k)] < h[Lson(k)])
son = Rson(k);
if (h[son] >= h[k])
son = 0;
}
if (son) {
swap(h[k], h[son]);
k = son;
}
} while(son);
}
void Percolate(heap h, int k) {
int key = h[k];
while ((k > 1) && (key < h[Father(k)])) {
h[k] = h[Father(k)];
k = Father(k);
}
h[k] = key;
}
void Erase(heap h, int& n, int k) {
h[k] = h[n];
--n;
if ((k > 1) && (h[k] < Father(k)))
Percolate(h, k);
else
Sift(h, n, k);
}
void Insert(heap h, int& n, int key) {
H[++n] = key;
Percolate(h, n);
}
int Search(heap h, int i, int key) {
if (i < 1 || i > n)
return 0;
if (h[i] == key)
return i;
if (h[i] < key) {
int s1, s2;
s1 = Search(h, Lson(i), key);
s2 = Search(h, Rson(i), key);
if (s1) return s1;
if (s2) return s2;
}
return 0;
}
/*
void BuildHeap(heap h, int n) {
for (i=n/2; i; --i)
Sift(h, n, i);
}
void HeapSort(heap h, int n) {
BuildHeap(h, n);
for (int i=n; i>1; --i) {
swap(h[1], h[i]);
Sift(h, i-1, 1);
}
}
*/
void ShowHeap(heap h, int n) {
for (int i=1; i<=n; ++i)
printf("%d ", h[i]);
printf("\n");
}
int main() {
fin >> t;
while (t--) {
fin >> p;
if (p == 1) {
fin >> x;
Insert(H, n, x);
v[n] = x;
ShowHeap(H, n);
continue;
}
if (p == 2) {
fin >> x;
i = Search(H, 1, v[x]);
Erase(H, n, i);
ShowHeap(H, n);
continue;
}
if (p == 3) {
fout << H[1] << "\n";
continue;
}
}
fin.close();
fout.close();
return 0;
}