icpc-snippet

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub EarthMessenger/icpc-snippet

:heavy_check_mark: Double Ended Priority Queue
(lib/ds/depque.hpp)

Depends on

Verified with

Code

#pragma once
#include "lib/internal.hpp"
#include <queue>

/**
 * @brief Double Ended Priority Queue
 *
 * @tparam T    elements' type
 * @tparam Comp compare for min opt
 */
template <typename T, class MinComp = std::less<T>,
          class MaxComp = std::greater<T>>
class DEPQ
{
private:
  u32 sz;
  vec<T> a;
  vec<char> del;

  template <typename C> struct PosComp
  {
    const vec<T> &v;
    C comp;
    explicit PosComp(const vec<T> &u, C c) : v(u), comp(c) {}
    bool operator()(u32 x, u32 y) { return comp(v[x], v[y]); }
  };

  std::priority_queue<u32, vec<u32>, PosComp<MinComp>> mnh; // min heap
  std::priority_queue<u32, vec<u32>, PosComp<MaxComp>> mxh; // max heap

  void fresh_min()
  {
    while (!mnh.empty() && del[mnh.top()]) mnh.pop();
  }
  void fresh_max()
  {
    while (!mxh.empty() && del[mxh.top()]) mxh.pop();
  }

public:
  DEPQ(const MinComp &mnc = MinComp(), const MaxComp &mxc = MaxComp())
      : sz(), a(), del(), mnh(PosComp<MinComp>(a, mnc)), mxh(PosComp<MaxComp>(a, mxc))
  {
  }
  template <typename It>
  DEPQ(It first, It second, const MinComp &mnc = MinComp(),
       const MaxComp &mxc = MaxComp())
      : sz(second - first), a(first, second), del(sz), mnh([this, mnc]() {
          std::vector<u32> b(this->size());
          for (u32 i = 0; i < this->size(); i++) b[i] = i;
          return std::priority_queue(b.begin(), b.end(), PosComp<MinComp>(a, mnc));
        }()),
        mxh([this, mxc]() {
          std::vector<u32> b(this->size());
          for (u32 i = 0; i < this->size(); i++) b[i] = i;
          return std::priority_queue(b.begin(), b.end(), PosComp<MaxComp>(a, mxc));
        }())
  {
  }

  T min() { return fresh_min(), a[mnh.top()]; }
  T max() { return fresh_max(), a[mxh.top()]; }

  constexpr u32 size() { return sz; }

  void pop_min()
  {
    fresh_min();
    del[mnh.top()] = true;
    mnh.pop();
    --sz;
  }

  void pop_max()
  {
    fresh_max();
    del[mxh.top()] = true;
    mxh.pop();
    --sz;
  }

  void push(const T &x)
  {
    a.emplace_back(x);
    del.emplace_back();
    mnh.push(a.size() - 1u);
    mxh.push(a.size() - 1u);
    ++sz;
  }
};
Traceback (most recent call last):
  File "/home/runner/.local/lib/python3.10/site-packages/onlinejudge_verify/documentation/build.py", line 71, in _render_source_code_stat
    bundled_code = language.bundle(stat.path, basedir=basedir, options={'include_paths': [basedir]}).decode()
  File "/home/runner/.local/lib/python3.10/site-packages/onlinejudge_verify/languages/cplusplus.py", line 187, in bundle
    bundler.update(path)
  File "/home/runner/.local/lib/python3.10/site-packages/onlinejudge_verify/languages/cplusplus_bundle.py", line 401, in update
    self.update(self._resolve(pathlib.Path(included), included_from=path))
  File "/home/runner/.local/lib/python3.10/site-packages/onlinejudge_verify/languages/cplusplus_bundle.py", line 312, in update
    raise BundleErrorAt(path, i + 1, "#pragma once found in a non-first line")
onlinejudge_verify.languages.cplusplus_bundle.BundleErrorAt: lib/internal.hpp: line 4: #pragma once found in a non-first line
Back to top page