Cod sursa(job #2296746)

Utilizator piscotel29Nastasa Petru-Alexandru piscotel29 Data 4 decembrie 2018 23:30:25
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3 kb
#include <iostream>
#include <fstream>
using namespace std;

const int MAX_SIZE = 200005;
typedef int Heap[MAX_SIZE];
struct O{
    int index,val,poz;
}order[MAX_SIZE];
int pos[MAX_SIZE];


inline int father(int nod){
    return nod / 2;
}

inline int leftSon(int nod){
    return nod * 2;
}

inline int rightSon(int nod){
    return nod * 2 + 1;
}

inline int max(Heap H){
    return H[1];
}

void sift(Heap H,int n,int k){
    int son;
    do{
        son = 0;
        if(leftSon(k) <= n){
            son = leftSon(k);
            if(rightSon(k) <= n && H[rightSon(k)] < H[leftSon(k)])
                son = rightSon(k);
            if(H[son] >= H[k])
                son = 0;
        }

        if(son){
            swap(H[k],H[son]);
            //swap(pos[H[k]],pos[H[son]]);
            swap(order[k].index,order[son].index);
            swap(order[k].val,order[son].val);
            k = son;
        }
    }while(son);
}

void percolate(Heap H,int n,int k){
    int key = H[k],ind = k;
    while(k > 1 && H[father(k)] > key){
        H[k] = H[father(k)];
        order[k].index = order[father(k)].index;
        order[k].val = order[father(k)].val;
        //pos[H[k]] = pos[H[father(k)]];
        k = father(k);
    }
    H[k] = key;
    //pos[H[k]] = ind;
    order[k].index = ind;
    order[k].val = key;
}

void buildHeap(Heap H,int n){

    for(int i=n/2;i>=1;--i)
        sift(H,n,i);
}

void cut(Heap H,int &n,int k){

    H[k] = H[n];
    n--;
    if(k > 1 && H[k] < H[father(k)])
        percolate(H,n,k);
    else
        sift(H,n,k);
}


void insert(Heap H,int &n, int value){
    H[++n] = value;
    percolate(H,n,n);
}

void write(Heap H,int n){

    for(int i=1;i<=n;i++)
        cout << H[i] << " ";
    cout << "\n";

}


void read(Heap H){
    fstream in("heapuri.in",ios::in);
    fstream out("heapuri.out",ios::out);
    int number_op,n = 0,ord=0;
    in >> number_op;
    for(int i=1;i<=number_op;++i){
        int code,value;
        in >> code;
        if(code == 3){
            out << max(H) << "\n";
            //buildHeap(H,n);
        }
        if(code == 1){
            in >> value;
            ++ord;
            order[ord].index = ord;
            order[ord].poz = ord;
            order[ord].val = value;
            //pos[value] = ord;
            insert(H,n,value);
        }
        if(code == 2){
            int position;
            in >> value;
            //cout << order[order[order[value].index].index].poz << " " << order[order[order[value].index].index].val << "\n";
            position = order[order[order[value].index].index].poz;
            cut(H,n,position);
        }

    }
    /*for(int i=1;i<=n;i++)
        cout << H[i] << " ";
    cout << "\n";*/
    in.close();
    out.close();
}

int main()
{
    Heap H;
    read(H);
    /*cout << "\n";
    for(int i=1;i<=4;i++)
       cout << order[i].poz << " " << order[i].index << " "<< order[i].val << "\n";*/
    return 0;
}