#include #pragma GCC diagnostic ignored "-Wsign-compare" #pragma GCC diagnostic ignored "-Wsign-conversion" #define NDEBUG using i32 = int32_t; using i64 = int64_t; using u32 = uint32_t; using u64 = uint64_t; using uint = unsigned int; using usize = std::size_t; using ll = long long; using ull = unsigned long long; using ld = long double; template constexpr T popcount(const T u) { return u ? static_cast(__builtin_popcountll(static_cast(u))) : static_cast(0); } template constexpr T log2p1(const T u) { return u ? static_cast(64 - __builtin_clzll(static_cast(u))) : static_cast(0); } template constexpr T msbp1(const T u) { return log2p1(u); } template constexpr T lsbp1(const T u) { return __builtin_ffsll(u); } template constexpr T clog(const T u) { return u ? log2p1(u - 1) : static_cast(u); } template constexpr bool ispow2(const T u) { return u and (static_cast(u) & static_cast(u - 1)) == 0; } template constexpr T ceil2(const T u) { return static_cast(1) << clog(u); } template constexpr T floor2(const T u) { return u == 0 ? static_cast(0) : static_cast(1) << (log2p1(u) - 1); } template constexpr bool btest(const T mask, const usize ind) { return ((static_cast(mask) >> ind) & static_cast(1)); } template constexpr T bcut(const T mask, const usize ind) { return ind == 0 ? static_cast(0) : static_cast((static_cast(mask) << (64 - ind)) >> (64 - ind)); } template bool chmin(T& a, const T& b) { return (a > b ? a = b, true : false); } template bool chmax(T& a, const T& b) { return (a < b ? a = b, true : false); } constexpr unsigned int mod = 1000000007; template constexpr T inf_v = std::numeric_limits::max() / 4; template constexpr Real pi_v = Real{3.141592653589793238462643383279502884}; template T read() { T v; return std::cin >> v, v; } template std::vector read_vec(const std::size_t size) { std::vector v(size); for (auto& e : v) { std::cin >> e; } return v; } template auto read_vals() { return std::tuple...>{read()...}; } #define SHOW(...) static_cast(0) template std::vector make_v(const std::size_t size, T v) { return std::vector(size, v); } template auto make_v(const std::size_t size, Args... args) { return std::vector(size, make_v(args...)); } template class base_graph { public: using cost_type = Cost; base_graph(const usize sz) : sz{sz}, edges(sz), rev_edges(sz) {} void add_edge(const usize from, const usize to, const Cost cost, const bool bi = false) { assert(from < sz), assert(to < sz); edges[from].push_back(edge{from, to, cost}), rev_edges[to].push_back(edge(to, from, cost)); if (bi) { add_edge(to, from, cost, false); } } struct edge { edge(const usize from, const usize to, const Cost cost) : from{from}, to{to}, cost{cost} {} usize from, to; Cost cost; bool operator<(const edge& e) const { return cost != e.cost ? cost < e.cost : to < e.to; } }; std::vector& operator[](const usize i) { return assert(i < sz), edges[i]; } const std::vector& operator[](const usize i) const { return assert(i < sz), edges[i]; } const std::vector>& rev_edge() const { return rev_edges; } std::vector>& rev_edge() { return rev_edges; } friend std::ostream& operator<<(std::ostream& os, const base_graph& g) { os << "[\n"; for (usize i = 0; i < g.sz; i++) { for (const auto& e : g.edges[i]) { os << i << "->" << e.to << ":" << e.cost << "\n"; } } return (os << "]\n"); } static usize to(const edge& e) { return e.to; } usize size() const { return sz; } private: const usize sz; std::vector> edges, rev_edges; }; template<> class base_graph { public: base_graph(const usize sz) : sz{sz}, edges(sz), rev_edges(sz) {} void add_edge(const usize from, const usize to, const bool bi = false) { assert(from < sz), assert(to < sz); edges[from].push_back(to), rev_edges[to].push_back(from); if (bi) { add_edge(to, from, false); } } std::vector& operator[](const usize i) { return assert(i < sz), edges[i]; } const std::vector& operator[](const usize i) const { return assert(i < sz), edges[i]; } const std::vector>& rev_edge() const { return rev_edges; } std::vector>& rev_edge() { return rev_edges; } friend std::ostream& operator<<(std::ostream& os, const base_graph& g) { os << "[\n"; for (usize i = 0; i < g.sz; i++) { for (const usize to : g.edges[i]) { os << i << "->" << to << "\n"; } } return (os << "]\n"); } static usize to(const usize e) { return e; } usize size() const { return sz; } private: const usize sz; std::vector> edges, rev_edges; }; using graph = base_graph; using tree = graph; template using cost_graph = base_graph; template using cost_tree = cost_graph; template std::pair> top_sort(const base_graph& g) { const usize v = g.size(); std::vector srt, used(v, 0); auto dfs = [&](auto&& self, const usize s) -> bool { if (used[s] == 1) { return false; } else if (used[s] == 0) { used[s] = 1; for (const auto& e : g[s]) { const usize to = base_graph::to(e); if (not self(self, to)) { return false; } } used[s] = 2, srt.push_back(s); } return true; }; for (usize i = 0; i < v; i++) { if (not dfs(dfs, i)) { return {false, srt}; } } std::reverse(srt.begin(), srt.end()); return {true, srt}; } int main() { const auto [h, w] = read_vals(); auto a = read_vec(h), b = read_vec(w); std::sort(a.begin(), a.end()), std::sort(b.begin(), b.end()); graph g(h * w); for (usize i = 0; i < h; i++) { for (usize j = 0; j + 1 < w; j++) { const usize s = i * w + j, t = i * w + j + 1; if (j + 1 < a[i]) { g.add_edge(s, t); } else { g.add_edge(t, s); } } } for (usize j = 0; j < w; j++) { for (usize i = 0; i + 1 < h; i++) { const usize s = i * w + j, t = (i + 1) * w + j; if (i + 1 < b[j]) { g.add_edge(s, t); } else { g.add_edge(t, s); } } } const auto res = top_sort(g); assert(res.first); const auto ord = res.second; auto ans = make_v(h, w, 0UL); for (usize i = 0; i < h * w; i++) { const usize p = ord[i]; ans[p / w][p % w] = i + 1; } for (usize i = 0; i < h; i++) { for (usize j = 0; j < w; j++) { std::cout << ans[i][j] << " "; } std::cout << std::endl; } return 0; }