cp-library

C++ Library for Competitive Programming

View the Project on GitHub emthrm/cp-library

:question: 区間に辺を張るテク
(include/emthrm/graph/noshi_graph.hpp)

時間計算量

$O(\log{\lvert V \rvert})$

仕様

template <typename CostType>
struct NoshiGraph;

メンバ変数

名前 説明
std::vector<std::vector<Edge<CostType>>> graph 有向グラフ

メンバ関数

名前 効果・戻り値
explicit NoshiGraph(const int n); 頂点数 $n$ のオブジェクトを構築する。
void add_edge(const int src, const int dst, const CostType cost = 0); コスト $\mathrm{cost}$ の辺 $(\mathrm{src}, \mathrm{dst})$ を加える。
add_edge(src_l, src_r, dst_l, dst_r, cost); コスト $\mathrm{cost}$ の辺 $(s, t)$ ($s \in \lbrace \mathrm{srcL}, \ldots, \mathrm{srcR} - 1 \rbrace,\ t \in \lbrace \mathrm{dstL}, \ldots, \mathrm{dstR} - 1 \rbrace$) を加える。

参考文献

Submissons

https://codeforces.com/contest/786/submission/100222769

Depends on

Verified with

Code

#ifndef EMTHRM_GRAPH_NOSHI_GRAPH_HPP_
#define EMTHRM_GRAPH_NOSHI_GRAPH_HPP_

#include <bit>
#include <vector>

#include "emthrm/graph/edge.hpp"

namespace emthrm {

template <typename CostType>
struct NoshiGraph {
  std::vector<std::vector<Edge<CostType>>> graph;

  explicit NoshiGraph(const int n)
      : p2(std::bit_ceil(static_cast<unsigned int>(n))) {
    const int dbl = p2 << 1, hlf = p2 >> 1;
    graph.resize(dbl + p2);
    for (int i = 1; i < hlf; ++i) {
      add_edge(i + p2, (i << 1) + p2);
      add_edge(i + p2, (i << 1) + 1 + p2);
      add_edge((i << 1) + dbl, i + dbl);
      add_edge((i << 1) + 1 + dbl, i + dbl);
    }
    for (int src = p2 + hlf, dst = 0; dst < p2; ++src, dst += 2) {
      add_edge(src, dst);
      add_edge(src, dst + 1);
    }
    for (int src = 0, dst = dbl + hlf; src < p2; src += 2, ++dst) {
      add_edge(src, dst);
      add_edge(src + 1, dst);
    }
  }

  void add_edge(const int src, const int dst, const CostType cost = 0) {
    graph[src].emplace_back(src, dst, cost);
  }

  void add_edge(int src_l, int src_r, int dst_l, int dst_r,
                const CostType cost) {
    if (src_r <= src_l || dst_r <= dst_l) [[unlikely]] return;
    const int src_id = graph.size(), dst_id = src_id + 1;
    graph.emplace_back();
    graph.emplace_back();
    if ((dst_l += p2) & 1) add_edge(dst_id, dst_l++ - p2);
    if ((dst_r += p2) & 1) add_edge(dst_id, --dst_r - p2);
    for (dst_l >>= 1, dst_r >>= 1; dst_l < dst_r; dst_l >>= 1, dst_r >>= 1) {
      if (dst_l & 1) add_edge(dst_id, dst_l++ + p2);
      if (dst_r & 1) add_edge(dst_id, --dst_r + p2);
    }
    add_edge(src_id, dst_id, cost);
    if ((src_l += p2) & 1) add_edge(src_l++ - p2, src_id);
    if ((src_r += p2) & 1) add_edge(--src_r - p2, src_id);
    for (src_l >>= 1, src_r >>= 1; src_l < src_r; src_l >>= 1, src_r >>= 1) {
      if (src_l & 1) add_edge(src_l++ + (p2 << 1), src_id);
      if (src_r & 1) add_edge(--src_r + (p2 << 1), src_id);
    }
  }

 private:
  const int p2;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_NOSHI_GRAPH_HPP_
#line 1 "include/emthrm/graph/noshi_graph.hpp"



#include <bit>
#include <vector>

#line 1 "include/emthrm/graph/edge.hpp"
/**
 * @title 辺
 */

#ifndef EMTHRM_GRAPH_EDGE_HPP_
#define EMTHRM_GRAPH_EDGE_HPP_

#include <compare>

namespace emthrm {

template <typename CostType>
struct Edge {
  CostType cost;
  int src, dst;

  explicit Edge(const int src, const int dst, const CostType cost = 0)
      : cost(cost), src(src), dst(dst) {}

  auto operator<=>(const Edge& x) const = default;
};

}  // namespace emthrm

#endif  // EMTHRM_GRAPH_EDGE_HPP_
#line 8 "include/emthrm/graph/noshi_graph.hpp"

namespace emthrm {

template <typename CostType>
struct NoshiGraph {
  std::vector<std::vector<Edge<CostType>>> graph;

  explicit NoshiGraph(const int n)
      : p2(std::bit_ceil(static_cast<unsigned int>(n))) {
    const int dbl = p2 << 1, hlf = p2 >> 1;
    graph.resize(dbl + p2);
    for (int i = 1; i < hlf; ++i) {
      add_edge(i + p2, (i << 1) + p2);
      add_edge(i + p2, (i << 1) + 1 + p2);
      add_edge((i << 1) + dbl, i + dbl);
      add_edge((i << 1) + 1 + dbl, i + dbl);
    }
    for (int src = p2 + hlf, dst = 0; dst < p2; ++src, dst += 2) {
      add_edge(src, dst);
      add_edge(src, dst + 1);
    }
    for (int src = 0, dst = dbl + hlf; src < p2; src += 2, ++dst) {
      add_edge(src, dst);
      add_edge(src + 1, dst);
    }
  }

  void add_edge(const int src, const int dst, const CostType cost = 0) {
    graph[src].emplace_back(src, dst, cost);
  }

  void add_edge(int src_l, int src_r, int dst_l, int dst_r,
                const CostType cost) {
    if (src_r <= src_l || dst_r <= dst_l) [[unlikely]] return;
    const int src_id = graph.size(), dst_id = src_id + 1;
    graph.emplace_back();
    graph.emplace_back();
    if ((dst_l += p2) & 1) add_edge(dst_id, dst_l++ - p2);
    if ((dst_r += p2) & 1) add_edge(dst_id, --dst_r - p2);
    for (dst_l >>= 1, dst_r >>= 1; dst_l < dst_r; dst_l >>= 1, dst_r >>= 1) {
      if (dst_l & 1) add_edge(dst_id, dst_l++ + p2);
      if (dst_r & 1) add_edge(dst_id, --dst_r + p2);
    }
    add_edge(src_id, dst_id, cost);
    if ((src_l += p2) & 1) add_edge(src_l++ - p2, src_id);
    if ((src_r += p2) & 1) add_edge(--src_r - p2, src_id);
    for (src_l >>= 1, src_r >>= 1; src_l < src_r; src_l >>= 1, src_r >>= 1) {
      if (src_l & 1) add_edge(src_l++ + (p2 << 1), src_id);
      if (src_r & 1) add_edge(--src_r + (p2 << 1), src_id);
    }
  }

 private:
  const int p2;
};

}  // namespace emthrm
Back to top page