#include // c #include // io #include #include #include #include // container #include #include #include #include #include #include // other #include #include #include #include #include using namespace std; typedef long long ll;typedef unsigned long long ull;typedef long double ld; #define ALL(c) c.begin(),c.end() #define IN(l,v,r) (l<=v && v < r) template void UNIQUE(T v){v.erase(unique(ALL(v)),v.end());} //debug #define DUMP(x) cerr << #x <<" = " << (x) #define LINE() cerr<< " (L" << __LINE__ << ")" struct range{ struct Iter{ int v,step; Iter& operator++(){v+=step;return *this;} bool operator!=(Iter& itr){return vitr.v;} int& operator*(){return v;} }; Iter i, n; rrange(int i, int n,int step):i({i-1,step}), n({n-1,step}){} rrange(int i, int n):rrange(i,n,1){} rrange(int n) :rrange(0,n){} Iter& begin(){return n;} Iter& end(){return i;} }; //input template istream& operator >> (istream& is,pair& p){return is>>p.first>>p.second;} template istream& operator >> (istream& is,tuple& t){return is >> get<0>(t);} template istream& operator >> (istream& is,tuple& t){return is >> get<0>(t) >> get<1>(t);} template istream& operator >> (istream& is,tuple& t){return is >>get<0>(t)>>get<1>(t)>>get<2>(t);} template istream& operator >> (istream& is,tuple& t){return is >> get<0>(t)>>get<1>(t)>>get<2>(t)>>get<3>(t);} template istream& operator >> (istream& is,vector& as){for(int i:range(as.size()))is >>as[i];return is;} //output template ostream& operator << (ostream& os, const set& ss){for(auto a:ss){if(a!=ss.begin())os<<" "; os< ostream& operator << (ostream& os, const pair& p){return os< ostream& operator << (ostream& os, const map& m){bool isF=true;for(auto& p:m){if(!isF)os< ostream& operator << (ostream& os, const tuple& t){return os << get<0>(t);} template ostream& operator << (ostream& os, const tuple& t){return os << get<0>(t)<<" "<(t);} template ostream& operator << (ostream& os, const tuple& t){return os << get<0>(t)<<" "<(t)<<" "<(t);} template ostream& operator << (ostream& os, const tuple& t){return os << get<0>(t)<<" "<(t)<<" "<(t)<<" "<(t);} template ostream& operator << (ostream& os, const vector& as){for(int i:range(as.size())){if(i!=0)os<<" "; os< ostream& operator << (ostream& os, const vector>& as){for(int i:range(as.size())){if(i!=0)os< inline T INF(){assert(false);}; template<> inline int INF(){return 1<<28;}; template<> inline ll INF(){return 1LL<<58;}; template<> inline double INF(){return 1e16;}; template<> inline long double INF(){return 1e16;}; template inline T EPS(){assert(false);}; template<> inline int EPS(){return 1;}; template<> inline ll EPS(){return 1LL;}; template<> inline double EPS(){return 1e-8;}; template<> inline long double EPS(){return 1e-8;}; // min{2^r | n < 2^r} template T upper_pow2(T n){ T res=1;while(res T msb(T n){ int d=62;while((1LL<n)d--;return d;} template T pmod(T v,U M){return (v%M+M)%M;} ll gcd_positive(ll a,ll b) { return b == 0 ? a : gcd_positive(b,a%b); } ll gcd(ll a,ll b) { return gcd_positive(abs(a), abs(b)); } ll lcm(ll a,ll b){return a/gcd(a,b)*b;} template struct Edge{ int from,to;Cost cost; Edge(int from,int to,Cost cost):from(from),to(to),cost(cost){}; bool operator<(Edge r) const{ return cost(Edge r) const{ return cost>r.cost;} }; struct UnionFind{ vector> es; vector > f_cache, t_cache; vector col; vector par,rank,ss; int size; UnionFind(int n):size(n){ par=vector(n);iota(ALL(par),0); rank = vector(n);ss=vector(n,1); col = vector(n); f_cache = vector >(n); t_cache = vector >(n); } void add_edge(int f,int t, int x){ int ei = es.size(); es.push_back({f,t,x}); f_cache[f].push_back(ei); t_cache[t].push_back(ei); } int root(int x){ return par[x] = par[x] == x ? x: root(par[x]);} bool same(int x,int y){ return root(x) == root(y);} void unite(int x,int y){ x = root(x);y = root(y); if(x==y)return; if(f_cache[x].size() + t_cache[x].size() > f_cache[y].size() + t_cache[y].size()) swap(x,y); // if(rank[x]>rank[y])swap(x,y); for(int ei:f_cache[x]) f_cache[y].push_back(ei); for(int ei:t_cache[x]) t_cache[y].push_back(ei); par[x] = y;ss[y]+=ss[x]; if(rank[x] == rank[y]) rank[x]++; size--; } int getS(int x){ return ss[root(x)];} }; int dy[4] = {0,1,0,-1},dx[4]={1,0,-1,0}; class Main{ public: int H,W; int enc(int y,int x){return y * W + x;} void run(){ cin >> H >> W; vector> board(H,vector(W));cin >> board; UnionFind uf(H*W); for(int y:range(H))for(int x:range(W)) uf.col[enc(y,x)]=board[y][x]; for(int y:range(H))for(int x:range(W)){ for(int d:range(4)){ int ny = y + dy[d],nx = x + dx[d]; if(!IN(0,ny,H) || !IN(0,nx,W))continue; uf.add_edge(enc(y,x),enc(ny,nx),1); uf.add_edge(enc(ny,nx),enc(y,x),1); if(board[y][x]==board[ny][nx]) uf.unite(enc(y,x),enc(ny,nx)); } } int Q;cin >> Q; for(int q:range(Q)){ int r,c,x;cin >> r >> c >> x;r--;c--; int gi = uf.root(enc(r,c)); if(uf.col[gi]!=x){ for(int i:range(uf.f_cache[gi].size()))uf.unite(gi, uf.root(uf.es[uf.f_cache[gi][i]].to)); uf.col[uf.root(gi)] = x; } } // for(int y:range(H)){ // for(int x:range(W)){ // if(x!=0) cout <<" "; // cout << uf.root(enc(y,x)); // } // cout << endl; // } for(int y:range(H)){ for(int x:range(W)){ if(x!=0) cout <<" "; cout << uf.col[uf.root(enc(y,x))]; } cout << endl; } } }; int main(){ cout <