Pagini recente » Istoria paginii runda/343242354534/clasament | Cod sursa (job #697272) | Cod sursa (job #1722763) | Sandbox (cutiuţa cu năsip) | Cod sursa (job #1862256)
#include <fstream>
using namespace std;
ifstream f("heapuri.in");
ofstream g("heapuri.out");
int vec[500005], n; // vector cu numere si contorul de numere
int heap[500005], l;// heap-ul in care tin pozitiile numerelor din vec[], dar si numarul de elemente
int poz[500005]; // 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*x+1]] < vec[heap[y]] && 2*x+1 <= l)
x = 2*y+1;
else if(vec[heap[2*x]] < vec[heap[y]] && 2*x <= l)
x = 2*y;
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
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;
}