Pagini recente » Cod sursa (job #1522520) | Cod sursa (job #226727) | Cod sursa (job #222737) | Cod sursa (job #2629548) | Cod sursa (job #1807515)
#include <fstream>
#include <vector>
using namespace std;
typedef vector<pair<int, int>> Heap;
typedef unsigned int uint;
inline uint Tata(int x) { return x / 2; }
inline uint FiuStanga(int x) { return 2 * x; }
inline uint FiuDreapta(int x) { return 2 * x + 1; }
void Percolate(Heap &h, int p)
{
while (p > 0 && h[p] < h[Tata(p)]) {
swap(h[p], h[Tata(p)]);
p = Tata(p);
}
}
void Sift(Heap &h, int p)
{
while (p != -1) {
int fiu = -1;
if (FiuStanga(p) < h.size()) {
fiu = FiuStanga(p);
if (FiuDreapta(p) < h.size() && h[FiuDreapta(p)].first < h[fiu].first)
fiu = FiuDreapta(p);
if (h[fiu].first < h[p].first)
swap(h[fiu], h[p]);
else fiu = -1;
}
p = fiu;
}
}
void Insereaza(Heap &h, int &n, int x)
{
h.push_back({x, ++n});
Percolate(h, h.size() - 1);
}
void Sterge(Heap &h, int t)
{
for (uint i = 0; i < h.size(); ++i) {
if (h[i].second == t) {
h[i] = h.back();
h.pop_back();
if (i != h.size()) {
if (i != 0 && h[i].first < h[Tata(i)].first)
Percolate(h, i);
if (FiuStanga(i) < h.size() && h[i].first >= h[FiuStanga(i)].first)
Sift(h, i);
}
break;
}
}
}
int main()
{
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
int n;
fin >> n;
Heap heap;
int intrate = 0;
while (n--) {
int r;
fin >> r;
if (r == 3) {
fout << heap[0].first << "\n";
} else {
int x;
fin >> x;
if (r == 1)
Insereaza(heap, intrate, x);
else Sterge(heap, x);
}
}
return 0;
}