Antichamber
Time limit5sMemory limit1024 MB
Maintain a black-cell grid under repaints that delete split-off pieces and fill holes, answering component size and same-component queries online.
- Level
Hard10 of 10
- Topics
- Union-find, Simulation, Dynamic programming
- Solved
- No attempts yet
Statement
The game takes place in an infinite grid. Each cell is either white or black. Two cells are considered adjacent if they share a side. A maximum connected set of black cells is called a component. The size of a component is the number of cells in it.
The Brick Tool lets the player repaint any cell to the opposite color. Right after each repaint, the following checks run and can change the grid.
If a cell was repainted to white and the number of components increased, then some component has split into several subcomponents. In this case, all newly formed subcomponents are removed except possibly the largest one. The largest subcomponent is kept only if its size is strictly greater than the sum of the sizes of all the other subcomponents. Removing a component means repainting all of its cells to white.
Next, regardless of the color of the repainted cell, holes in components are removed. If a white cell lies inside any cycle of black cells, it is repainted to black. Two neighboring cells in a cycle must be adjacent in the sense defined above.
Initially all cells are white. You are given a sequence of operations, which must be executed in the given order. Each operation is either a repaint or a query. There are two types of queries. A Comp query asks whether two given cells are both black and belong to the same component. A Size query asks for the size of the component containing a given cell. If that cell is white, the answer is 0.
Input
The first line contains a single integer , the number of repaintings and queries (). Each of the following lines describes one operation.
Draw: repaint the cell at to the opposite color.Size: find the size of the component containing the cell at .Comp: determine whether the cells at and belong to the same component.
All coordinates are positive integers not exceeding .
Output
Print the answers to the queries in the order they appear in the input, one per line. An answer to a Size query is an integer. An answer to a Comp query is either YES or NO.
Hint
The numbers in the pictures below are the number of times the Brick Tool has been used.



