Cod sursa(job #2506600)

Utilizator NaritaandreiCNAINarita Andrei NaritaandreiCNAI Data 8 decembrie 2019 14:54:53
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.79 kb
#include <iostream>
#include <stdio.h>
using namespace std;
FILE *f,*g;
int h[200002],cate[200002],poz[200002];
int n,nfake;
void shift(int node)
{
    int key,catea,son;
    key=h[node];
    catea=cate[node];
    do
    {
        son=0;
        if(node*2<=n)
        {
                son=node*2;
            if(node*2+1<=n && h[node*2+1]<h[node*2])
                son=node*2+1;
            if(h[son]>=h[node])
                son=0;
        }
        if(son)
        {
            h[node]=h[son];
            cate[node]=cate[son];
            poz[cate[son]]=node;
            node=son;
        }
    }while(son);
    h[node]=key;
    cate[node]=catea;
    poz[cate[node]]=node;
}
void percolate(int node)
{
    int key,catea;
    key=h[node];
    catea=cate[node];
    while(node>1 && key<h[node/2])
    {
        h[node]=h[node/2];
        poz[cate[node/2]]=node;
        cate[node]=cate[node/2];
        node/=2;
    }
    h[node]=key;
    cate[node]=catea;
    poz[cate[node]]=node;
}
void cut(int x)
{
    h[poz[x]]=h[n];
    poz[cate[n]]=poz[x];
    cate[poz[x]]=cate[n];
    n--;
    percolate(poz[x]);
    shift(poz[x]);
}

void insert_heap(int node)
{
    h[++n]=node;
    poz[++nfake]=n;
    cate[n]=nfake;
    percolate(n);
}
void read()
{   int nr,x,t;
    fscanf(f,"%d",&nr);
    for(int i=1; i<=nr; i++)
    {
        fscanf(f,"%d",&t);
        if(t==1)
        {
            fscanf(f,"%d",&x);
            insert_heap(x);
        }
        else if(t==2)
        {
            fscanf(f,"%d",&x);
            cut(x);
        }
        else
            fprintf(g,"%d\n",h[1]);
    }
}
int main()
{
    f=fopen("heapuri.in","r");
    g=fopen("heapuri.out","w");
    read();

    fclose(f);
    fclose(g);
    return 0;
}