Cod sursa(job #1947898)

Utilizator DobosDobos Paul Dobos Data 31 martie 2017 15:04:16
Problema A+B Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.58 kb
#include <bits/stdc++.h>
#define NMAX 200005

using namespace std;

ifstream fin ("heapuri.in");
ofstream fout ("heapuri.out");


int H[NMAX],P[NMAX],E[NMAX],N,NR;

void UpHeap(int k){

    while(k >> 1 > 0){
        if(H[E[k]] > H[E[k >> 1]]){
            swap(E[k],E[k >> 1]);
            P[E[k]] = k;
            P[E[k >> 1]] = k >> 1;
            k = k >> 1;
        } else
            return;
    }

}
void shift(int k){
    int son;
    do{
        son = 0;
        if(2 * k > N) continue;
        if(H[2 * k] > H[k])
            son = 2 * k;
        if(H[2 * k + 1] > H[k] && H[2 * k + 1] >  H[2 * k] && (2 * k + 1) <= N)
            son = 2 * k + 1;

        if(son){
           swap(E[k],E[son]);
           P[E[k]] = son;
           P[E[son]] = k;
        }


    }while(son);
}


int main()
{
    ios :: sync_with_stdio(false);

    int n,t,x,k = 0;

    fin >> n;

    while(n--){
        fin >> t;

        if(t == 1){
            fin >> x;
            H[++NR] = x;
            P[NR] = N;
            E[N] = NR;
            percolate(N);
        }
        if(t == 2){
            for(int i = 1; i <= N; i++)
                fout << P[i] << " " << H[i] << "\n";

            fin >> x;
            E[x] = E[N];

            P[E[N]] = P[E[x]];
                        E[x] = E[N];
            P[N] = P[x];
            N--;
            if(k > 1 && H[x] > H[x/2])
                percolate(x);
            else
                shift(x);

        }
        if(t == 3)
            fout << H[1] << "\n";
    }



    return 0;
}