#include #include #include using namespace std; typedef pair ip; #define Y first #define X second struct Query{ int r; int c; int x; }; class Island; int h, w; int **a; Island*** islands; class Island{ private: void init(int y, int x){ if(y<0 || x < 0 || y >= h || x >= w) return; for(int i=0; i < pos.size(); i++){ if(pos[i].X == x && pos[i].Y == y){ return; } } if(a[y][x] != color) { if(islands[y][x] != NULL){ setJoinedIsland(islands[y][x]); } return; } pos.push_back(ip(y,x)); islands[y][x] = this; init(y-1,x); init(y,x-1); init(y+1,x); init(y,x+1); } public: Island* parent; vector childs; vector joined_islands; vector pos; int color; int id; Island(int y,int x,int id){ this->id = id; parent = NULL; color = a[y][x]; init(y,x); } void setJoinedIsland(Island* island){ for(int i=0; isetJoinedIsland(this); } void setParent(Island *p){ parent = p; color = p->color; for(int i=0;isetParent(p); } } void reverseColor(){ color = (color+1)%2; for(int i=0;isetParent(this); childs.push_back(joined_islands[i]); } joined_islands.clear(); } }; int main(){ cin >> h >> w; a = new int*[h]; islands = new Island**[h]; for(int i=0;i> a[i][ii]; islands[i][ii] = NULL; } } int q; cin >> q; Query* qa = new Query[q]; for(int i=0; i> qa[i].r >> qa[i].c >> qa[i].x; qa[i].r -= 1; qa[i].c -= 1; } int c=0; for(int i=0;ireverseColor(); } for(int i=0;icolor << " "; /*for(int iii=0;iiijoined_islands.size();iii++){ cout << islands[i][ii]->joined_islands[iii]->id << ", "; } cout << endl;*/ } cout << endl; } }