Cod sursa(job #2811484)

Utilizator mihnea.cazan15mihnea cazan mihnea.cazan15 Data 2 decembrie 2021 12:56:04
Problema Heapuri Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.61 kb
#include <bits/stdc++.h>

using namespace std;
const int MAX_HEAP_SIZE = 200005;
typedef int Heap[MAX_HEAP_SIZE];

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

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

inline int right_son(int nod) {
    return nod * 2 + 1;
}
void up(Heap H,int N, int nod)
{
    int key=H[nod];
    while(nod>1 && H[nod]>H[father(nod)])
    {
        H[nod]=H[father(nod)];
        nod=father(nod);
    }
    H[nod]=key;
}
void down(Heap H,int N,int nod)
{
    while(69420)
    {
        int son=0;
        if(left_son(nod)<N)
           son=left_son(nod);
        if(right_son(nod)<=N && H[right_son(nod)]>H[left_son(nod)])
           son=right_son(nod);
        if(son)
        {
            if(H[nod]<H[son])
            {
                swap(H[nod],H[son]);
                nod=son;
            }
            else if(H[nod]>=H[son])
                    break;
        }
        else if(!son)
                break;
    }
}
void insert(Heap H,int &N,int key)
{
    H[++N]=key;
    up(H,N,N);
}
vector<int> ord;
int main()
{
    ifstream cin("heapuri.in");
    ofstream cout("heapuri.out");
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
       int tip,nod,key;
       cin>>tip;
       if(tip==1)
       {
           cin>>key;
           insert(H,N,key);
           ord.push_back(key);
       }
       if(tip==2)
       {
           int poz;
           cin>>poz;
           key=ord[poz-1];
           sterge(H,N,key);
       }
       if(tip==3)
          cout<<H[1]<<"\n";
    }
    // trb terminata
    return 0;
}