Under construction

Still being built. Things may change

Antichamber

Time limit5sMemory limit1024 MB

Summary
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 NN, the number of repaintings and queries (1≤N≤1051 \le N \le 10^5). Each of the following NN lines describes one operation.

  • Draw xx yy: repaint the cell at (x,y)(x, y) to the opposite color.
  • Size xx yy: find the size of the component containing the cell at (x,y)(x, y).
  • Comp x1x_1 y1y_1 x2x_2 y2y_2: determine whether the cells at (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) belong to the same component.

All coordinates are positive integers not exceeding 10610^6.

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.

PictureDescription
Playfield after the 14th use of the Brick Tool
The 15th use does not change the playfield.
Playfield after the 18th use of the Brick Tool
After the 21st use, all cells in the playfield are white again.

Examples1

  1. Example 1

    Input
    38
    Draw 2 2
    Draw 2 3
    Draw 2 4
    Draw 2 5
    Draw 2 6
    Draw 3 6
    Draw 4 6
    Draw 5 5
    Draw 5 4
    Draw 4 4
    Draw 4 2
    Size 4 2
    Size 2 4
    Size 4 4
    Size 3 3
    Comp 2 2 2 4
    Comp 2 2 4 4
    Comp 3 3 4 4
    Draw 3 2
    Draw 4 3
    Size 2 2
    Draw 5 6
    Size 2 2
    Draw 3 3
    Size 3 3
    Comp 3 3 4 4
    Draw 2 4
    Draw 3 4
    Draw 4 4
    Size 3 2
    Size 3 6
    Draw 4 7
    Draw 3 5
    Size 2 5
    Draw 4 6
    Size 2 5
    Size 4 5
    Size 4 7
    
    Expected output
    1
    7
    3
    0
    YES
    NO
    NO
    13
    18
    18
    YES
    0
    9
    9
    0
    0
    0