题目
题目描述
Masha 有一个宽度为 nn,长度为 mm 的国际象棋盘。
这个象棋盘左下角的坐标为 (1,1)(1,1),且颜色为白色。(象棋盘是黑白相间的)
现在 Maxim 首先选择了一个矩形,并把这个矩形内的象棋盘刷成了黑色,然后 Denis 又选择了一个矩形并把这个矩形内的象棋盘刷成了白色。 (注意先后顺序)
请输出这个象棋盘最后有多少个黑色格子,多少个白色格子。
输入格式
第一行一个整数 tt,表示有 tt 组数据。
每组数据第一行两个整数 n,mn,m
接下来一行四个整数 x1,y1,x2,y2x1,y1,x2,y2,表示 Maxim 选定矩形的左下角坐标和右上角坐标。
接下来一行也是四个整数 x3,y3,x4,y4x3,y3,x4,y4,表示 Denix 选定矩形的左下角坐标和右上角坐标。
输出格式
对于每一组数据,输出一行两个整数,表示白色格子的数量和黑色格子的数量 (白前黑后)
数据范围
1\leq t\leq 10^31≤t≤10
3
1 \leq n,m \leq 10^91≤n,m≤10
9
1 \leq x1 \leq x2 \leq m1≤x1≤x2≤m, 1 \leq x3 \leq x4 \leq m1≤x3≤x4≤m
1 \leq y1 \leq y2 \leq n1≤y1≤y2≤n, 1 \leq y3 \leq y4 \leq n1≤y3≤y4≤n
题目描述
Recently, Masha was presented with a chessboard with a height of n n and a width of m m .
The rows on the chessboard are numbered from 1 1 to n n from bottom to top. The columns are numbered from 1 1 to m m from left to right. Therefore, each cell can be specified with the coordinates (x,y) (x,y) , where x x is the column number, and y y is the row number (do not mix up).
Let us call a rectangle with coordinates (a,b,c,d) (a,b,c,d) a rectangle lower left point of which has coordinates (a,b) (a,b) , and the upper right one — (c,d) (c,d) .
The chessboard is painted black and white as follows:
An example of a chessboard.Masha was very happy with the gift and, therefore, invited her friends Maxim and Denis to show off. The guys decided to make her a treat — they bought her a can of white and a can of black paint, so that if the old board deteriorates, it can be repainted. When they came to Masha, something unpleasant happened: first, Maxim went over the threshold and spilled white paint on the rectangle (x_1,y_1,x_2,y_2) (x
1
,y
1
,x
2
,y
2
) . Then after him Denis spilled black paint on the rectangle (x_3,y_3,x_4,y_4) (x
3
,y
3
,x
4
,y
4
) .
To spill paint of color color color onto a certain rectangle means that all the cells that belong to the given rectangle become color color . The cell dyeing is superimposed on each other (if at first some cell is spilled with white paint and then with black one, then its color will be black).
Masha was shocked! She drove away from the guests and decided to find out how spoiled the gift was. For this, she needs to know the number of cells of white and black color. Help her find these numbers!
输入输出格式
输入格式:
The first line contains a single integer t t ( 1 \le t \le 10^3 1≤t≤10
3
) — the number of test cases.
Each of them is described in the following format:
The first line contains two integers n n and m m ( 1 \le n,m \le 10^9 1≤n,m≤10
9
) — the size of the board.
The second line contains four integers x_1 x
1
, y_1 y
1
, x_2 x
2
, y_2 y
2
( 1 \le x_1 \le x_2 \le m, 1 \le y_1 \le y_2 \le n 1≤x
1
≤x
2
≤m,1≤y
1
≤y
2
≤n ) — the coordinates of the rectangle, the white paint was spilled on.
The third line contains four integers x_3 x
3
, y_3 y
3
, x_4 x
4
, y_4 y
4
( 1 \le x_3 \le x_4 \le m, 1 \le y_3 \le y_4 \le n 1≤x
3
≤x
4
≤m,1≤y
3
≤y
4
≤n ) — the coordinates of the rectangle, the black paint was spilled on.
输出格式:
Output t t lines, each of which contains two numbers — the number of white and black cells after spilling paint, respectively.
输入输出样例
输入样例#1: 复制
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
输出样例#1: 复制
0 4
3 9
2 3
8 8
4 8
说明
Explanation for examples:
The first picture of each illustration shows how the field looked before the dyes were spilled. The second picture of each illustration shows how the field looked after Maxim spoiled white dye (the rectangle on which the dye was spilled is highlighted with red). The third picture in each illustration shows how the field looked after Denis spoiled black dye (the rectangle on which the dye was spilled is highlighted with red).
In the first test, the paint on the field changed as follows:
In the second test, the paint on the field changed as follows:
In the third test, the paint on the field changed as follows:
In the fourth test, the paint on the field changed as follows:
In the fifth test, the paint on the field changed as follows:
思路
我们对一个棋盘填色,不仅仅只是减去一半就完了,对于长宽都是奇数的棋盘,我们要特别判断。例如:
1 0 1
0 1 0
1 0 1
(1是黑,0是白)
这样的棋盘如果我们涂黑,就是涂9/2+19/2+1个,根据题意,当一个格子奇偶相同,那么就是白色,不同就是黑色。
我们就先涂白的,再不考虑交集涂黑的,那么你会问了,如果有交集,黑的不就涂少了?对,是涂少了,我们再判断加上就好了。
例如, 交集是:
1 0 1
0 1 0
1 0 1
我们先涂白:
0 0 0
0 0 0
0 0 0
再不考虑涂白,将它涂黑:
0 1 0
1 0 1
0 1 0
那么我们就要处理,将它变成这样:
1 1 1
1 1 1
1 1 1
这不就是再进行一遍涂黑的工作吗?
代码
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll getblack(int x1,int y1,int x2,int y2)
{
bool b=(x1+y1)&1;
int n=x2-x1+1,m=y2-y1+1;
if(b) return 1ll*n*(m/2)+((m&1)?(n/2)+(n&1):0);
else return 1ll*n*(m/2)+((m&1)?(n/2):0);
}
ll getwhite(int x1,int y1,int x2,int y2)
{
return 1ll*(x2-x1+1)*(y2-y1+1)-getblack(x1,y1,x2,y2);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n,m,x1,y1,x2,y2,x3,y3,x4,y4;
scanf("%d%d",&n,&m);
long long s1=getwhite(1,1,n,m);
scanf("%d%d%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
int u=max(x1,x3),d=min(x2,x4);
int l=max(y1,y3),r=min(y2,y4);
if(u<=d&&l<=r)
{
s1+=getblack(x1,y1,x2,y2)-getblack(u,l,d,r)-getwhite(x3,y3,x4,y4);
}else
{
s1+=getblack(x1,y1,x2,y2)-getwhite(x3,y3,x4,y4);
}
printf("%lld %lld\n",s1,1ll*n*m-s1);
}
return 0;
}