Cod sursa(job #1025315)

Utilizator sebinechitasebi nechita sebinechita Data 9 noiembrie 2013 19:51:18
Problema Heapuri Scor 10
Compilator cpp Status done
Runda Arhiva educationala Marime 1.78 kb
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
//#include <cmath>
#include <queue>
#include <deque>
#include <iomanip>
using namespace std;

ifstream fin("heapuri.in");
ofstream fout("heapuri.out");
#define baza 10
#define MAX 201000

typedef long long int lli;

int x[MAX], heap[MAX], fr[MAX], z;

void put(int i)
{
    heap[++z]=i;
    fr[i]=z;
    int c=z;
    while(x[heap[c]]<x[heap[c/2]] && c>=1)
    {
        swap(heap[c],heap[c/2]);
        swap(fr[heap[c]],fr[heap[c/2]]);
        c/=2;
    }
}

void elimina(int i)
{

    swap(heap[i],heap[z]);
    swap(fr[heap[i]],fr[heap[z]]);

    z--;
    if(i>z)
    {
        return;
    }
    int c=i;
    while(x[heap[c]]<x[heap[c/2]] && c>=1)
    {
        swap(heap[c],heap[c/2]);
        swap(fr[heap[c]],fr[heap[c/2]]);
        c/=2;
    }

    while((x[heap[c]]>x[heap[2*c]] || x[heap[c]]>x[heap[2*c+1]]) && 2*c<=z)
    {
        if(x[heap[c]]>x[heap[2*c]])
        {
            swap(heap[c],heap[c*2]);
            swap(fr[heap[c]],fr[heap[c*2]]);
            c=c*2;
        }
        else
        {
            if(2*c+1>z)
                return;
            swap(heap[c],heap[c*2+1]);
            swap(fr[heap[c]],fr[heap[c*2+1]]);
            c=c*2+1;
        }
    }

}

int main()
{
    int i, n=0,l,s, t,j;
    fin>>t;
    for(i=1;i<=t;i++)
    {
        fin>>l;
        if(l==1)
        {
            fin>>s;
            n++;
            x[n]=s;
            put(n);
        }
        if(l==2)
        {

            fin>>s;
           // x[s]=-1;
            elimina(fr[s]);

        }
        if(l==3)
        {
            fout<<x[heap[1]]<<"\n";
        }
    }




    return 0;
}