Cod sursa(job #2892776)

Utilizator Cosmina_GheorgheGheorghe Cosmina Cosmina_Gheorghe Data 23 aprilie 2022 16:08:44
Problema Heapuri Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.95 kb
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;

ifstream f("heapuri.in");
ofstream g("heapuri.out");
struct elem
{
    int val, poz;
};
int aux[100000];
vector<elem> heapp;
void urca(int poz) {
    while (poz) {
    int tata = (poz - 1) / 2;
    if (heapp[tata].val > heapp[poz].val) {
    swap(heapp[tata], heapp[poz]);
    swap(aux[heapp[tata].poz],aux[heapp[poz].poz]);
    poz = tata;
    } else {
    break;
    }
    }
}

void push(elem yy) {
heapp.push_back(yy);
urca(heapp.size()-1);
}

void coboara(int poz) {
if (poz * 2 + 1 >= heapp.size())
    return;
int fiu_st = heapp[poz * 2 + 1].val;
if ((poz * 2 + 2 == heapp.size()) || fiu_st < heapp[poz * 2 + 2].val) {
    if (fiu_st < heapp[poz].val) {
        swap(heapp[poz], heapp[poz * 2 + 1]);
        swap(aux[heapp[poz].poz],aux[heapp[poz*2+1].poz]);
        coboara(poz * 2 + 1);
        return;
}
    else { return;
    }
}
else {
if (heapp[poz * 2 + 2].val < heapp[poz].val) {
    swap(heapp[poz], heapp[poz * 2 + 2]);
    swap(aux[heapp[poz].poz],aux[heapp[poz*2+2].poz]);
    coboara(poz * 2 + 2);
    return;
}
else {
return;
}
}}
int main()
{
    int n,x,y;
   f >> n;
   int cnt=0;
    for(int k = 0; k < n; k++)
    {
        f>>x;
        if(x==1)
        {   cnt++;
            f>>y;
            elem yy;
            yy.poz=cnt;
            yy.val=y;
            aux[yy.poz]=heapp.size();
            push(yy);
        }
        if(x==2)
        {
            f>>y;
            int kk=aux[y];
            swap(heapp[kk], heapp[heapp.size() - 1]);
            swap(aux[heapp[kk].poz],aux[heapp[heapp.size()-1].poz]);
            heapp.pop_back();
            urca(kk);
            coboara(kk);
        }
        if(x==3)
        {
            g<<heapp[0].val<<'\n';
        }
    }
    /*for(int i = 0; i < heapp.size(); i++)
        cout << heapp[i].val << " ";
    cout<<endl;*/

    return 0;
}