#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include//assert(); //#include //xAOJ ///////// #define REP(i, x, n) for(int i = x; i < n; i++) #define rep(i,n) REP(i,0,n) #define P(p) cout<<(p)< ///////// typedef long long LL; typedef long double LD; typedef unsigned long long ULL; ///////// using namespace::std; ///////// ///////// class UnionFind{ public: int cNum;//要素数 int cSize;// vector parent; vector rank; vector color; vector< vector< int > > tonari; UnionFind(int n){ cNum = n; cSize = n; parent = vector(n); rank = vector(n,0); color = vector(n); tonari = vector< vector >(n); for(int i=0;i temp; vector::iterator it,endit; it = tonari[x].begin(); endit = tonari[x].end(); for(;it != endit;++it){ if( !same(y,*it) ){ temp.push_back(*it); } } tonari[x].clear(); it = tonari[y].begin(); endit = tonari[y].end(); for(;it != endit;++it){ if( !same(y,*it) ){ temp.push_back(*it); } } sort( temp.begin(), temp.end() ); temp.erase( unique( temp.begin(), temp.end() ), temp.end() ); tonari[y].swap( temp ); } void addVec(int x, vector& Vec){ vector::iterator it,endit; vector::iterator it2,endit2; vector temp; int ter; it = Vec.begin(); endit = Vec.end(); for(;it != endit;++it){ ter = find(*it); it2 = tonari[ ter ].begin(); endit2 = tonari[ ter ].end(); temp.insert( temp.end(), it2, endit2 ); tonari[ ter ].clear(); } it = Vec.begin(); endit = Vec.end(); for(;it != endit;++it){ parent[ find(*it) ] = x; } vector temp2; it = temp.begin(); endit = temp.end(); for(;it != endit; ++it){ if( !same(x,*it) ){ temp2.push_back( *it ); } } temp.swap( temp2 ); sort( temp.begin(), temp.end() ); temp.erase( unique( temp.begin(), temp.end() ), temp.end() ); tonari[x].swap( temp ); } void tonariAdd(int atti,int kotti){ tonari[ find(atti) ].push_back(kotti); tonari[ find(kotti) ].push_back(atti); } }; void solve(){ int H,W; cin >> H >> W; UnionFind UF(H*W); int incolor; for(int h=0;h> incolor; if( incolor == 1 ){ UF.color[h*W + w] = true; }else{ UF.color[h*W + w] = false; } } } //////////////// if( 1 0 ){ if( UF.color[h*W+w] == UF.color[(h-1)*W + w] ){ UF.add(h*W+w,(h-1)*W+w ); }else{ UF.tonariAdd(h*W+w,(h-1)*W+w); } } if( w> 0 ){ if( UF.color[h*W+w] == UF.color[h*W+(w-1)] ){ UF.add( h*W+w,h*W+(w-1) ); }else{ UF.tonariAdd(h*W+w,h*W+(w-1) ); } } } } //////////////// int Q; cin >> Q; int R,C,Xnum; bool X; int ter; vector tempVec; tempVec.reserve(H*W); while(Q--){ cin >> R >> C >> Xnum; --R;--C; if( Xnum == 1 ){ X = true; }else{ X = false; } ter = UF.find(R*W+C); if( UF.color[ ter ] == X ) continue; UF.color[ter] = X; if( UF.cSize == 1 ){ continue;} tempVec.clear(); tempVec.swap( UF.tonari[ ter ] ); UF.addVec(ter, tempVec); } for(int h=0;h