Cod sursa(job #1490872)

Utilizator AdrianaMAdriana Moisil AdrianaM Data 24 septembrie 2015 12:21:23
Problema Heapuri Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
#include <fstream>
#include <vector>
#include <stack>
#include <bitset>
#define INF 0x3f3f3f3f
using namespace std;

ifstream is("heapuri.in");
ofstream os("heapuri.out");

int n, el;
int h[200001], nr[200001], p[200001];

void Read();
void Insert(int x);
void Delete(int poz);

int main()
{
    Read();
    is.close();
    os.close();
    return 0;
}

void Read()
{
    int m, tip, x;
    is >> m;
    for ( int i = 1; i <= m; ++i )
    {
        is >> tip;
        if ( tip != 3 )
        {
            is >> x;
            if ( tip == 1 )
                Insert(x);
            else
                Delete(p[x]);
        }
        else
           os << nr[h[1]] << "\n";

    }
   /* for ( int i = 1; i <= el; ++i )
        os << p[h[i]] << " " << h[i] << " " << nr[h[i]] << "\n";
    os << "\n";*/
}

void Delete(int poz)
{
    if ( poz == n )
    {
        --n;
        return;
    }
    h[poz] = h[n--];
    p[h[poz]] = poz;
    int up = poz, down = 2 * poz;
    while ( ( down <= n && nr[h[up]] > nr[h[down]] ) || ( down < n && nr[h[up]] > nr[h[down + 1]] ) )
    {
        if ( down < n && nr[h[down]] > nr[h[down + 1]] )
            ++down;
        swap(h[up], h[down]);
        swap(p[h[up]], p[h[down]]);
        up = down;
        down *= 2;
    }
}

void Insert(int x)
{
    h[++n] = ++el;
    p[el] = n;
    nr[h[n]] = x;
    int down = n, up = n / 2;
    while ( up && nr[h[down]] < nr[h[up]] )
    {
        swap(h[down], h[up]);
        swap(p[h[down]], p[h[up]]);
        down = up;
        up /= 2;
    }
}