#include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } bool topological_sort(vector> g, vector &order){ int n = g.size(); order.clear(); vector used(n, false); function dfs = [&](int v){ used[v] = true; for(int to : g[v]){ if(!used[to]) dfs(to); } order.push_back(v); }; for(int v = 0; v < n; v++){ if(!used[v]) dfs(v); } reverse(order.begin(), order.end()); vector inv_order(n); for(int i = 0; i < n; i++) inv_order[order[i]] = i; for(int v = 0; v < n; v++){ for(int u : g[v]){ if(inv_order[v] > inv_order[u]) return false; } } return true; } using P = pair; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; int h, w; cin >> h >> w; vector r(h), c(w); for(int i = 0; i < h; i++) { cin >> r[i]; } for(int i = 0; i < w; i++) { cin >> c[i]; } auto to_index = [&](int i, int j){ return i*w+j; }; vector> g(h*w); for(int i = 1; i < h; i++) g[to_index(i, 0)].push_back(to_index(0, 0)); for(int j = 1; j < w; j++) g[to_index(0, j)].push_back(to_index(0, 0)); for(int i = 1; i < h; i++){ int x = w-r[i]+1; if(x-1 != 0){ g[to_index(i, x-1)].push_back(to_index(i, 0)); } if(x != w) g[to_index(i, 0)].push_back(to_index(i, x)); for(int j = x; j+1 < w; j++){ g[to_index(i, j)].push_back(to_index(i, j+1)); } } for(int j = 1; j < w; j++){ int x = h-c[j]+1; if(x-1 != 0){ g[to_index(x-1, j)].push_back(to_index(0, j)); } if(x != h) g[to_index(0, j)].push_back(to_index(x, j)); for(int i = x; i+1 < h; i++){ g[to_index(i, j)].push_back(to_index(i+1, j)); } } vector ord; assert(topological_sort(g, ord)); vector inv_ord(h*w); for(int i = 0; i < h*w; i++){ inv_ord[ord[i]] = i; } auto ans = vec2d(h, w, 0); for(int i = 0; i < h; i++){ for(int j = 0; j < w; j++){ int idx = to_index(i, j); ans[i][j] = inv_ord[idx]+1; } } for(int i = 0; i < h; i++) print_vector(ans[i]); }