Pagini recente » Cod sursa (job #1828991) | Cod sursa (job #1556403) | Cod sursa (job #1118943) | Cod sursa (job #1618671) | Cod sursa (job #1862331)
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int vec[200005], n; // vector cu numere si contorul de numere
int heap[200005], l;// heap-ul in care tin pozitiile numerelor din vec[], dar si numarul de elemente
int poz[200005]; // pozitia in heap a indicilor numerelor din vec[]
int i, j, x, y, o,q;
void urc(int x) {
while (x/2 && vec[heap[x]] < vec[heap[x/2]]) {
poz[heap[x]] = x/2;
poz[heap[x/2]] = x;
swap(heap[x], heap[x/2]);
x /= 2;
}
}
void cobor(int x) {
int y;
while (x != y) {
y = x;
if(vec[heap[2*y]] < vec[heap[x]] && 2*y <= l)
x = 2*x;
else if (vec[heap[2*y+1]] < vec[heap[x]] && 2*y+1 <= l)
x = 2*y+1;
poz[heap[x]] = y;
poz[heap[y]] = x;
swap(heap[x], heap[y]);
}
}
void adaug(int x) {
l++, n++;
vec[n] = x;
heap[l] = n;
poz[n] = l;
urc(l);
}
void scot(int x) {
vec[x] = -1; // pune -1 in locul numarului scos, ca sa aiba pozitia 1
urc(poz[x]); // in heap dupa ce il ridica
poz[heap[1]] = 0; // numarul scos are pozitia 0 in heap, deci ca si inexistent
heap[1] = heap[l]; // ultimul element din heap il pun in varf
l--;
poz[heap[1]] = 1; // si ii aloc pozitia 1
if (l > 1) // dupa care il cobor in heap
cobor(1);
}
int main() {
f >> o;
while (o--) {
f >> q;
if (q < 3) {
f >> x;
if (q == 1)
adaug(x);
else scot(x);
}
else
g << vec[heap[1]] << '\n';
}
return 0;
}