Cod sursa(job #1834167)

Utilizator silkMarin Dragos silk Data 24 decembrie 2016 00:23:15
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.28 kb
#include <cstdio>
#include <algorithm>
#define NMax 200000
#define oo 1<<30
using namespace std;

int heap[NMax+1];
int pos[NMax+1];
int aux[NMax+1];
int N,n;

void Inter(int a, int b)
{
    swap(heap[a], heap[b]);
    swap(pos[ aux[a] ], pos[ aux[b] ]);
    swap(aux[a], aux[b]);
}

void Coboara(int k)
{
    int fiu;
    N = n/2;
    while(k <= N)
    {
        fiu = 2*k;
        if( fiu+1 <= n && heap[fiu] > heap[fiu+1] ) ++fiu;
        if( heap[k] > heap[fiu] ) Inter(k, fiu);
        else return;
        k = fiu;
    }
}

void Urca(int k)
{
    int tata;
    while(k > 1)
    {
        tata = k/2;
        if( heap[k] < heap[tata] ) Inter(k, tata);
        else return;
        k = tata;
    }
}

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

    int c,x,T,i=0;

    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&c);
        if(c==1)
        {
            scanf("%d",&x);
            heap[++n] = x;
            pos[++i] = n;
            aux[n] = i;
            Urca(pos[i]);
        }
        else if(c==2)
        {
            scanf("%d",&x);
            heap[ pos[x] ] = oo;
            Coboara(pos[x]);
        }
        else printf("%d\n",heap[1]);
    }


return 0;
}