## JOI '20 Open P1 - Furniture

View as PDF

Points: 20 (partial)
Time limit: 2.5s
Memory limit: 512M

Problem type

JOI-kun's room is rectangular shaped. It is a grid of blocks. There are horizontal rows. Each row is parallel to the east-west direction. There are vertical columns. Each column is parallel to the south-north direction. The block in the -th row from the north and the -th column from the west is denoted by the block . Pieces of furniture are located in some of the blocks. For , if , there is a piece of furniture in the block . If , there is no furniture in the block .

We say the arrangement of furniture is nice if we can travel from the block to the block without passing through blocks with furniture by repeating travels from one block to another block which is in the south or the east direction. It is guaranteed that the initial arrangement of furniture is nice.

JOI-kun will perform operations. The -th operation will be performed in the following way.

If the arrangement of furniture remains nice if a new piece of furniture is located in the block , then he locates a new piece of furniture in the block . Otherwise, he performs nothing.

Note that he will not perform an operation to a block where a piece of furniture is located initially, or where he already performed an operation. There is no furniture in the block and the block , and he will not perform an operation in these blocks. Write a program which, given the size of the room, the initial arrangement of furniture, and the blocks where he will perform the operations, determines whether a piece of furniture is located or not for each operation.

#### Input Specification

Read the following data from the standard input. All the values in the input are integers.

#### Output Specification

Write lines to the standard output. The -th line should contain 1 if JOI-kun locates a new piece of furniture in the block in the -th operation. Otherwise, it should contain 0.

#### Constraints

• .
• .
• .
• .
• .
• The initial arrangement of furniture is nice.
• .
• .
• .
• .
• .
• .
• .

1. (5 points) , .
2. (95 points) No additional constraints.

#### Sample Input 1

2 3
0 0 1
0 0 0
3
2 2
2 1
1 2

#### Sample Output 1

0
1
0

#### Explanation for Sample 1

The first operation is performed to the block . If a new piece of furniture is located at the block , then the arrangement will not remain nice. Therefore, JOI-kun does not actually locate a piece of furniture at the block . The output should be .

The second operation is performed to the block . If a new piece of furniture is located at the block , we can travel the blocks in this order, for example, and the arrangement will remain nice. Therefore, JOI-kun locates a new piece of furniture at the block . The output should be .

The third operation is performed to the block . Since a piece of furniture is already located at the block , the arrangement will not remain nice if a new piece of furniture is located at the block . Therefore, the output should be .

#### Sample Input 2

2 5
0 0 0 0 0
0 0 0 1 0
2
1 2
2 2

#### Sample Output 2

0
1

#### Explanation for Sample 2

The first operation is performed to the block . If a new piece of furniture is located at the block , then the arrangement will not remain nice. Therefore, JOI-kun does not actually locate a piece of furniture, and the output should be . Note that if we travel the blocks in this order, then we do not pass through blocks with furniture. However, since the travel from the block to the block is toward the north direction, it does not satisfy the condition of nice arrangements.

The second operation is performed to the block . If a new piece of furniture is located at the block , then the arrangement will remain nice since we can travel the blocks in this order. Therefore, JOI-kun locates a new piece of furniture at the block , and the output should be .