Cod sursa(job #2761164)

Utilizator MihaiBirsanMihai Birsan MihaiBirsan Data 30 iunie 2021 22:48:15
Problema Heapuri Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.5 kb
#include <bits/stdc++.h>

using namespace std;

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


int n, cmd, x;
//int v[200001], heap[200001], poz[200001];
//vector<int> v;
priority_queue <int, vector<int>, greater<int> > pq;


int main()
{
    //int k = 1, ok = 0, c = 1;
    //int pos[200001],a[200001];
    vector<int> v;
    vector<int> poz;

    fin >> n;
    for(int i = 1; i <= n; i++)
    {
        fin >> cmd ;;
        if(cmd == 1)
        {
            fin >> x;
            pq.push(x);
            poz.push_back(x);
        }
        if(cmd == 2)
        {
            fin >> x;

            if(poz[x] == pq.top())
            {
                pq.pop();
            }
            else
            {
                //c = 1;
                while(pq.empty() == false)
                {
                    if(poz[x-1] == pq.top())
                    {
                        pq.pop();
                    }
                    else
                    {
                        v.push_back(pq.top());
                        //a[c] = pq.top();
                        pq.pop();
                        //c++;
                    }

                }
                for(int j = 0; j< v.size();j++)
                {
                    //fout<<v[j]<<' ';
                    pq.push(v[j]);
                }
                v.clear();
            }
        }
        if(cmd == 3)
            fout << pq.top() << "\n";
    }

    return 0;
}