Cod sursa(job #794130)

Utilizator CrescentselectJicol Crescent Crescentselect Data 5 octombrie 2012 18:38:39
Problema Heapuri Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 1.55 kb
#include <iostream>
#include<fstream>

using namespace std;
ifstream in ("heapuri.in");
ofstream out("heapuri.out");

int n , v[200001], pozitii[200001], lungime,poz,h[200001];

void schimb (int &a, int &b)
{
    int tmp=a;
    a=b;
    b=tmp;
}
void urca(int x)
{
    while (x> 1 && v[h[x]]<v[h[x/2]])
    {
        schimb(h[x],h[x/2]);
        pozitii[h[x]]=x;
        pozitii[h[x/2]]=x/2;
        x/=2;
    }
}
void coboara (int x)
{
    int fs= 2*x;
    int fd=x*2+1;
    int bun =x;
    if(fs<=lungime && v[h[fs]]<v[h[bun]])
        bun=fs;
    if (fd<=lungime && v[h[fd]]<v[h[bun]])
        bun=fd;
    if(bun !=x)
    {
        //cout<<"schimb"<<h[bun]<<" cu "<<h[x]<<endl;
        schimb(h[bun],h[x]);
        pozitii [h[bun]]=bun;
        pozitii[h[x]]=x;
        coboara (bun);
    }
}
void taie(int x)
{
    h[x]=h[lungime--];
    pozitii[h[x]]=x;
    if(x>1 && v[h[x]] < v[h[x/2]])
        urca(x);
    coboara(x);
}
int main()
{
    in >>n;
    int tmp, x;
    for( int i=1;i<=n;i++)
    {
        in >>tmp;
        if (tmp==1)
        {
            in>>v[++poz];
            h[++lungime]=poz;
            pozitii[h[lungime]] = lungime;
            urca(lungime);
        }
        else if (tmp==2)
        {
            in>>x;
            taie(pozitii[x]);
        }
        else
        {
            out<<v[h[1]]<<endl;
        }
    }
    /*
    for ( int i=1;i<=lungime ;i++)
    {
     //   cout<<v[i]<<" ";
    }
    //cout<<endl;
    //cout <<lungime <<endl;
    */
    return 0;
}