# Cherry and Bits Solutions CENS20A

Page Contents

## Cherry and Bits Solution

Cherry has a paired network A comprising of N lines and M segments. The lines are numbered from 1 to N, sections are numbered from 1 to M. Component at line I (1 ≤ i ≤ N) and segment j (1 ≤ j ≤ M) is meant as Aij. All components of An are either 0 or 1.

He performs Q inquiries on grid. Each question is given by four numbers x1, y1, x2, y2 which characterize the square shape, where (x1, y1) represents the directions of the upper left cell of the square shape, while (x2, y2) represents the directions of the base right cell. You have to flip all the pieces for example (0 to 1, 1 to 0) that are found completely inside the question square shape.

At long last, print the lattice subsequent to playing out all the questions.

Note: x1 speaks to the line number while y1 speaks to the segment number.

Info:

The principal line of the info contains two numbers N and M — the quantity of lines and the quantity of segments in the grid.

Every one of the following N lines contains a line of length M, where the jth character of ith line means the estimation of Ai,j.

Next line contains a whole number Q — the quantity of questions.

At that point follow Q lines with inquiries depictions. Every one of them contains four space-seperated whole numbers x1, y1, x2, y2 — directions of the up left and base right cells of the inquiry square shape.

Yield:

Print the grid, as N strings, in the wake of playing out all the questions.

Limitations

1≤N,M≤1000

0≤Aij≤1

1≤Q≤106

1≤x1≤x2≤N

1≤y1≤y2≤M

Test Input:

00

00

1 2

Test Output:

01

10

Clarification:

Model case 1:

Subsequent to preparing the first inquiry 1, grid becomes:

[1000]

Subsequent to preparing the second inquiry 2, the grid becomes:

[1001]

Subsequent to preparing the third inquiry 1 2, grid becomes:

[0110]

We have to yield the framework subsequent to handling all inquiries.

Solution

import java.io.IOException;

import java.io.PrintWriter;

class Rectangle {

public static void main(String[] args) throws IOException {

PrintWriter out = new PrintWriter(System.out);

StringBuilder sb = new StringBuilder();

int n = Integer.parseInt(in[0]);

int m = Integer.parseInt(in[1]);

char[][] ch = new char[n][m];

for(int i=0;i<n;i++){

}

int[][] arr1 = new int[n+1][m+1];

int[][] arr2 = new int[n+1][m+1];

int[][] ans = new int[n+1][m+1];

while(q–>0) {

int x1 = Integer.parseInt(in[0])-1;

int y1 = Integer.parseInt(in[1])-1;

int x2 = Integer.parseInt(in[2])-1;

int y2 = Integer.parseInt(in[3])-1;

arr1[x1][y1]++;

arr1[x1][y2+1]–;

arr2[x2+1][y1]–;

arr2[x2+1][y2+1]++;

}

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

if(j>0){

arr1[i][j]+=arr1[i][j-1];

}

}

}

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

if(j>0){

arr2[i][j]+=arr2[i][j-1];

}

}

}

for(int i=0;i<n;i++){

for(int j=0;j<m;j++){

ans[i][j]+=arr1[i][j]+arr2[i][j];

if(i>0){

ans[i][j]+=ans[i-1][j];

}

sb.append((ans[i][j]%2==0)?ch[i][j]:getChar(ch[i][j]));

}

sb.append(“n”);

}

out.print(sb);

out.flush();

}

public static char getChar(char ch){

if(ch==’1′){

return ‘0’;

}

return ‘1’;

}

}