Cod sursa(job #3266055)

Utilizator happyplaneDragos Miu-Baldu happyplane Data 5 ianuarie 2025 15:41:58
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.32 kb
#include <iostream>
#include <fstream>
#define Nmax 200001
using namespace std;

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

int n, nr, lung, cod, x, position;
int poz[Nmax];
pair<int, int> H[Nmax + 5];

void push_top(int x)
{
  while (x > 1)
  {
    if (H[x].first < H[x / 2].first)
    {
      swap(H[x], H[x / 2]);
      swap(poz[H[x].second], poz[H[x / 2].second]);
      x /= 2;
    }
    else break;
  }
}

void push_down(int x, int n)
{
  while (2 * x <= n)
  {
    int p = 2 * x;
    if (p + 1 <= n && H[p + 1].first < H[p].first)
      p++;
    if (H[x].first < H[p].first)
      return;
    else
    {
      swap(H[x], H[p]);
      swap(poz[H[x].second], poz[H[p].second]);
      x = p;
    }
  }
}

int main()
{
  fin >> n;
  for (int i = 1;i <= n;i++)
  {
    fin >> cod;
    if (cod == 1)
    {
      fin >> x;
      nr++, lung++;
      poz[nr] = lung;
      H[lung].first = x;
      H[lung].second = nr;
      push_top(lung);
    }
    else
      if (cod == 2)
      {
        fin >> position;
        int p = poz[position];
        swap(H[p], H[lung]);
        swap(poz[H[p].second], poz[H[lung].second]);
        lung--;
        push_top(p);
        push_down(p, lung);
      }
      else
        fout << H[1].first << '\n';
  }
  return 0;
}