Pagini recente » Cod sursa (job #1507805) | Cod sursa (job #2872260) | Cod sursa (job #3154462) | Cod sursa (job #2295509) | Cod sursa (job #580663)
Cod sursa(job #580663)
#include <cstdio>
#include <cassert>
const int MAX_N = 200001;
int n, vSize, heapSize;
int v[MAX_N], heap[MAX_N], pos[MAX_N];
void swap ( int &a, int &b ) {
a ^= b ^= a ^= b;
}
void insert ( int x ) {
++heapSize; heap[heapSize] = x;
pos[heap[heapSize]] = heapSize;
for (int k = heapSize; k > 1 && v[heap[k/2]] > v[heap[k]]; k = k/2) {
swap(heap[k/2], heap[k]);
pos[heap[k]] = k;
pos[heap[k/2]] = k/2;
}
}
void pop() {
swap(heap[1], heap[heapSize]);
pos[heap[1]] = 1;
--heapSize;
for (int k = 1; k <= heapSize; ) {
if (k*2 <= heapSize && v[heap[k]] > v[heap[k*2]]) {
swap(heap[k], heap[k*2]);
pos[heap[k]] = k;
pos[heap[k*2]] = k*2;
k = k*2;
} else
if (k*2+1 <= heapSize && v[heap[k]] > v[heap[k*2+1]]) {
swap(heap[k], heap[k*2+1]);
pos[heap[k]] = k;
pos[heap[k*2+1]] = k*2+1;
k = k*2+1;
} else {
break;
}
}
}
void pop ( int x ) {
for (int k = pos[x]; k > 1; k = k/2) {
swap(heap[k/2], heap[k]);
pos[heap[k]] = k;
pos[heap[k/2]] = k/2;
}
pop();
}
int main() {
freopen("heapuri.in","rt",stdin);
freopen("heapuri.out","wt",stdout);
scanf("%d",&n);
for (int i = 0, op, x; i < n; ++i) {
scanf("%d",&op);
if (op == 1) {
scanf("%d",&v[vSize]); ++vSize;
insert(vSize-1);
} else
if (op == 2) {
scanf("%d",&x);
--x;
pop(x);
} else
if (op == 3) {
printf("%d\n",v[heap[1]]);
}
/* for (int i = 1; i <= heapSize; ++i) {
// fprintf(stderr,"Asserting (pos[heap[%d] = %d] = %d) == %d\n",i,heap[i],pos[heap[i]],i);
assert(pos[heap[i]] == i);
}*/
}
return 0;
}