#include #define EPS 1e-9 #define INF 1070000000LL #define MOD 1000000007LL #define fir first #define foreach(it,X) for(auto it=(X).begin();it!=(X).end();it++) #define numa(x,a) for(auto x: a) #define ite iterator #define mp make_pair #define rep(i,n) rep2(i,0,n) #define rep2(i,m,n) for(int i=m;i<(n);i++) #define pb push_back #define pf push_front #define sec second #define sz(x) ((int)(x).size()) #define ALL( c ) (c).begin(), (c).end() #define gcd(a,b) __gcd(a,b) #define mem(x,n) memset(x,n,sizeof(x)) #define endl "\n" using namespace std; template void deploy(std::ostream &os, const TUPLE &tuple){} template void deploy(std::ostream &os, const TUPLE &t){ os << (POS == 0 ? "" : ", ") << get(t); deploy(os, t); } template std::ostream& operator<<(std::ostream &os, std::pair &p){ os << "(" << p.first <<", " << p.second <<")";return os; } template std::ostream& operator<<(std::ostream &os, std::vector &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; } template std::ostream& operator<<(std::ostream &os, std::set &v){ int remain = v.size(); os << "{"; for(auto e: v) os << e << (--remain == 0 ? "}" : ", "); return os; } template std::ostream& operator<<(std::ostream &os, std::map &mp){ int remain = mp.size(); os << "{"; for(auto e: mp) os << "(" << e.first << " -> " << e.second << ")" << (--remain == 0 ? "}" : ", "); return os; } #define DEBUG1(var0) { std::cerr << (#var0) << "=" << (var0) << endl; } #define DEBUG2(var0, var1) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG1(var1); } #define DEBUG3(var0, var1, var2) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG2(var1,var2); } #define DEBUG4(var0, var1, var2, var3) { std::cerr << (#var0) << "=" << (var0) << ", ";DEBUG3(var1,var2,var3); } using ll = long long; int H,W; #define MAX_N 202 int board[MAX_N][MAX_N]; //Union-Find木 template class UFind{ //vector par; pair par[MAX_N][MAX_N]; set > adj[MAX_N][MAX_N]; //vector myrank; public: int color[MAX_N][MAX_N]; UFind(){ rep(i,NV){ rep(j,NV){ par[i][j] = mp(i,j); } } } inline pair find(pair x){//木の根を求める if (par[x.fir][x.sec] == x) { return x; }else{ return par[x.fir][x.sec] = find(par[x.fir][x.sec]); } } inline void unite(pair x, pair y) {//xとyの属する集合を併合 x = find(x); y = find(y); if (x == y) { return; } if (sz(adj[x.fir][x.sec]) > sz(adj[y.fir][y.sec])) { swap(x,y); } // y <- x par[x.fir][x.sec] = y; adj[y.fir][y.sec].insert(ALL(adj[x.fir][x.sec])); adj[x.fir][x.sec].clear(); } inline void add(pair x, pair y) { //x <- y x = find(x); y = find(y); if (x == y) { return; } adj[x.fir][x.sec].insert(y); } inline bool same(pair x, pair y) {//x,yが同じ集合に属するか否か return find(x) == find(y); } inline int getcolor(pair x){ x = find(x); return color[x.fir][x.sec]; } inline void update(pair root, int X) { vector >cur_adj(ALL(adj[root.fir][root.sec])); for (auto &a : cur_adj) { a = find(a); } color[root.fir][root.sec] = X; sort(ALL(cur_adj));cur_adj.erase(unique(ALL(cur_adj)), cur_adj.end()); root = find(root); adj[root.fir][root.sec].clear(); adj[root.fir][root.sec].insert(ALL(cur_adj)); for (auto &a : cur_adj) { if (getcolor(a) == X) { unite(a, root); } } root = find(root); color[root.fir][root.sec] = X; //DEBUG1(root); } inline void dbg(){ rep(i,H){ rep(j,W){ auto mypar = find(mp(i,j)); //auto col = getcolor(mp(i,j)); DEBUG4(i,j,mypar,adj[i][j]); } } } }; //Union-find木終わり UFind <> UF; inline bool OK(int x, int y) { return 0 <= x && x < H && 0 <= y && y < W; } inline void paint(int R, int C, int X){ auto root = UF.find(mp(R,C)); if (UF.getcolor(root) == X) { return; } UF.update(root,X); } int main() { cin.tie(0); ios_base::sync_with_stdio(0); cin >> H >> W; rep(i,H){ rep(j,W){ cin >> board[i][j]; UF.color[i][j] = board[i][j]; } } int dx[] = {-1,0,0,1}; int dy[] = {0,1,-1,0}; //int dx[] = {0,1}; //int dy[] = {1,0}; rep(i,H){ rep(j,W){ rep(k,4){ int nx = i + dx[k], ny = j + dy[k]; if (OK(nx, ny)) { if (board[nx][ny] == board[i][j]) { UF.unite(mp(i,j), mp(nx,ny)); }else{ UF.add(mp(i,j), mp(nx,ny)); } } } } } //UF.dbg(); int Q; cin >> Q; while(Q--){ int R,C,X; cin >> R >> C >> X; R--;C--; paint(R,C,X); //DEBUG3(R,C,X); /* rep(i,H){ rep(j,W){ if (j) cout << ' '; if (i == R && j == C) { cout << '*'; continue; } cout << board[i][j]; } cout << "\t"; */ if (Q == 0) { rep(i,H){ rep(j,W){ if (j) cout << ' '; char c = UF.getcolor(mp(i,j)) + '0'; cout << c; } cout << endl; } } } return 0; }