Cod sursa(job #3371812)

Utilizator Cezar2009Cezar Mihai Titihazan Cezar2009 Data 31 octombrie 2026 21:21:32
Problema Heapuri Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 7.94 kb
// https://www.infoarena.ro/problema/heapuri

#pragma GCC optimize("Ofast,unroll-loops")
// #pragma GCC optimize("O3,unroll-loops")
#pragma GCC target(                                                            \
    "avx512f,avx512bw,avx512dq,avx512cd,avx512vl,bmi,bmi2,lzcnt,popcnt,fma")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt,fma")

// #define _USE_MATH_DEFINES

#include <bits/stdc++.h>

using namespace std;

using int64 = int64_t;
using uint64 = uint64_t;
using int32 = int32_t;
using uint32 = uint32_t;
using int16 = int16_t;
using uint16 = uint16_t;
using pii = pair<int, int>;
using pll = pair<int64, int64>;

#define all(x) (x).begin(), (x).end()
#define allg(x) (x).begin(), (x).end(), greater<int>()
#define sz(x) (int)(x).size()
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rfor(i, st, dr) for (auto i = (st); i <= (dr); ++i)
#define for0(i, n) for (auto i = 0; i < (n); ++i)
#define rfor0(i, n) for (auto i = (n) - 1; i >= 0; --i)
#define for1(i, n) for (auto i = 1; i <= (n); ++i)
#define rfor1(i, n) for (auto i = (n); i >= 1; --i)
#define foreach(x, a) for (auto &x : a)
#define cforeach(x, a) for (const auto &x : a)
#define ft first
#define sd second
#define cendl cout << "\n"
#define fendl fout << "\n"
#define FASTIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(nullptr);

// #define int int64

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

// FILE* fin = fopen("", "r");
// FILE* fout = fopen("", "w");

const int NRMAX = 200000;

template <typename T, typename HashFunc = std::hash<T>> class HashMap {
private:
  int MOD;
  HashFunc hasher;

  std::vector<std::vector<std::pair<T, int>>> v;

  size_t hashF(const T &x) const { return hasher(x) % MOD; }

public:
  HashMap(int mod) : MOD(mod) { v.resize(MOD); }

  void insert(const T &x) {
    size_t nr = hashF(x);

    for (auto &it : v[nr]) {
      if (it.first == x) {
        it.second++;
        return;
      }
    }
    v[nr].push_back(std::make_pair(x, 1));
  }

  void erase(const T &x) {
    size_t nr = hashF(x);

    for (size_t i = 0; i < v[nr].size(); ++i) {
      if (v[nr][i].first == x) {
        if (--v[nr][i].second > 0)
          return;

        v[nr][i] = v[nr].back();
        v[nr].pop_back();
        return;
      }
    }
  }

  typename std::vector<std::pair<T, int>>::iterator find(const T &x) {
    size_t nr = hashF(x);

    for (size_t i = 0; i < v[nr].size(); ++i)
      if (v[nr][i].first == x)
        return v[nr].begin() + i;
    return v[nr].end();
  }

  int count(const T &x) const {
    size_t nr = hashF(x);
    for (size_t i = 0; i < v[nr].size(); ++i)
      if (v[nr][i].first == x)
        return v[nr][i].second;
    return 0;
  }

  int countOnes() const {
    int rez = 0;
    for (int nr = 0; nr < MOD; ++nr) {
      for (size_t i = 0; i < v[nr].size(); ++i) {
        if (v[nr][i].second == 1)
          ++rez;
      }
    }
    return rez;
  }

  int size() const {
    int sum = 0;
    for (int i = 0; i < MOD; ++i)
      sum += (int)v[i].size();
    return sum;
  }

  bool empty() const { return (size() == 0); }

  void clear() {
    for (int i = 0; i < MOD; ++i)
      v[i].clear();
  }

  void print() const {
    for (int i = 0; i < MOD; ++i) {
      if (!v[i].empty()) {
        std::cout << i << ": ";
        for (size_t j = 0; j < v[i].size(); ++j) {
          std::cout << v[i][j].first << " " << v[i][j].second;
          std::cout << " ";
        }
        std::cout << "\n";
      }
    }
  }

  int &operator[](const T &x) {
    auto it = find(x);

    if (it != v[hashF(x)].end())
      return it->second;

    v[hashF(x)].push_back({x, 0});
    return v[hashF(x)].back().second;
  }

  struct iterator {
    int buc;
    size_t ind;
    HashMap *h;

    iterator(int b, size_t i, HashMap *hashPtr) : buc(b), ind(i), h(hashPtr) {}

    std::pair<T, int> &operator*() { return h->v[buc][ind]; }

    iterator &operator++() {
      ++ind;
      while (buc < h->MOD && ind >= h->v[buc].size()) {
        ++buc;
        ind = 0;
      }
      return *this;
    }
    bool operator==(const iterator &other) const {
      return h == other.h && buc == other.buc && ind == other.ind;
    }
    bool operator!=(const iterator &other) const { return !(*this == other); }
    std::pair<T, int> *operator->() { return &h->v[buc][ind]; }
  };

  iterator begin() {
    int b = 0;
    while (b < MOD && v[b].empty())
      b++;
    return iterator(b, 0, this);
  }

  iterator end() { return iterator(MOD, 0, this); }
};

// greater<T> -> max heap
// less<T> -> min heap
template <typename T, typename Compare = std::less<T>> class Heap {
private:
  T *heap;
  int size;
  int capacity;
  Compare comp;

  int parent(int i) { return (i - 1) / 2; }
  int leftChild(int i) { return 2 * i + 1; }
  int rightChild(int i) { return 2 * i + 2; }

  void shiftUp(int i) {
    while (i > 0 && comp(heap[i], heap[parent(i)])) {
      std::swap(heap[i], heap[parent(i)]);
      i = parent(i);
    }
  }

  void shiftDown(int i) {
    while (true) {
      int best = i;
      int left = leftChild(i);
      int right = rightChild(i);

      if (left < size && comp(heap[left], heap[best]))
        best = left;

      if (right < size && comp(heap[right], heap[best]))
        best = right;

      if (best == i)
        break;

      std::swap(heap[i], heap[best]);
      i = best;
    }
  }

public:
  Heap(int capacity) : heap(new T[capacity]), size(0), capacity(capacity) {}
  // mai ar merge un constructor aici pentru cand primeste si un array

  ~Heap() { delete[] heap; }

  // dezactivez copierea ca nu este sigur ca copiez new
  // habarnam asa o zis chatgpt am incredere in el
  Heap(const Heap &) = delete;
  Heap &operator=(const Heap &) = delete;

  bool empty() const { return size == 0; }
  int getSize() const { return size; }

  void insert(const T &nr) {
    // as putea sa verific daca e full
    if (size == capacity)
      throw std::overflow_error("Heap is full");

    heap[size] = nr;
    shiftUp(size);
    ++size;
  }

  void update(int i, const T &nr) {
    if (i < 0 || i >= size)
      throw std::out_of_range("Invalid index");

    heap[i] = nr;
    if (i && comp(heap[i], heap[parent(i)]))
      shiftUp(i);
    else
      shiftDown(i);
  }

  void remove(int i) {
    if (i < 0 || i >= size)
      throw std::out_of_range("Invalid index");

    if (i == size - 1) {
      size--;
      return;
    }

    heap[i] = heap[--size];

    if (i > 0 && comp(heap[i], heap[parent(i)]))
      shiftUp(i);
    else
      shiftDown(i);
  }

  T removeTop() {
    if (empty())
      throw std::underflow_error("Heap is empty");

    T top = getTop();

    --size;
    if (size > 0) {
      heap[0] = heap[size];
      shiftDown(0);
    }

    return top;
  }

  const T &getTop() const {
    if (empty())
      throw std::underflow_error("Heap is empty");

    return heap[0];
  }

  void buildHeap() {
    for (int i = size / 2 - 1; i >= 0; --i)
      shiftDown(i);
  }
};

int32 main() {
  // FASTIO;

  vector<int> v;
  v.reserve(NRMAX);
  Heap<int> hp(NRMAX);
  HashMap<int> mp(NRMAX);

  int n, c, x;

  fin >> n;
  while (n--) {
    fin >> c;
    if (c == 1) {
      fin >> x;
      hp.insert(x);
      v.pb(x);
    } else if (c == 2) {
      fin >> x; // pozitia care trebuie eliminata
      // cout << v[x - 1] << " ";
      mp[v[x - 1]] = true;
    } else {
      while (mp[hp.getTop()]) {
        // cout << hp.getTop() << " ";
        mp[hp.removeTop()] = 0;
      }
      // cout << "\n";

      fout << hp.getTop() << "\n";
    }
  }
  // while (hp.empty()) {
  //   cout << hp.getTop() << " ";
  //   hp.removeTop();
  // }
  // cout << "\n";

  return 0;
}