#include using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x class UF { public: vector par,rank; UF() {rank=vector(um,0); for(int i=0;irank[y]) return par[x]=y; rank[x]+=rank[x]==rank[y]; return par[y]=x; } }; UF<500000> uf; int H,W,Q; int A[201*201]; vector S[201*201]; int id(int y,int x){ return y*W+x;} void solve() { int i,j,k,l,r,x,y; string s; cin>>H>>W; FOR(y,H) FOR(x,W) { cin>>A[id(y,x)]; } FOR(y,H) FOR(x,W) { if(y) { if(A[id(y,x)]==A[id(y-1,x)]) uf(id(y,x),id(y-1,x)); else S[id(y,x)].push_back(id(y-1,x)); } if(y>Q; while(Q--) { cin>>y>>x>>k; x=uf[id(y-1,x-1)]; if(A[x]==k) continue; vector NEW; FORR(r,S[x]) if(uf[x]!=uf[r]) { FORR(r2,S[uf[r]]) if(uf[r2]!=uf[x]) NEW.push_back(uf[r2]); uf(x,r); } sort(ALL(NEW)); NEW.erase(unique(ALL(NEW)),NEW.end()); S[uf[x]]=NEW; A[uf[x]]=k; } FOR(y,H) FOR(x,W) _P("%d%c",A[uf[id(y,x)]],(x==W-1)?'\n':' '); } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); solve(); return 0; }