We are given a chessboard of size n rows (height) and m columns (width). Rows numbered from 1 to n bottom to top (but that just determines the coloring pattern; note that the usual chessboard coloring: cell (x,y) is black or white based on parity of (x+y) maybe? The image likely shows that (1,1) is black? Let's read problem statement: "The chessboard is painted black and white as follows: An example of a chessboard." So we need to deduce the coloring pattern. Typically a chessboard: bottom-left (1,1) might be black or white? In many programming problems, they define (1,1) as white or black arbitrarily. But we need to determine from sample. We have several test cases with outputs of number of white and black cells after painting.
Given initial board is standard chessboard pattern: alternating colors. We need to compute final counts after two operations: first white paint over rectangle (x1,y1,x2,y2), then black paint over rectangle (x3,y3,x4,y4). Overlapping: later black overrides earlier white if they overlap.
We are given t test cases up to 1e3, n,m up to 1e9, so we need O(1) per test case, no loops.
So we need to compute initial counts of white and black cells on board (based on parity), then adjust for painting operations.
But careful: The initial coloring: The board has rows from bottom (1) to top (n). Columns from left (1) to right (m). The typical chessboard: bottom-left corner (1,1) could be black or white? The example should tell.
We have sample inputs and outputs; we can deduce.
Let's parse sample.
Test case 1:
n=2,m=2
White rect: (1,1,2,2) entire board
Black rect: (1,1,2,2) entire board
So final: all cells black because black over white. So final white=0, black=4. Output: 0 4.
If initial board had some colors, but after painting we get all black. So no info.
Test case 2:
n=3,m=4
White rect: (2,2,3,2) => interesting: y1=y2=2 so it's a rectangle covering columns 2 to 3, row 2 only. So it's a 2x1 horizontal segment? Actually width = (3-2+1)=2 columns, height = (2-2+1)=1 row. So two cells: (2,2) and (3,2). Both become white.
Black rect: (3,1,4,3) => columns 3-4, rows 1-3, so a 2x3 rectangle = 6 cells? Actually width=2, height=3, total 6 cells. This rectangle covers some cells that might have been white painted.
After all operations, output white=3, black=9. Total cells = 12.
So we need initial counts. Let's compute initial white and black counts on 3x4 board.
We need to determine parity: (x+y) mod 2? Usually one color for even sum, another for odd sum. But which one is white? Let's assume (x+y) even => white; odd => black, or vice versa.
Let's compute both possibilities.
Coordinates: x from 1 to 4, y from 1 to 3.
List cells:
(1,1) sum=2 even
(2,1) sum=3 odd
(3,1) sum=4 even
(4,1) sum=5 odd
(1,2) sum=3 odd
(2,2) sum=4 even
(3,2) sum=5 odd
(4,2) sum=6 even
(1,3) sum=4 even
(2,3) sum=5 odd
(3,3) sum=6 even
(4,3) sum=7 odd
So even sum: 6 cells, odd sum: 6 cells? Actually count: even: (1,1),(3,1),(2,2),(4,2),(1,3),(3,3) =6; odd: (2,1),(4,1),(1,2),(3,2),(2,3),(4,3)=6. So equal because total even 12.
Now after operations:
White paint on (2,2) and (3,2): cells (2,2) (even sum? (2,2) sum=4 even) so originally white if even=white, else black. After white paint, these become white regardless.
Black paint on rectangle (3,1)-(4,3): cells:
(3,1) even sum
(4,1) odd
(3,2) odd sum? (3,2) sum=5 odd (but note (3,2) also in white rectangle? yes, (3,2) is in both; white then black overrides, so becomes black)
(4,2) even
(3,3) even
(4,3) odd
So total 6 cells become black after operations, overriding any previous.
Now final colors:
We need to compute final white count = 3 according to output.
Let's compute final colors assuming each cell:
We'll list all 12 cells with final color:
(1,1): not in white rect? white rect only row2, so (1,1) not white painted. Not in black rect? black rect cols 3-4, so col1 not. So remains initial color.
(2,1): not in white (row2 only), not in black (col2? black cols 3-4 only). So initial.
(3,1): in black rect -> black.
(4,1): in black -> black.
(1,2): not white? white includes row2, col2-3, so col1 not. Not black (col1). So initial.
(2,2): white rect -> white, but also black? black rect includes cols 3-4, col2 not. So white.
(3,2): white rect -> white, but also black rect (col3) -> black overrides. So black.
(4,2): black -> black.
(1,3): not white, not black (col1), initial.
(2,3): not white, not black (col2), initial.
(3,3): black -> black.
(4,3): black -> black.
Now count white cells. They are: from above: (2,2) white; plus any initial white that remained. Let's count based on assumed initial color mapping.
Let's test both mappings:
Case A: even sum = white, odd sum = black.
Initial: even cells are white, odd cells black.
Now final:
(1,1) even -> white, remains white.
(2,1) odd -> black, remains black.
(3,1) even -> white -> becomes black (overridden) => black.
(4,1) odd -> black -> black (overridden? actually black paint makes black, so remains black). So black.
(1,2) odd -> black, remains black.
(2,2) even -> white -> white (painted white) => white.
(3,2) odd -> black -> black (overridden by black) => black.
(4,2) even -> white -> black => black.
(1,3) even -> white -> white (not painted) => white.
(2,3) odd -> black -> black (not painted) => black.
(3,3) even -> white -> black => black.
(4,3) odd -> black -> black => black.
Now count whites: (1,1), (2,2), (1,3) => 3 whites. That's exactly output white=3. Blacks count would be 12-3=9. Output black=9. So matches!
Case B: even sum = black, odd sum = white. Then initial whites = odd cells =6, blacks =6.
Now compute final:
(1,1) even -> black (initial) remains black.
(2,1) odd -> white remains white.
(3,1) even -> black -> becomes black? black paint makes black (same) => black.
(4,1) odd -> white -> black => black.
(1,2) odd -> white -> white (no paint) => white.
(2,2) even -> black -> white (white paint) => white.
(3,2) odd -> white -> black => black.
(4,2) even -> black -> black => black.
(1,3) even -> black -> black? no paint => black.
(2,3) odd -> white -> white => white.
(3,3) even -> black -> black => black.
(4,3) odd -> white -> black => black.
Now whites: (2,1), (1,2), (2,2), (2,3)? Wait (2,2) white, (1,2) white, (2,1) white, (2,3) white. Also (???) (4,?) none. So whites: (2,1), (1,2), (2,2), (2,3) =4 whites? Also maybe (4,?) no. Check (3,?) no. (1,1) black, (3,1) black, (4,1) black, (3,2) black, (4,2) black, (1,3) black, (3,3) black, (4,3) black. So whites count=4, not 3. So output says white=3, so mapping B fails.
Thus the correct coloring: (x+y) even => white, odd => black. Good.
So initial board: white cells count = number of cells with (x+y) even, black = total - white.
We need to compute efficiently for huge n,m.
But careful: The board's rows are from bottom to top, but parity based on x+y works regardless of orientation. So fine.
Now we have two painting operations: first white on rectangle R1, then black on rectangle R2. Overlap: if a cell is in both, it ends black. So effectively, we paint white on R1 \ R2, and black on R2 (the entire R2, including intersection). So the net effect: cells in R2 become black regardless of original; cells in R1 \ R2 become white; cells outside both keep original color.
Thus we need to compute final counts:
Let total cells = N = n*m.
Initial white = W0, black = B0 = N - W0.
Let area of R1 = A1, area of R2 = A2, area of intersection I = area of R1 ∩ R2.
Then after painting:
- Cells in R2 (area A2) become black. Among these, some were originally white, some black.
- Cells in R1 but not in R2 (area A1 - I) become white.
- Cells outside both (area N - (A1 ∪ A2)) remain original.
But careful: overlapping region R1 ∩ R2 gets painted white then black, so final black. So indeed the above.
Thus we can compute final white count W_final = (white cells in area outside both) + (white cells from R1\R2? Actually they become white irrespective of original, so all cells in R1\R2 become white; count = area(R1\R2) = A1 - I. But wait: some of those might have originally been white? But that's fine; they become white anyway. So total white = (initial white in region outside both) + (A1 - I). However, we must ensure we don't double count and that we account correctly for black.
Alternatively compute net changes.
Let’s define:
U = all cells.
Define sets:
S1 = R1 (white-painted)
S2 = R2 (black-painted)
After painting:
- For any cell in S2: black.
- For any cell in S1 \ S2: white.
- For any cell not in S1 ∪ S2: original color.
Thus final white = |{cells not in S1 ∪ S2 that were originally white}| + |S1 \ S2|.
final black = total - final white.
So we need to compute:
W_final = (W0) - (white cells that become non-white) + (non-white cells that become white) ??? Better to compute directly.
Compute:
W_final = W0
- (white cells in S2) because those become black.
- (white cells in S1 \ S2? Actually white cells in S1 \ S2 become white anyway, so they remain white. So no subtraction there. However, if they were originally white, they stay white; if originally black, they become white, so we need to add them. So overall:
W_final = W0 - white_in_S2 + black_in_S1\S2.
But black_in_S1\S2 = (area of S1\S2) - white_in_S1\S2.
But we could compute directly.
Alternatively compute:
W_final = W0 - white_in_S2 + (area_of_S1_minus_S2) - white_in_S1_minus_S2.
But white_in_S1_minus_S2 is part of W0? Actually white_in_S1_minus_S2 is subset of W0 that also lies in S1\S2. So subtracting white_in_S1_minus_S2 from W0? Wait let's expand:
W_final = W0 - white_in_S2 + black_in_S1\S2.
But black_in_S1\S2 = (|S1\S2| - white_in_S1\S2).
So W_final = W0 - white_in_S2 + |S1\S2| - white_in_S1\S2 = W0 + |S1\S2| - (white_in_S2 + white_in_S1\S2).
But white_in_S2 + white_in_S1\S2 = white_in_(S2 ∪ (S1\S2))? Actually S2 and S1\S2 are disjoint. So it's white_in_S2 + white_in_S1\S2 = white_in_(S2 ∪ (S1\S2)) = white_in_(S1 ∪ S2) because S1 ∪ S2 = S2 ∪ (S1\S2). So that's white cells in S1 ∪ S2.
Thus W_final = W0 + |S1\S2| - white_in_(S1 ∪ S2).
But |S1\S2| = A1 - I.
So W_final = W0 + (A1 - I) - white_in_(S1 ∪ S2).
But white_in_(S1 ∪ S2) can be computed as white_in_S1 + white_in_S2 - white_in_I (since intersection counted twice). But maybe easier to compute final white directly as: white outside S1∪S2 + area of S1\S2. So:
W_final = white_outside_S1∪S2 + |S1\S2|.
Where white_outside_S1∪S2 = W0 - white_in_S1∪S2.
Thus W_final = W0 - white_in_S1∪S2 + (A1 - I). Same as before.
So we need:
- W0: initial number of white cells on whole board.
- A1: area of R1.
- I: area of intersection of R1 and R2.
- white_in_S1∪S2: number of cells in R1 ∪ R2 that are initially white.
We can compute white_in_S1∪S2 as white_in_S1 + white_in_S2 - white_in_I.
Thus we need to compute for any given axis-aligned rectangle (x1,y1,x2,y2) the number of cells within it that are initially white (i.e., (x+y) even). Similarly, we need total area of rectangle (easy). Intersection area: compute rectangle intersection coordinates: xL = max(x1, x3), xR = min(x2, x4), similarly yB = max(y1, y3), yT = min(y2, y4). If xL <= xR and yB <= yT then intersection exists and I = (xR - xL +1) * (yT - yB +1). Else I = 0.
Now we need to compute white count in any rectangle with dimensions up to 1e9. Since coordinates can be large, we need formula.
So problem reduces to: given rectangle with integer coordinates from x=a to x=b, y=c to y=d inclusive, count number of cells where (x+y) is even.
We can count total cells = (b-a+1)*(d-c+1). The parity pattern: In a grid, half the cells are even sum, half odd sum if total cells even; if odd, there is one more of one parity depending on start corner parity.
We can compute number of even sum cells = ceil(total/2) or floor? Let's derive.
Define f(a,b,c,d) = count of (x,y) with x∈[a,b], y∈[c,d] and (x+y)%2==0.
We can treat parity of x and y. Since (x+y)%2 = (x%2) XOR (y%2) (with parity: even+even=even, odd+odd=even; even+odd=odd). So even sum when parity(x) == parity(y).
Thus count of cells where parity(x)=parity(y) = count where both even + both odd.
Let’s define:
Let X_even = number of x in [a,b] with x%2=0.
X_odd = total_x - X_even.
Similarly Y_even, Y_odd.
Then number of even-sum cells = X_even * Y_even + X_odd * Y_odd.
Because for each pair (x,y) if both even or both odd, sum even.
So we just need counts of evens/odds in integer intervals.
Given integer interval [L,R] inclusive, length len = R-L+1.
Number of evens = floor((R)/2) - floor((L-1)/2).
Number of odds = len - evens.
Or we can compute directly: if L is even, then evens = ((R-L)//2 +1) else evens = ((R - (L+1))//2 +1)? Better use formula.
So we can compute in O(1).
Thus we can compute white_in_R1, white_in_R2, white_in_intersection.
But careful: Are we sure that white = even sum? We deduced from sample. Yes.
Now we need to compute W0 for whole board: use same method: a=1,b=m, c=1,d=n.
Now final white = W0 + (A1 - I) - (white_in_R1 + white_in_R2 - white_in_I).
Alternatively, using earlier formula: W_final = W0 - white_in_S1∪S2 + (A1 - I). So compute white_in_S1∪S2 = white_in_R1 + white_in_R2 - white_in_I.
Thus W_final = W0 + (A1 - I) - (white_in_R1 + white_in_R2 - white_in_I).
Then black = total - W_final.
But we should check if this works with overlapping case and non-overlapping.
Edge case: If R1 and R2 overlap partially, I>0. Our formula accounts.
But what about cells that are painted both? In our calculation, S1\S2 area = A1 - I gets white, S2 gets black. So outside region, we keep original. So that seems correct.
But is there any subtlety about the order? The order is: first white on R1, then black on R2. So if a cell is in R2 but not in R1, it becomes black, overriding original. That is covered because S2 is painted black, irrespective of prior. So final black region includes all S2. So yes.
Now we must ensure that we compute A1 - I correctly. A1 = area of R1.
But wait: what if R2 is entirely inside R1? Then S1\S2 = R1 minus R2 gets white, R2 black. That matches order: first white on R1, then black on R2 (overwrites part of R1). So final: inside R2 black, rest of R1 white, outside unchanged. So formula works.
Now implement.
But we need to be careful about large numbers; use 64-bit (long long). n,m up to 1e9, product up to 1e18, fits in 64-bit signed (9e18). So use long long.
Now we compute:
Function count_even_sum(x1,x2,y1,y2):
len_x = x2 - x1 + 1
x_even = count_even_in_range(x1,x2)
x_odd = len_x - x_even
len_y = y2 - y1 + 1
y_even = count_even_in_range(y1,y2)
y_odd = len_y - y_even
return x_even * y_even + x_odd * y_odd
Where count_even_in_range(L,R):
// number of integers k in [L,R] with k%2==0.
// we can compute: if L>R return 0.
// evens = (R/2) - ((L-1)/2) using floor division.
// In C++: evens = (R/2) - ((L-1)/2) (integer division truncates towards zero, but for positive numbers it's floor). But careful: L,R >=1, positive. So we can use.
// However, if L=0? but L>=1. So fine.
// Better: first even number ge L: first_even = L if L%2==0 else L+1; if first_even > R then 0 else (R - first_even)/2 + 1.
// But simpler: evens = (R/2) - ((L-1)/2). Since integer division truncates, for positive numbers, floor division works.
// Example L=1,R=1: R/2=0, (L-1)/2=0, evens=0 correct.
// L=2,R=2: R/2=1, (L-1)/2=0, evens=1 correct.
// L=1,R=2: R/2=1, (L-1)/2=0, evens=1 correct (even number is 2).
// Works.
// However, careful: In C++ for negative numbers rounding, but here always non-negative.
return (R/2) - ((L-1)/2);
But note that (L-1) might be 0 if L=1, then (0/2)=0, fine.
We'll use long long.
Now compute:
total = n*m.
W0 = count_even_sum(1,m,1,n).
B0 = total - W0.
A1 = (x2-x1+1)*(y2-y1+1)
Compute white1 = count_even_sum(x1,x2,y1,y2)
Similarly for rectangle 2: A2 = (x4-x3+1)*(y4-y3+1); white2 = count_even_sum(x3,x4,y3,y4)
Compute intersection coordinates:
ix1 = max(x1,x3)
ix2 = min(x2,x4)
iy1 = max(y1,y3)
iy2 = min(y2,y4)
if ix1 <= ix2 && iy1 <= iy2:
I = (ix2-ix1+1)*(iy2-iy1+1)
whiteI = count_even_sum(ix1,ix2,iy1,iy2)
else:
I = 0
whiteI = 0
Then:
white_union = white1 + white2 - whiteI
W_final = W0 + (A1 - I) - white_union
But wait: does this formula double count anything? Let's verify with test case 2.
Test case 2:
n=3,m=4 => total=12.
W0 = count_even_sum(1,4,1,3) = we computed 6? Actually earlier we had 6 even sum cells. That matches.
R1: (2,2,3,2): A1 = (3-2+1)(2-2+1)=21=2
white1 = count_even_sum(2,3,2,2): x in [2,3], y=2.
x_even: from 2 to3: 2 even, 3 odd => x_even=1, x_odd=1
y_even: y=2 even => y_even=1, y_odd=0
white1 = 11 + 10 =1. Indeed (2,2) even sum, (3,2) odd sum => only one white originally.
R2: (3,1,4,3): A2 = (4-3+1)(3-1+1)=23=6
white2 = count_even_sum(3,4,1,3):
Compute x_even: [3,4]: 3 odd,4 even => x_even=1, x_odd=1.
y_even: y from1 to3: evens in [1,3]? numbers: 1 odd,2 even,3 odd => y_even=1 (y=2), y_odd=2.
white2 = x_eveny_even + x_oddy_odd = 11 + 12 =3. Let's list white cells: (3,2) odd? Actually (3,2) sum=5 odd, so not white; (4,2) sum=6 even -> white; (3,1) sum=4 even -> white; (4,1) sum=5 odd -> black; (3,3) sum=6 even -> white; (4,3) sum=7 odd -> black. So white cells: (3,1),(4,2),(3,3) =3. Good.
Intersection: R1 ∩ R2? R1 is y=2 only, columns 2-3. R2 is cols 3-4, rows 1-3. Intersection: x: max(2,3)=3, min(3,4)=3 => x=3; y: max(2,1)=2, min(2,3)=2 => y=2. So single cell (3,2). I=1. whiteI = count_even_sum(3,3,2,2): (3,2) sum=5 odd => whiteI=0.
Now white_union = white1+white2 - whiteI =1+3-0=4.
W_final = W0 + (A1 - I) - white_union =6 + (2-1) -4 =6+1-4=3. Yes correct.
Now check test case 5 from sample to ensure formula works.
Input:
5th test case:
3 4
1 2 4 2
2 1 3 3
Wait, the input listing:
3 4
1 2 4 2
2 1 3 3
But from the sample: The last test case in Input:
5
2 2 ... (first)
3 4 ... (second)
1 5 ... (third)
4 4 ... (fourth)
3 4 ... (fifth)
Wait we need to re-check sample input lines:
The Input given:
5
2 2
1 1 2 2
1 1 2 2
3 4
2 2 3 2
3 1 4 3
1 5
1 1 5 1
3 1 5 1
4 4
1 1 4 2
1 3 4 4
3 4
1 2 4 2
2 1 3 3
So test case 5: n=3, m=4, white rect: (1,2,4,2) meaning x1=1,y1=2,x2=4,y2=2 => row 2 columns 1-4 => 4 cells (a horizontal line across full width). Black rect: (2,1,3,3) => columns 2-3, rows 1-3 => 2x3=6 cells.
Output for test case 5: 4 8. Total cells = 12.
Let's compute manually to verify formula.
First compute initial board: as before, W0=6, B0=6.
R1: (1,2,4,2): A1 = (4-1+1)(2-2+1)=41=4.
white1: count of white cells in that row? Row y=2. x from 1 to 4.
(1,2) sum=3 odd => black
(2,2) sum=4 even => white
(3,2) sum=5 odd => black
(4,2) sum=6 even => white
So white1 = 2.
R2: (2,1,3,3): A2 = (3-2+1)(3-1+1)=23=6.
white2: we need to compute count of even sum cells in that rectangle.
Coordinates: x=2,3; y=1,2,3.
List:
(2,1) sum=3 odd -> black
(3,1) sum=4 even -> white
(2,2) sum=4 even -> white
(3,2) sum=5 odd -> black
(2,3) sum=5 odd -> black? sum=5 odd? 2+3=5 odd -> black
(3,3) sum=6 even -> white
So white2 = 3 (cells (3,1),(2,2),(3,3)). Wait (2,2) is white? Actually 2+2=4 even, yes white. So white2=3.
Intersection: R1∩R2: R1 is row2, cols1-4; R2 is cols2-3, rows1-3. Intersection: cols2-3, row2 => 2 cells: (2,2) and (3,2). So I = 2.
whiteI: count white in intersection: (2,2) white, (3,2) black => whiteI=1.
Now compute:
white_union = white1+white2 - whiteI =2+3-1=4.
W_final = W0 + (A1 - I) - white_union =6 + (4-2) -4 =6+2-4=4. Output white=4, black=8. Correct.
Thus formula works.
Now we also need to consider potential overflow: Use 128-bit? But product up to 1e18 fits in signed 64-bit (max 9e18). However intermediate multiplication like count_even uses multiplication of counts up to 1e9, product up to 1e18, still fits in 64-bit. Summation also fits. So long long (or int64) is enough.
Now implement for each test case.
Edge Cases:
- Overlap may be zero, then I=0, whiteI=0.
- Board could be 1x1, etc.
- Ensure correct integer types.
Now confirm with sample test case 3 and 4.
Test case 3:
1 5
White: (1,1,5,1) => entire row? n=1, m=5, so board is a single row of 5 cells. White rectangle covers entire board (since y1=1,y2=1, x1=1,x2=5). So A1=5, white1 = count white in whole board =? We'll compute.
Black rectangle: (3,1,5,1) => columns 3-5, row1 => area 3.
Output: 2 3 (white=2, black=3). Total cells=5.
Let's compute:
n=1,m=5 => total=5.
Initial white: count even sum in row y=1: x=1..5, sums: 1+1=2 even, 2+1=3 odd,3+1=4 even,4+1=5 odd,5+1=6 even. So white0 = 3 (x=1,3,5). Actually even sum = white, so whites: (1,1),(3,1),(5,1) => 3 whites. Blacks: (2,1),(4,1) => 2 blacks. But output after painting gives white=2, black=3. So final white decreased by 1, black increased by 1.
Now our formula:
W0=3.
R1: (1,1,5,1): A1=5, white1 = white0 =3 (since entire board).
R2: (3,1,5,1): A2=3, white2 = count white in cols3-5, row1: (3,1) white, (4,1) black, (5,1) white => white2=2.
Intersection: R1 ∩ R2 = R2 (since R2 subset of R1): I = 3, whiteI = white2 =2.
Now white_union = white1+white2 - whiteI =3+2-2=3.
W_final = W0 + (A1 - I) - white_union =3 + (5-3) -3 =3+2-3=2. Correct.
Test case 4:
4 4
White rect: (1,1,4,2) => columns 1-4, rows 1-2 => area = 42=8.
Black rect: (1,3,4,4) => columns 1-4, rows 3-4 => area = 42=8.
No overlap? Actually R1 rows 1-2, R2 rows 3-4, so disjoint => I=0.
Output: 8 8. Total =16.
Compute:
Board 4x4. Initial white: let's compute W0: 4x4 board half white? 16 total, typically 8 white, 8 black if even dimensions. Indeed initial likely 8 white, 8 black. So W0=8.
R1: A1=8, white1 = count white in rows 1-2, cols 1-4. Let's compute:
Rows 1-2:
Row1: x=1..4: sums with y=1: (1+1=2 even white), (2+1=3 odd black), (3+1=4 even white), (4+1=5 odd black) => whites: 2.
Row2: y=2: (1+2=3 odd black), (2+2=4 even white), (3+2=5 odd black), (4+2=6 even white) => whites:2.
Total white1=4? Actually sum: row1 whites 2, row2 whites 2 => 4. So white1=4.
R2: rows 3-4, cols1-4.
Row3: y=3: (1+3=4 even white), (2+3=5 odd black), (3+3=6 even white), (4+3=7 odd black) => 2 whites.
Row4: y=4: (1+4=5 odd black), (2+4=6 even white), (3+4=7 odd black), (4+4=8 even white) => 2 whites.
So white2=4.
I=0, whiteI=0.
white_union = white1+white2=8.
W_final = W0 + (A1 - I) - white_union =8 + (8-0) -8 =8. So white=8, black=8. Output matches.
Thus formula works.
Now implement solve() reading t test cases.
Algorithm per test case:
- Read n,m.
- Read x1,y1,x2,y2 for white paint.
- Read x3,y3,x4,y4 for black paint.
- Compute total = n*m.
- Compute W0 = count_even_sum(1, m, 1, n).
- Compute A1 = (x2-x1+1)*(y2-y1+1)
white1 = count_even_sum(x1,x2,y1,y2)
- Compute A2 = (x4-x3+1)*(y4-y3+1)
white2 = count_even_sum(x3,x4,y3,y4)
- Compute intersection:
ix1 = max(x1,x3); ix2 = min(x2,x4);
iy1 = max(y1,y3); iy2 = min(y2,y4);
if ix1 <= ix2 && iy1 <= iy2:
I = (ix2-ix1+1)*(iy2-iy1+1);
whiteI = count_even_sum(ix1,ix2,iy1,iy2)
else:
I = 0; whiteI = 0;
- Compute W_final = W0 + (A1 - I) - (white1 + white2 - whiteI);
Ensure W_final non-negative and not exceed total, but should be fine.
- B_final = total - W_final.
- Print W_final and B_final.
Check for overflow: A1 and I up to 1e18, but we subtract. Use long long.
Now the count_even_sum function: we can compute number of even numbers in [L,R] as:
long long count_even(long long L, long long R) {
if (L > R) return 0;
return (R / 2) - ((L - 1) / 2);
}
But careful: For L=1, (L-1)/2 = 0/2=0 fine. For L=0? not used. Works for positive.
Then count_even_sum(x1,x2,y1,y2):
long long len_x = x2 - x1 + 1;
long long x_even = count_even(x1, x2);
long long x_odd = len_x - x_even;
long long len_y = y2 - y1 + 1;
long long y_even = count_even(y1, y2);
long long y_odd = len_y - y_even;
return x_even * y_even + x_odd * y_odd;
Now test with sample.
But is there any nuance about the coordinate orientation? The board rows bottom to top doesn't affect parity; we used y as row number.
Now confirm with large values: n,m up to 1e9, product up to 1e18, our calculations are safe in 64-bit if we avoid overflow in multiplication like (x_even * y_even) may be up to (5e8)*(5e8)=2.5e17, still fine. Actually x_even <= m up to 1e9, y_even <= n up to 1e9, product up to 1e18. But sum of two such products can be up to 2e18, still within signed 64-bit? 2^63-1 ≈ 9.22e18, so safe. However, we must be careful when adding many terms; but overall fine.
Now think about possible pitfalls: The formula for final white might yield negative if something wrong? But should not.
Now also consider alternative approach: compute final black directly, but our formula is fine.
Let's test a random case manually.
Suppose n=1,m=1. White rect maybe (1,1,1,1). Black rect maybe (1,1,1,1). Then total=1, W0: (1,1) sum=2 even => white => W0=1. A1=1, white1=1. A2=1, white2=1. Intersection I=1, whiteI=1. Then white_union = 1+1-1=1. W_final = W0 + (A1-I) - white_union =1+(1-1)-1=0. So final white=0, black=1. Correct: black overrides.
If white rect (1,1,1,1), black rect (1,1,1,1) but reversed? Already same.
If white rect empty? Not possible because x1<=x2, etc.
Now consider case where R1 and R2 are disjoint and both contain many cells. Works.
Now confirm that we properly accounted for cells that are painted white then black: In S1 ∩ S2, we subtract white_in_S1∪S2? Let's double-check reasoning.
Our final white count formula: W_final = white_outside_union + area(S1\S2). Because:
- Outside union: keep original color. So white count = white_in_outside = W0 - white_in_union.
- S1\S2: all become white, so all those cells are white, regardless of original. So add |S1\S2|.
- S2: all become black, so none white.
So total white = (W0 - white_in_union) + |S1\S2| = W0 + |S1\S2| - white_in_union. This matches.
Thus we don't need to compute A2 at all except for intersection. But we used A1 and I, not A2. So indeed we only need A1, I, W0, white_union.
Now white_union = white1 + white2 - whiteI.
Thus final white = W0 + (A1 - I) - (white1 + white2 - whiteI).
We could also compute final black = total - final white.
Now we might also compute directly using black counts, but this is fine.
Now verify that (A1 - I) = |S1\S2| is indeed area of white-painted-only region. That's correct because S1\S2 area = A1 - area(S1∩S2) = A1 - I.
Now ensure that intersection area I is computed correctly. But note: Could be that the intersection is larger than zero but we must compute its area. Good.
Now we might also need to ensure that we treat coordinates correctly: The problem says rows from bottom to top, but coordinates (x,y) have x column, y row. Our parity based on (x+y) should be consistent regardless of y orientation. If y orientation were reversed (i.e., top to bottom), parity might change if we used (x+y) but we defined y from 1 to n, bottom to top. That's fine; parity is still based on sum. The initial board image might have bottom-left cell black or white? We deduced white for even sum. That matches sample. So orientation is consistent: bottom-left (1,1) -> sum=2 even -> white. In many chessboard images, bottom-left is often dark (black). But here it's white? Let's check sample: In test case 4 with board 4x4, initial board had 8 white and 8 black. In output after painting they got 8 white, 8 black. Without painting, they'd have 8 white, 8 black. So we don't know initial distribution. But from test case 2 we deduced that (1,1) is white because final white count matched that assignment. So bottom-left is white. That's okay.
Thus algorithm is correct.
Now we can code.
But let's consider potential pitfalls: The product (x2-x1+1)*(y2-y1+1) may overflow if we compute using int; we use long long.
Input constraints: t up to 1000, fine.
Implementation details: Use typedef long long ll.
Function to count even numbers in range:
ll even_cnt(ll l, ll r) {
return (r/2) - ((l-1)/2);
}
But careful: For l=0? Not possible. But if l=1, l-1=0, 0/2=0.
Alternatively:
if(l>r) return 0;
return (r/2) - ((l-1)/2);
Works for positive.
Now count_even_sum rectangle:
ll count_white(ll x1, ll x2, ll y1, ll y2) {
ll lenx = x2 - x1 + 1;
ll evenx = even_cnt(x1, x2);
ll oddx = lenx - evenx;
ll leny = y2 - y1 + 1;
ll eveny = even_cnt(y1, y2);
ll oddy = leny - eveny;
return evenx * eveny + oddx * oddy;
}
Now main loop.
Test with sample.
Edge Cases: Might need to compute total as ll n*m. Since n,m are up to 1e9, product up to 1e18. Use 64-bit.
Now also consider that our formula may produce W_final outside [0, total] due to some miscalculation? But shouldn't.
Now confirm with scenario where R1 and R2 overlap exactly: Then I = A1 = A2 (if equal) or I = min(A1, A2) if one contains the other. For example, R1 entirely inside R2: Then S1\S2 empty => A1-I =0. So W_final = W0 + 0 - (white1+white2-whiteI). But note that white_union = white1+white2-whiteI. Since R1 inside R2, white_union = white1+white2 - white1 = white2. So W_final = W0 - white2. That makes sense: All cells originally in R2 become black, so white cells in R2 are removed; cells outside remain. So final white = W0 - white_in_R2. That matches because S1\S2 empty and S2 covers R2. Good.
If R2 inside R1: Then S1\S2 = R1 minus R2, area A1 - I. white_union = white1+white2 - whiteI. Since white2 includes whiteI? Actually whiteI is white in intersection, which is part of white2. So white_union = white1 + (white2) - whiteI = white1 + (white2_without_intersection) = white1 + (white2 - whiteI). But white1 includes whiteI as well. So white_union = (white1_without_intersection + whiteI) + (white2_without_intersection) = white1_without + white2_without + whiteI. Wait not exactly. But final white = W0 + (A1 - I) - white_union. That should equal W0 - white1_without? Let's derive: Since R2 inside R1, final: R2 black, rest of R1 white, outside unchanged. So white = (white_outside) + (area of R1\R2). white_outside = W0 - white_in_R1 - white_in_outside_R2? Actually outside both = cells not in R1 (since R2 inside R1). So outside = U \ R1. White_outside = W0 - white_in_R1. So final white = (W0 - white_in_R1) + (A1 - I). That is W0 + (A1 - I) - white_in_R1. In our formula we have W0 + (A1 - I) - (white1+white2 - whiteI). But note white1+white2 - whiteI = white1 + (white2 - whiteI). Since white2 - whiteI = white_in_R2\R1? Actually R2 inside R1, so R2\R1 empty, so white2 = whiteI, so white2 - whiteI =0. So white1+white2 - whiteI = white1. So our formula gives W0 + (A1 - I) - white1, which matches. Good.
Thus consistent.
Now we should ensure that we treat coordinates as given: x is column, y is row. So parity based on (x+y). Fine.
Now complexity O(t). Good.
Now final step: implement.
But also think about potential pitfalls: The board dimensions are n (rows) and m (columns). In our count_even_sum we used y range from 1 to n. That's fine.
Now one more nuance: The board's initial coloring might be defined such that the cell (1,1) is black. But we deduced from sample it's white. Could there be other interpretations? For safety, we can compute initial white count based on (x+y) even or odd by inferring from test cases? Since problem statement says "The chessboard is painted black and white as follows: ". So we have to rely on image but it's not provided; from sample we deduced even sum = white. This is typical in many Codeforces problems: (1,1) is white. Also note that in many CF problems, they often define that cell (1,1) is white. So it's safe.
Now implement solve().
Will we need to handle large t up to 1000; each test constant operations.
Now let's code.
Potential integer division: In C++, integer division truncates towards zero. For positive numbers it's floor. So even_cnt works.
But note: For l=1, (l-1)/2 =0. Good.
Now for counting even in range [L,R] if L and R are both positive. Good.
Now test some random small cases with brute-force to verify.
But we can also think about alternative simpler formula: The number of white cells in rectangle can also be computed as (total+1 if first cell white?) but our parity method is clean.
Now proceed.
But wait: The problem statement says rows are numbered from 1 to n from bottom to top. That might affect parity if we used (x + (n-y+1))? But they said do not mix up; coordinates (x,y) are as given. The coloring is based on the board; presumably the standard chessboard pattern with alternating colors, so (1,1) bottom-left. In the description they gave an example image. From sample we matched using (x+y) even -> white. So the orientation is consistent; even though rows are bottom to top, the parity sum remains same: bottom-left (1,1) has sum 2 even, so white. So fine.
Now I think we are ready.
But I'll double-check sample test case 2 with an alternative mapping: Could there be an off-by-one in parity due to row numbering? If rows were top to bottom, then (1,1) would be top-left. But they explicitly said rows from bottom to top, so (1,1) is bottom-left. So using (x+y) we got correct. So it's consistent.
Now we can produce final answer.
One more nuance: The board dimensions can be up to 1e9, but the rectangles may also be large. The count_even_sum function may overflow when computing len_x * len_y? Not used directly but we compute areas separately. In count_white we multiply evenx * eveny which each up to 1e9 so product up to 1e18; that's okay. But we also compute total = n*m, which is up to 1e18. Use unsigned long long? signed long long can hold up to 9.22e18, so up to 1e18 fits. But if n=m=1e9, product=1e18; ok. If n=1e9, m=1e9, product=1e18; plus some other sums still < 2e18, still safe.
But what about (x2-x1+1)*(y2-y1+1) could be up to 1e18 as well, but fits.
Now implement.
We'll define:
using ll = long long;
ll countEven(ll l, ll r) {
return (r / 2) - ((l - 1) / 2);
}
ll whiteInRect(ll x1, ll x2, ll y1, ll y2) {
ll x_even = countEven(x1, x2);
ll x_odd = (x2 - x1 + 1) - x_even;
ll y_even = countEven(y1, y2);
ll y_odd = (y2 - y1 + 1) - y_even;
return x_even * y_even + x_odd * y_odd;
}
Then main:
int t; cin >> t;
while(t--){
ll n,m; cin>>n>>m;
ll x1,y1,x2,y2; cin>>x1>>y1>>x2>>y2;
ll x3,y3,x4,y4; cin>>x3>>y3>>x4>>y4;
ll total = n*m;
ll W0 = whiteInRect(1, m, 1, n);
ll A1 = (x2 - x1 + 1) * (y2 - y1 + 1);
ll white1 = whiteInRect(x1, x2, y1, y2);
ll A2 = (x4 - x3 + 1) * (y4 - y3 + 1); // not needed except maybe for debug
ll white2 = whiteInRect(x3, x4, y3, y4);
ll ix1 = max(x1, x3);
ll ix2 = min(x2, x4);
ll iy1 = max(y1, y3);
ll iy2 = min(y2, y4);
ll I = 0;
ll whiteI = 0;
if (ix1 <= ix2 && iy1 <= iy2) {
I = (ix2 - ix1 + 1) * (iy2 - iy1 + 1);
whiteI = whiteInRect(ix1, ix2, iy1, iy2);
}
ll white_union = white1 + white2 - whiteI;
ll W_final = W0 + (A1 - I) - white_union;
ll B_final = total - W_final;
cout << W_final << " " << B_final << "\n";
}
Now test with given sample.
Should output as expected.
Now consider additional test: n=1,m=1, white rect = (1,1,1,1), black rect = (1,1,1,1) => output 0 1. Good.
Another test: n=1,m=2, white rect (1,1,1,1), black rect (2,1,2,1). Then board initially: (1,1) sum=2 even white, (2,1) sum=3 odd black. After white on (1,1) -> white; black on (2,1) -> black. So final white=1, black=1. Our computation: total=2, W0 = whiteInRect(1,2,1,1) => x1=1,x2=2,y1=1,y2=1 => x_even: countEven(1,2) -> (2/2)-(0/2)=1-0=1; x_odd=1; y_even: countEven(1,1)= (1/2)-((0)/2)=0-0=0? Actually (1/2)=0, (0/2)=0, so y_even=0; y_odd=1; whiteInRect = 10 + 11 =1. So W0=1. A1 = 11=1, white1 = whiteInRect(1,1,1,1) => (1,1) even -> white count 1? Compute: x_even(1,1) countEven(1,1)=? (1/2)=0, (0/2)=0 =>0? That's odd: (1,1) sum=2 even, but countEven(1,1) gave 0? That seems wrong. Let's compute manually: In range [1,1], there is one integer: 1. 1 is odd. So number of evens is 0. That is correct: only integer 1 is odd. But we need white count for cell (1,1) where x=1 odd, y=1 odd? Wait y=1 odd, parity matches (both odd) => sum even. In our whiteInRect we use: evenx * eveny + oddx * oddy. x_even=0, x_odd=1; y_even=0, y_odd=1; so whiteInRect = 00 + 1*1 = 1. So indeed whiteInRect=1. Good. So white1=1.
Black rect: (2,1,2,1): A2=1, white2 = whiteInRect(2,2,1,1): x_even? 2 even so x_even=1, x_odd=0; y_even=0, y_odd=1; white2 = 10 + 01 =0. So white2=0.
Intersection: none? x1=1 vs x3=2, no overlap, I=0.
white_union = white1+white2=1.
W_final = W0 + (A1 - I) - white_union =1 + (1-0) -1 =1. So white=1, black=1. Correct.
Now test a scenario where R1 and R2 overlap partially but the parity formula is correct.
Thus solution is robust.
Now we can finalize.
But we might need to consider if we could get negative W_final due to some miscalculation? Let's test extreme: board 1x1, white rect maybe doesn't cover any? But white rect always valid and could be not covering whole board. Suppose n=1,m=1, white rect (1,1,1,1) (area 1) and black rect (1,1,1,1) (area 1). We already computed W_final=0. Good.
Now if white rect is empty? Not possible. So fine.
Now we need to be careful about potential integer division when computing even_cnt for large numbers. The formula (r/2) - ((l-1)/2) works for positive integers. Since we use long long, division truncates towards zero. But when l=0? Not used. If l=1, l-1=0, 0/2=0. So ok.
Now edge: if l=0? Not used. But suppose in some case coordinates start at 1 always. Good.
Now we can produce final code.
I'll add comments.
Also ensure using namespace std; for brevity.
Now we must note that reading large numbers, we use scanf or cin with sync.
Now final answer.
One more thought: Could there be any issue with the board orientation? Let's consider the possibility that the parity might be (x + (n-y+1))? Since rows from bottom to top, but the coloring might be based on visual pattern, e.g., bottom-left is black. In many chessboards, bottom-left is dark (black). However the sample gave bottom-left as white. But we can't rely on typical chessboard; we rely on sample. The sample matched even sum = white. So it's correct.
To be thorough, let's check the sample's first test case: n=2,m=2, white rect all, black rect all. Output 0 4. No info.
Second test case we used to deduce.
Third test case also consistent.
Fourth: also consistent.
So fine.
Now we implement.
But wait: In the problem statement they said "rows on the chessboard are numbered from 1 to n from bottom to top." That means that (x,y) with y=1 is bottom row. So the typical chessboard would have alternating colors; if you stand at the board, the bottom-left is often a dark square. However, the image might show bottom-left as white? It's not provided. But the sample shows that (1,1) is white in our deduction. So it's consistent with the problem's intended coloring. So go ahead.
Now final solution.
But we should also verify if there is any possibility of double counting in white_union: white1 and white2 might both count whiteI, we subtract one, good.
Now we can code.
I will produce final answer.
However, consider that some participants might compute using formula for white count as ceil(total/2) or floor. But we need rectangle-specific.
Now done.
Now I'll write solution.
Let's also check if any potential overflow in multiplication: (x2 - x1 + 1) can be up to 1e9, similarly for y, product up to 1e18; we store in ll, fine.
Now finalize.
But wait: Should we compute A1 - I or (A1 - I) could be negative? Since I <= A1, because intersection area cannot exceed area of R1. So it's non-negative.
Now output.
Time complexity: O(t).
Now I will produce final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// number of even integers in [l, r] (l, r inclusive, l >= 1)
ll even_cnt(ll l, ll r) {
return (r / 2) - ((l - 1) / 2);
}
// number of white cells in rectangle (x1..x2, y1..y2)
// white = (x+y) even
ll white_in_rect(ll x1, ll x2, ll y1, ll y2) {
ll x_even = even_cnt(x1, x2);
ll x_odd = (x2 - x1 + 1) - x_even;
ll y_even = even_cnt(y1, y2);
ll y_odd = (y2 - y1 + 1) - y_even;
return x_even * y_even + x_odd * y_odd;
}
void solve() {
int t;
cin >> t;
while (t--) {
ll n, m;
cin >> n >> m;
ll x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
ll x3, y3, x4, y4;
cin >> x3 >> y3 >> x4 >> y4;
ll total = n * m;
ll W0 = white_in_rect(1, m, 1, n); // initial white cells
// white rectangle
ll A1 = (x2 - x1 + 1) * (y2 - y1 + 1);
ll white1 = white_in_rect(x1, x2, y1, y2);
// black rectangle
ll white2 = white_in_rect(x3, x4, y3, y4);
// intersection
ll ix1 = max(x1, x3), ix2 = min(x2, x4);
ll iy1 = max(y1, y3), iy2 = min(y2, y4);
ll I = 0, whiteI = 0;
if (ix1 <= ix2 && iy1 <= iy2) {
I = (ix2 - ix1 + 1) * (iy2 - iy1 + 1);
whiteI = white_in_rect(ix1, ix2, iy1, iy2);
}
ll white_union = white1 + white2 - whiteI;
ll white_final = W0 + (A1 - I) - white_union;
ll black_final = total - white_final;
cout << white_final << " " << black_final << "\n";
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
return 0;
}