I got this problem in a real interview, we can call it "number of islands III"
The problem "number of islands II" is shown below:
"A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land. Given a list of positions to operate, count the number of islands after each addLand operation. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water."
Here the follow up is:
We may have another operation called submerge, where we turn the land at position (row, col) into water.
Then, still, we have a sequence of operation consist of addLand and submerge, update the number of islands after each operation。
What I can generate at that time is simply perform dfs on 4 directions, I can't figure out an efficient algorithm applying union-find like I did in "number of islands II".
Does anybody have some good ideas?