Pagini recente » Cod sursa (job #2789846) | Cod sursa (job #2014210) | Cod sursa (job #2020990) | Cod sursa (job #1787466) | Cod sursa (job #3314098)
#include <fstream>
#include <algorithm>
using namespace std;
const int NMAX = 200000;
// heap[i] = idx-ul valorii minime (e.g. v[heap[1]] este minim)
int heap[NMAX + 1], heap_size;
// poz[i] = pozitia in heap a celei de-a i-a valori inserate
int poz_h[NMAX + 1];
// v[i] = valorile propriu-zise din multime
int v[NMAX + 1];
ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
void heapSwap(int p, int q) {
swap(poz_h[heap[p]], poz_h[heap[q]]);
swap(heap[p], heap[q]);
}
void heapUp(int poz) {
while (poz > 1 && v[heap[poz]] < v[heap[(poz >> 1)]]) {
heapSwap(poz, poz >> 1);
poz = (poz >> 1);
}
}
void heapDown(int poz) {
int st, dr, best;
while ((poz << 1) <= heap_size) {
st = (poz << 1);
best = st;
dr = st + 1;
if (dr <= heap_size && v[heap[dr]] < v[heap[st]])
best = dr;
if (v[heap[poz]] > v[heap[best]]) {
heapSwap(poz, best);
poz = best;
} else {
break;
}
}
}
void heapInsert(int idx) {
heap_size++;
heap[heap_size] = idx;
poz_h[idx] = heap_size;
heapUp(heap_size);
}
void heapErase(int idx) {
int poz = poz_h[idx];
heapSwap(poz, heap_size);
heap_size--;
heapDown(poz);
heapUp(poz);
}
int main() {
int op, tip, x, n = 0;
for (fin >> op; op > 0; --op) {
fin >> tip;
switch (tip)
{
case 1:
fin >> x;
n++; v[n] = x;
heapInsert(n);
break;
case 2:
fin >> x;
heapErase(x);
break;
case 3:
fout << v[heap[1]] << "\n";
break;
default:
break;
}
}
return 0;
}