classUnionFind{public:UnionFind(intn):id(n),size(n,1){iota(begin(id),end(id),0);}voidunion_(intu,intv){constinti=find(u);constintj=find(v);if(i==j)return;id[i]=j;size[j]+=size[i];}intgetStableSize(){// Bricks connected with 0 (top) are stablereturnsize[find(0)];}private:vector<int>id;vector<int>size;intfind(intu){returnid[u]==u?u:id[u]=find(id[u]);}};classSolution{public:vector<int>hitBricks(vector<vector<int>>&grid,vector<vector<int>>&hits){m=grid.size();n=grid[0].size();UnionFinduf(m*n+1);// 0 := top (stable)// Mark cells to hit as 2for(constvector<int>&hit:hits){constinti=hit[0];constintj=hit[1];if(grid[i][j]==1)grid[i][j]=2;}// Union all 1sfor(inti=0;i<m;++i)for(intj=0;j<n;++j)if(grid[i][j]==1)unionNeighbors(grid,uf,i,j);vector<int>ans(hits.size());intstableSize=uf.getStableSize();for(inti=hits.size()-1;i>=0;--i){constintx=hits[i][0];constinty=hits[i][1];if(grid[x][y]==2){// Cells marked from 1 to 2grid[x][y]=1;// Unhit, restore back to 1unionNeighbors(grid,uf,x,y);constintnewStableSize=uf.getStableSize();if(newStableSize>stableSize)ans[i]=newStableSize-stableSize-1;// 1 := the hit cellstableSize=newStableSize;}}returnans;}private:constvector<int>dirs{0,1,0,-1,0};intm;intn;voidunionNeighbors(constvector<vector<int>>&grid,UnionFind&uf,inti,intj){constinthashed=hash(i,j);for(intk=0;k<4;++k){constintx=i+dirs[k];constinty=j+dirs[k+1];if(x<0||x==m||y<0||y==n)continue;if(grid[x][y]!=1)continue;uf.union_(hashed,hash(x,y));}if(i==0)uf.union_(hashed,0);}inthash(inti,intj){returni*n+j+1;}};
classUnionFind{publicUnionFind(intn){id=newint[n];size=newint[n];for(inti=0;i<n;++i)id[i]=i;Arrays.fill(size,1);}publicvoidunion(intu,intv){finalinti=find(u);finalintj=find(v);if(i==j)return;id[i]=j;size[j]+=size[i];}publicintgetStableSize(){// Bricks connected with 0 (top) are stablereturnsize[find(0)];}privateint[]id;privateint[]size;privateintfind(intu){returnid[u]==u?u:(id[u]=find(id[u]));}}classSolution{publicint[]hitBricks(int[][]grid,int[][]hits){this.m=grid.length;this.n=grid[0].length;UnionFinduf=newUnionFind(m*n+1);// 0 := top (stable)// Mark cells to hit as 2for(int[]hit:hits){finalinti=hit[0];finalintj=hit[1];if(grid[i][j]==1)grid[i][j]=2;}// Union all 1sfor(inti=0;i<m;++i)for(intj=0;j<n;++j)if(grid[i][j]==1)unionNeighbors(grid,uf,i,j);int[]ans=newint[hits.length];intstableSize=uf.getStableSize();for(inti=hits.length-1;i>=0;--i){finalintx=hits[i][0];finalinty=hits[i][1];if(grid[x][y]==2){// Cells marked from 1 to 2grid[x][y]=1;// Unhit, restore back to 1unionNeighbors(grid,uf,x,y);finalintnewStableSize=uf.getStableSize();if(newStableSize>stableSize)ans[i]=newStableSize-stableSize-1;// 1 := the hit cellstableSize=newStableSize;}}returnans;}privateintm;privateintn;privatestaticfinalint[]dirs={0,1,0,-1,0};privatevoidunionNeighbors(int[][]grid,UnionFinduf,inti,intj){finalinthashed=hash(i,j);for(intk=0;k<4;++k){finalintx=i+dirs[k];finalinty=j+dirs[k+1];if(x<0||x==m||y<0||y==n)continue;if(grid[x][y]!=1)continue;uf.union(hashed,hash(x,y));}if(i==0)uf.union(hashed,0);}privateinthash(inti,intj){returni*n+j+1;}}