Pagini recente » Cod sursa (job #1782449) | Cod sursa (job #1174652) | Cod sursa (job #588507) | Cod sursa (job #2207153) | Cod sursa (job #2267144)
#include <stdio.h>
#include <unistd.h>
#define NMAX 200000
#define CHUNK (1 << 22)
static char buf[CHUNK], obuf[CHUNK];
static size_t bpos, oblen;
static inline int read_int(void)
{
int x = 0;
while ('0' <= buf[bpos] && buf[bpos] <= '9') {
x = x * 10 + buf[bpos] - '0';
bpos++;
}
bpos++;
return x;
}
static inline void write_int(int x)
{
int aux = 1;
while (aux * 10 <= x) {
aux *= 10;
}
do {
obuf[oblen++] = '0' + x / aux;
x %= aux;
aux /= 10;
} while (aux);
obuf[oblen++] = '\n';
}
static struct heap_elem {
int x;
int t;
} heap[NMAX+1];
static int idx_at_time[NMAX+1], heapsize;
static inline void insert(int x, int time)
{
struct heap_elem aux;
int new_idx;
aux.x = x;
aux.t = time;
++heapsize;
heap[heapsize] = aux;
new_idx = heapsize;
while (new_idx > 1 && heap[new_idx / 2].x >= heap[new_idx].x) {
aux = heap[new_idx / 2];
heap[new_idx / 2] = heap[new_idx];
heap[new_idx] = aux;
idx_at_time[heap[new_idx].t] = new_idx;
new_idx /= 2;
}
idx_at_time[time] = new_idx;
}
static inline void delete(int time)
{
struct heap_elem aux;
int new_idx = idx_at_time[time], aux_idx, swapped = 0;
heap[new_idx] = heap[heapsize];
idx_at_time[heap[new_idx].t] = new_idx;
heapsize--;
while (new_idx * 2 <= heapsize) {
aux_idx = new_idx;
if (new_idx * 2 <= heapsize && heap[new_idx].x >= heap[new_idx * 2].x) {
aux_idx = new_idx * 2;
}
if (new_idx * 2 + 1 <= heapsize &&
heap[aux_idx].x >= heap[new_idx * 2 + 1].x) {
aux_idx = new_idx * 2 + 1;
}
if (new_idx == aux_idx) {
break;
}
swapped = 1;
aux = heap[aux_idx];
heap[aux_idx] = heap[new_idx];
heap[new_idx] = aux;
idx_at_time[heap[aux_idx].t] = aux_idx;
idx_at_time[heap[new_idx].t] = new_idx;
new_idx = aux_idx;
}
if (!swapped) {
while (new_idx > 1 && heap[new_idx / 2].x >= heap[new_idx].x) {
aux = heap[new_idx / 2];
heap[new_idx / 2] = heap[new_idx];
heap[new_idx] = aux;
idx_at_time[heap[new_idx].t] = new_idx;
new_idx /= 2;
}
idx_at_time[time] = new_idx;
}
}
int main(void)
{
int n, op, x = 0, time;
freopen("heapuri.in", "r", stdin);
freopen("heapuri.out", "w", stdout);
read(STDIN_FILENO, buf, sizeof buf);
n = read_int();
time = 0;
while (n--) {
op = read_int();
if (op != 3) {
x = read_int();
}
if (op == 1) {
time++;
insert(x, time);
}
if (op == 2) {
delete(x);
}
if (op == 3) {
write_int(heap[1].x);
}
}
write(STDOUT_FILENO, obuf, oblen);
return 0;
}