#pragma once #include <cassert> #include <utility> #include "Mylib/Graph/Template/graph.cpp" namespace haar_lib { template <typename T> class range_edge_graph : public graph<T> { using super = graph<T>; int k_, N_; int calc_size(int N) const { int ret = 1; while (ret < N) ret *= 2; return ret; } public: range_edge_graph() {} range_edge_graph(int N) : graph<T>(5 * (k_ = (calc_size(N)))), N_(N) { for (int i = 2; i < 2 * k_; ++i) { super::add_edge(k_ + (i >> 1), k_ + i, 0); super::add_edge(3 * k_ + i, 3 * k_ + (i >> 1), 0); } for (int i = 0; i < N_; ++i) { super::add_edge(2 * k_ + i, i, 0); super::add_edge(i, 4 * k_ + i, 0); } } void add_edge(int i, int j, T w) { super::add_edge(i, j, w); } void add_edge(int i, std::pair<int, int> p, T w) { auto [l, r] = p; for (l += k_, r += k_; l < r; l >>= 1, r >>= 1) { if (l & 1) super::add_edge(i, l + k_, w), ++l; if (r & 1) --r, super::add_edge(i, r + k_, w); } } void add_edge(std::pair<int, int> p, int i, T w) { auto [l, r] = p; for (l += k_, r += k_; l < r; l >>= 1, r >>= 1) { if (l & 1) super::add_edge(l + 3 * k_, i, w), ++l; if (r & 1) --r, super::add_edge(r + 3 * k_, i, w); } } void add_edge(std::pair<int, int> p, std::pair<int, int> q, T w) { int x = super::size(); super::ds.emplace_back(); int y = super::size(); super::ds.emplace_back(); add_edge(p, x, 0); add_edge(x, y, w); add_edge(y, q, 0); } }; } // namespace haar_lib
#line 2 "Mylib/Graph/Template/range_edge_graph.cpp" #include <cassert> #include <utility> #line 2 "Mylib/Graph/Template/graph.cpp" #include <iostream> #include <vector> namespace haar_lib { template <typename T> struct edge { int from, to; T cost; int index = -1; edge() {} edge(int from, int to, T cost) : from(from), to(to), cost(cost) {} edge(int from, int to, T cost, int index) : from(from), to(to), cost(cost), index(index) {} }; template <typename T> struct graph { using weight_type = T; using edge_type = edge<T>; std::vector<std::vector<edge<T>>> data; auto& operator[](size_t i) { return data[i]; } const auto& operator[](size_t i) const { return data[i]; } auto begin() const { return data.begin(); } auto end() const { return data.end(); } graph() {} graph(int N) : data(N) {} bool empty() const { return data.empty(); } int size() const { return data.size(); } void add_edge(int i, int j, T w, int index = -1) { data[i].emplace_back(i, j, w, index); } void add_undirected(int i, int j, T w, int index = -1) { add_edge(i, j, w, index); add_edge(j, i, w, index); } template <size_t I, bool DIRECTED = true, bool WEIGHTED = true> void read(int M) { for (int i = 0; i < M; ++i) { int u, v; std::cin >> u >> v; u -= I; v -= I; T w = 1; if (WEIGHTED) std::cin >> w; if (DIRECTED) add_edge(u, v, w, i); else add_undirected(u, v, w, i); } } }; template <typename T> using tree = graph<T>; } // namespace haar_lib #line 5 "Mylib/Graph/Template/range_edge_graph.cpp" namespace haar_lib { template <typename T> class range_edge_graph : public graph<T> { using super = graph<T>; int k_, N_; int calc_size(int N) const { int ret = 1; while (ret < N) ret *= 2; return ret; } public: range_edge_graph() {} range_edge_graph(int N) : graph<T>(5 * (k_ = (calc_size(N)))), N_(N) { for (int i = 2; i < 2 * k_; ++i) { super::add_edge(k_ + (i >> 1), k_ + i, 0); super::add_edge(3 * k_ + i, 3 * k_ + (i >> 1), 0); } for (int i = 0; i < N_; ++i) { super::add_edge(2 * k_ + i, i, 0); super::add_edge(i, 4 * k_ + i, 0); } } void add_edge(int i, int j, T w) { super::add_edge(i, j, w); } void add_edge(int i, std::pair<int, int> p, T w) { auto [l, r] = p; for (l += k_, r += k_; l < r; l >>= 1, r >>= 1) { if (l & 1) super::add_edge(i, l + k_, w), ++l; if (r & 1) --r, super::add_edge(i, r + k_, w); } } void add_edge(std::pair<int, int> p, int i, T w) { auto [l, r] = p; for (l += k_, r += k_; l < r; l >>= 1, r >>= 1) { if (l & 1) super::add_edge(l + 3 * k_, i, w), ++l; if (r & 1) --r, super::add_edge(r + 3 * k_, i, w); } } void add_edge(std::pair<int, int> p, std::pair<int, int> q, T w) { int x = super::size(); super::ds.emplace_back(); int y = super::size(); super::ds.emplace_back(); add_edge(p, x, 0); add_edge(x, y, w); add_edge(y, q, 0); } }; } // namespace haar_lib