Cod sursa(job #3331226)

Utilizator NutaAlexandruASN49K NutaAlexandru Data 25 decembrie 2025 20:06:24
Problema Heapuri Scor 100
Compilator c-64 Status done
Runda Arhiva educationala Marime 2.98 kb
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdbool.h>
#include <ctype.h>

#define LG 20
#define UNUSED -1

/////////////////
///Read the data
#define BUFF_SIZE 4096
FILE *fin;
int it = BUFF_SIZE - 1;
char buff[BUFF_SIZE];
int get_char()
{
    it++;
    if (it == BUFF_SIZE)
    {
        fread(buff, sizeof(char), BUFF_SIZE, fin);
        it = 0;
    }
    return buff[it];
}

int read_int()
{
    int x = 0, sign = 1;
    char ch = get_char();
    while (ch != '-' && !isdigit(ch))
    {
        ch = get_char();
    }
    if (ch == '-')
    {
        sign = -1;
        ch = get_char();
    }

    while (isdigit(ch))
    {
        x = x * 10 + (ch - '0');
        ch = get_char();
    }
    return sign * x;
}


/////////////////
#define N_MAX 200000

int heap_sz, cnt_added;
int heap[N_MAX + 1];
int pos_heap[N_MAX], val[N_MAX];

void swap(int *x,int *y)
{
    int aux = *x;
    *x = *y;
    *y = aux;
    return;
}

int parent(int x)
{
    return x / 2;
}

int left_child(int x)
{
    return 2 * x;
}

int right_child(int x)
{
    return 2 * x + 1;
}

int cmp(int x, int y)
{
    return val[heap[x]] - val[heap[y]];
}

bool better(int x, int y)
{
    return cmp(x, y) < 0;
}

void swap_heap(int x, int y)
{
    swap(&pos_heap[heap[x]], &pos_heap[heap[y]]);
    swap(&heap[x], &heap[y]);
}

void push_up(int node)
{
    while (node != 1 && better(node, parent(node)))
    {
        swap_heap(node, parent(node));
        node = parent(node);
    }
}

void push_down(int node)
{
    while (right_child(node) <= heap_sz  && (better(left_child(node), node) || better(right_child(node), node)))
    {
        if (better(left_child(node), right_child(node)))
        {
            swap_heap(node, left_child(node));
            node = left_child(node);
        }
        else
        {
            swap_heap(node, right_child(node));
            node = right_child(node);
        }
    }
    if (left_child(node) <= heap_sz && better(left_child(node), node))
    {
        swap_heap(node, left_child(node));
        node = left_child(node);
    }
}

void push(int x)
{
    heap[++heap_sz] = cnt_added;
    pos_heap[cnt_added] = heap_sz;
    val[cnt_added] = x;
    push_up(heap_sz);
    cnt_added++;
    return;
}

void erase(int p)
{
    int pos = pos_heap[p];
    //printf("erase : %d %d\n", val[p], p);
    swap_heap(pos, heap_sz);
    heap_sz--;
    push_up(pos);
    push_down(pos);
}

int min()
{
    return val[heap[1]];
}

int main(void)
{
    setbuf(stdout, NULL);
    fin = fopen("heapuri.in", "r");
    FILE *fout = fopen("heapuri.out", "w");
    assert(fin != NULL);
    assert(fout != NULL);
    int q = read_int();
    while (q--)
    {
        int cer = read_int();
        if (cer == 1)
        {
            int val = read_int();
            push(val);
        }
        else if (cer == 2)
        {
            int x = read_int();
            x--;
            erase(x);
        }
        else
        {
            fprintf(fout, "%d\n", min());
        }
    }
    fclose(fin);
    fclose(fout);
    return 0;
}