Pagini recente » Cod sursa (job #931505) | Cod sursa (job #2930709) | Cod sursa (job #2190539) | Cod sursa (job #1788530) | Cod sursa (job #1822704)
#include <cstdio>
const int MAX_N = 200000;
int size;
int v[1+MAX_N], heap[1+MAX_N], poz[1+MAX_N];
bool cmp(int a, int b) { return v[heap[a]] < v[heap[b]]; }
inline void swap(int &a, int &b) {
int aux;
aux = a;
a = b;
b = aux;
}
inline void swapheap(int a, int b) {
swap(heap[a], heap[b]);
swap(poz[heap[a]], poz[heap[b]]);
}
void upheap(int poz) {
while(poz > 1) {
if(cmp(poz, poz / 2))
swapheap(poz, poz / 2);
else
poz = 1;
poz = poz / 2;
}
}
void downheap(int poz) {
int targ;
while(poz * 2 <= size) {
targ = poz;
if(poz * 2 <= size && cmp(poz * 2, targ))
targ = poz * 2;
if(poz * 2 + 1 <= size && cmp(poz * 2 + 1, targ))
targ = poz * 2 + 1;
if(poz != targ) {
swapheap(poz, targ);
poz = targ;
} else
poz = size;
}
}
void add(int i) {
size++;
heap[size] = i;
poz[i] = size;
upheap(size);
}
void erase(int poz) {
swapheap(poz, size);
--size;
downheap(poz);
}
int main() {
int n, m, ac, x;
FILE *fin = fopen("heapuri.in", "r");
FILE *fout = fopen("heapuri.out", "w");
fscanf(fin, "%d", &m);
n = 0;
for(int i = 0; i < m; ++i) {
fscanf(fin, "%d", &ac);
if(ac == 1) {
fscanf(fin, "%d", &x);
++n;
v[n] = x;
add(n);
} else if(ac == 2) {
fscanf(fin, "%d", &x);
erase(poz[x]);
} else
fprintf(fout, "%d\n", v[heap[1]]);
}
fclose(fin);
fclose(fout);
return 0;
}