Cod sursa(job #1491202)

Utilizator GabiADAndrei Gabriel GabiAD Data 24 septembrie 2015 22:09:37
Problema Heapuri Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 2.86 kb
#include <stdio.h>
#include <stdlib.h>

struct nod
{
    int val;
    struct nod *urm;
};

int a[200000], n, m, n_ord;
int ord[200001];
struct nod *deleted[10000], *p;

void siftUp(int index)
{
    int tata = (index-1)/2, aux;

    if(a[index] < a[tata])
    {
        aux = a[index];
        a[index] = a[tata];
        a[tata] = aux;

        siftUp(tata);
    }
}

void siftDown(int index)
{
    int fiu1 = 2*index + 1;
    int fiu2 = 2*index + 2, aux;

    if(fiu1 >= n)
        return;
    else if(fiu2 >= n && (a[index] > a[fiu1]))
    {
        aux = a[index];
        a[index] = a[fiu1];
        a[fiu1] = aux;
    }
    else if((a[index] > a[fiu1]) && (a[fiu1] < a[fiu2]))
    {
        aux = a[index];
        a[index] = a[fiu1];
        a[fiu1] = aux;

        siftDown(fiu1);
    }
    else if((a[index] > a[fiu2]) && (a[fiu2] <= a[fiu1]))
    {
        aux = a[index];
        a[index] = a[fiu2];
        a[fiu2] = aux;

        siftDown(fiu2);
    }

}

void push(int x)
{
    n++;
    a[n-1] = x;

    siftUp(n-1);
}

int pop()
{
    int i, au = a[0];
    a[0] = a[--n];
    siftDown(0);

    return au;
}


int eSters(int x)
{
    p = deleted[x%10000];
/*
    while(p != NULL)
    {
        if(p->val == x)
        {
            return 1;
        }
        p = p->urm;
    }*/

    if((p != NULL) && (p->val == x))
    {
        deleted[x%10000] = p->urm;
        return 1;
    }

    if(p != NULL)
        while(p->urm != NULL)
        {
            if(p->urm->val == x)
            {
                p->urm = p->urm->urm;
                return 1;
            }
            p = p->urm;
        }

    return 0;
}


int main()
{
    freopen("heapuri.in", "r", stdin);
    freopen("heapuri.out", "w", stdout);


    int i, j, x, instr, n_ex;
    scanf("%d\n", &n_ex);


    for(i = 0; i < n_ex; i++)
    {
        scanf("%d", &instr);
        if(instr == 1)
        {
            scanf("%d", &x);
            push(x);
            ord[++n_ord] = x;
            //printf("ord[%d] = %d\n", n_ord, x);
        }
        else if(instr == 2)
        {
            scanf("%d", &x);

            if(deleted[ord[x]%10000] == NULL)
            {
                deleted[ord[x]%10000] = (struct nod*)malloc(sizeof(struct nod));
                deleted[ord[x]%10000]->val = ord[x];
                deleted[ord[x]%10000]->urm = NULL;
            }
            else
            {
                p = (struct nod*)malloc(sizeof(struct nod));
                p->val = ord[x];
                p->urm = deleted[ord[x]%10000];
                deleted[ord[x]%10000] = p;
            }

        }
        else if(instr == 3)
        {
            while(eSters(a[0]))
            {
                //printf("Elementul a[0] = %d este sters", a[0]);
                pop();
            }
            printf("%d\n", a[0]);

        }
    }





    return 0;
}