Flipping the Vegetable Garden
InterviewTime limit1sMemory limit128 MB
Apply up to one million rectangle flips to an n by n binary garden and print the final layout.
- Level
Medium4 of 10
- Topics
- Prefix sum, Matrix
- Solved
- No attempts yet
Problem
The Byteland allotment garden (BOD) has grown only two vegetables for many years: carrots and parsley. The garden is a square with side length and is divided into unit cells. Exactly one carrot or one parsley plant grows in each cell.
A special cultivation routine gives the vegetables their taste and character. Every year the owner picks one rectangular region and flips the cultivation inside it. Flipping the cultivation means that, within that region, a carrot is planted wherever parsley used to grow and parsley is planted wherever a carrot used to grow. Every cell outside the region stays exactly as it was the year before.
The owner has written down a cultivation plan spanning many years. Determine what the garden will look like after the whole plan has been carried out.
Input
The first line contains one integer (), the size of the garden.
The next lines describe the initial garden. Each line describes one horizontal strip (row) and contains integers (). If is , a carrot grows in that cell; if is , parsley grows there. The -th of these lines is row , and its -th number is the cell in column .
The next line contains one integer (), the number of years in the owner's plan.
Each of the following lines describes one year's flip, in chronological order. A line contains four integers (, ). It means that in that year the cultivation is flipped in every cell whose column index is between and and whose row index is between and ; that is, in the rectangle whose top-left corner is (column , row ) and whose bottom-right corner is (column , row ).
Output
Print the state of the garden after years on lines. Each line describes one horizontal strip (row) and contains integers . Here is if a carrot grows in that cell and if parsley grows there. Separate the numbers on a line with single spaces.
Hint
