#include #include #include template struct Edge { int src, dst; Cost cost; Edge(int src = -1, int dst = -1, Cost cost = 1) : src(src), dst(dst), cost(cost){}; bool operator<(const Edge& e) const { return this->cost < e.cost; } bool operator>(const Edge& e) const { return this->cost > e.cost; } }; template struct Graph { std::vector>> graph; Graph(int n = 0) : graph(n) {} void span(bool direct, int src, int dst, Cost cost = 1) { graph[src].emplace_back(src, dst, cost); if (!direct) graph[dst].emplace_back(dst, src, cost); } int size() const { return graph.size(); } void clear() { graph.clear(); } void resize(int n) { graph.resize(n); } std::vector>& operator[](int v) { return graph[v]; } std::vector> operator[](int v) const { return graph[v]; } }; template struct TopologicalSort { Graph graph; std::vector visited; std::vector order, id; explicit TopologicalSort(const Graph& graph) : graph(graph), visited(graph.size(), false), id(graph.size()) { for (int v = 0; v < (int)graph.size(); ++v) dfs(v); std::reverse(order.begin(), order.end()); for (int i = 0; i < (int)graph.size(); ++i) id[order[i]] = i; } void dfs(int v) { if (visited[v]) return; visited[v] = true; for (const auto& e : graph[v]) dfs(e.dst); order.push_back(v); } }; void solve() { int h, w; std::cin >> h >> w; Graph<> graph(h * w); auto enc = [&](int x, int y) { return x * w + y; }; std::vector ps(h); for (auto& p : ps) std::cin >> p; std::sort(ps.begin(), ps.end()); for (int x = 0; x < h; ++x) { int p = ps[x]; for (int y = 0; y + 1 < p; ++y) { graph.span(true, enc(x, y), enc(x, y + 1)); } if (p < w) graph.span(true, enc(x, p), enc(x, 0)); for (int y = p; y + 1 < w; ++y) { graph.span(true, enc(x, y + 1), enc(x, y)); } } std::vector qs(w); for (auto& q : qs) std::cin >> q; std::sort(qs.begin(), qs.end()); for (int y = 0; y < w; ++y) { int q = qs[y]; for (int x = 0; x + 1 < q; ++x) { graph.span(true, enc(x, y), enc(x + 1, y)); } if (q < h) graph.span(true, enc(q, y), enc(0, y)); for (int x = q; x + 1 < h; ++x) { graph.span(true, enc(x + 1, y), enc(x, y)); } } TopologicalSort ts(graph); for (int x = 0; x < h; ++x) { for (int y = 0; y < w; ++y) { std::cout << ts.id[enc(x, y)] + 1 << " "; } std::cout << "\n"; } } int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); solve(); return 0; }