#include #include #include #include #include #include #include #include #include using namespace std; #define FOR(x,y) for(int x = 0;x < y;x++) #define LLI long long int #define FORR(x,arr) for(auto& x:arr) #define ALL(a) (a.begin()),(a.end()) template class UF { public: vector _parent,_rank; UF() { _parent=_rank=vector(um,0); for(int i=0;i_rank[yRoot]) { _parent[xRoot] = yRoot; return yRoot; } if(_rank[xRoot]<_rank[yRoot]) { _parent[yRoot] = xRoot; return xRoot; } if(xRoot != yRoot) { _parent[yRoot] = xRoot; _rank[xRoot]++; return xRoot; } return xRoot; } }; int H,W,Q; int Board[201*201]; vector S[201*201]; int GetIndex(int y,int x) { return y*W+x; } UF<5000000> uf; int main() { cin >> H >> W; FOR(y, H) { FOR(x, W) { cin >> Board[GetIndex(y, x)]; } } FOR(y,H) FOR(x,W) { if(y) { if(Board[GetIndex(y,x)]==Board[GetIndex(y-1,x)]) uf.Union(GetIndex(y,x),GetIndex(y-1,x)); else S[GetIndex(y,x)].push_back(GetIndex(y-1,x)); } if(y>Q; FOR(i,Q) { int y, x, k; cin>>y>>x>>k; int parent =uf.Find(GetIndex(y-1,x-1)); if(Board[parent]==k) continue; vector NEW; FORR(r,S[parent]) { if(uf.Find(parent)!=uf.Find(r)) { FORR(r2,S[uf.Find(r)]) if(uf.Find(r2)!=uf.Find(parent)) NEW.push_back(uf.Find(r2)); uf.Union(parent,r); } } sort(ALL(NEW)); NEW.erase(unique(ALL(NEW)),NEW.end()); S[uf.Find(parent)]=NEW; Board[uf.Find(parent)]=k; } FOR(y,H) FOR(x,W) printf("%d%c",Board[uf.Find(GetIndex(y,x))],(x==W-1)?'\n':' '); return 0; }