C. Masha and two friends
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Recently, Masha was presented with a chessboard with a height of nn and a width of mm.

The rows on the chessboard are numbered from 11 to nn from bottom to top. The columns are numbered from 11 to mm from left to right. Therefore, each cell can be specified with the coordinates (x,y)(x,y), where xx is the column number, and yy 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 (x1,y1,x2,y2)(x1,y1,x2,y2). Then after him Denis spilled black paint on the rectangle (x3,y3,x4,y4)(x3,y3,x4,y4).

To spill paint of color colorcolor onto a certain rectangle means that all the cells that belong to the given rectangle become colorcolor. 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!

Input

The first line contains a single integer tt (1t1031≤t≤103) — the number of test cases.

Each of them is described in the following format:

The first line contains two integers nn and mm (1n,m1091≤n,m≤109) — the size of the board.

The second line contains four integers x1x1, y1y1, x2x2, y2y2 (1x1x2m,1y1y2n1≤x1≤x2≤m,1≤y1≤y2≤n) — the coordinates of the rectangle, the white paint was spilled on.

The third line contains four integers x3x3, y3y3, x4x4, y4y4 (1x3x4m,1y3y4n1≤x3≤x4≤m,1≤y3≤y4≤n) — the coordinates of the rectangle, the black paint was spilled on.

Output

Output tt lines, each of which contains two numbers — the number of white and black cells after spilling paint, respectively.

Example
input
Copy
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
output
Copy
0 4
3 9
2 3
8 8
4 8

 

题目链接:http://codeforces.com/contest/1080/problem/C

题目大意:给定一个n x m大小的由黑白方格组成的矩阵,左下角(1,1)处为白色方格,每个相邻方格颜色都不一样。再给你两个矩形的左下角和右上角坐标,把第一个的矩形内的所有方格染成白色,第二个矩形内的所有方格染成黑色。输出最后矩阵中有多少个白色方格和黑色方格。

思路:
这道题n和m都比较大,所以我们找一下排列规律,我们发现,对于给定的矩形,首先判断左下角的颜色是黑色还是白色,可以用左下角的坐标(x,y)中 (x+y) mod 2 的值来判断。
然后判断给定矩形内的方块个数,如果是奇数,则整个矩形内左下角颜色个数等于矩形所有方块数除以2加1,否则黑白各占一半。
由以上两点规律我们可以先分别处理整个矩阵的黑白颜色,再处理第一次染成白色时的矩形,再处理第二次染成黑色时的矩形。但是注意题目难点在于,给定的两个矩形可能是相交的。我们最后要特殊处理一下相交区域,因为相交区域的存在使得我们少加了一些黑色的格子,相应的多加了一些白色的格子。
判断两个矩阵相交,由于给的两个矩形都给的是左下角和右上角的坐标(第一个矩形:(x1,y1),(x2,y2) 和 第二个矩形:(x3,y3),(x4,y4))。所以我们根据这个来判断,我们设

n1=min(max(x1,x2),max(x3,x4));
m1=min(max(y1,y2),max(y3,y4));
n2=max(min(x1,x2),min(x3,x4));
m2=max(min(y1,y2),min(y3,y4));

然后判断如果 n1>=n2 && m1>=m2 ,则两个矩形相交。

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<map>
#define closeio std::ios::sync_with_stdio(false)
using namespace std;
typedef long long ll;
const int maxn=10;
ll n,m,x1,y1,x2,y2,x3,y3,x4,y4;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        scanf("%I64d%I64d",&n,&m);
        scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2);
        scanf("%I64d%I64d%I64d%I64d",&x3,&y3,&x4,&y4);
        ll ans1 = 0, ans2 = 0;//ans1白色个数,ans2是黑色个数 
        if(n%2 == 1 && m%2 == 1)
        {
            ans1 = n*m/2+1;
            ans2 = n*m/2;
        }
        else
        {
            ans1 = ans2 = n*m/2;
        }
        
        if((x1+y1)%2==0)//左下角为白色
        {
            ans2 -= (x2-x1+1)*(y2-y1+1)/2;
            ans1 += (x2-x1+1)*(y2-y1+1)/2;
        }
        else//左下角为黑色
        {
            if((x2-x1+1)%2 == 1 && (y2-y1+1)%2 == 1)
            {
                ans2 -= ((x2-x1+1)*(y2-y1+1)/2+1);
                ans1 += ((x2-x1+1)*(y2-y1+1)/2+1);
            }
            else
            {
                ans2 -= (x2-x1+1)*(y2-y1+1)/2;
                ans1 += (x2-x1+1)*(y2-y1+1)/2;
            }
        }
        if((x3+y3)%2==0)//左下角为白色
        {
            if((x4-x3+1)%2 == 1 && (y4-y3+1)%2 == 1)
            {
                ans2 += ((x4-x3+1)*(y4-y3+1)/2+1);
                ans1 -= ((x4-x3+1)*(y4-y3+1)/2+1);
            }
            else
            {
                ans2 += (x4-x3+1)*(y4-y3+1)/2;
                ans1 -= (x4-x3+1)*(y4-y3+1)/2;
            }
        }
        else//左下角为黑色
        {
            ans2 += ((x4-x3+1)*(y4-y3+1)/2);
            ans1 -= ((x4-x3+1)*(y4-y3+1)/2);
        }
        //处理相交
        ll n1,n2,m1,m2;
        n1=min(max(x1,x2),max(x3,x4));
        m1=min(max(y1,y2),max(y3,y4));
        n2=max(min(x1,x2),min(x3,x4));
        m2=max(min(y1,y2),min(y3,y4));    
        if(n1>=n2&&m1>=m2)
        {
            swap(n1,n2);//这里需交换使后续计算为正值 
            swap(m1,m2);
                
            if((n1+m1)%2==0)//左下角为白色
            {
                ans2 += (n2-n1+1)*(m2-m1+1)/2;
                ans1 -= (n2-n1+1)*(m2-m1+1)/2;
            }
            else//左下角为黑色
            {
                if((n2-n1+1)%2 == 1 && (m2-m1+1)%2 == 1)
                {
                    ans2 += ((n2-n1+1)*(m2-m1+1)/2+1);
                    ans1 -= ((n2-n1+1)*(m2-m1+1)/2+1);
                }
                else
                {
                    ans2 += (n2-n1+1)*(m2-m1+1)/2;
                    ans1 -= (n2-n1+1)*(m2-m1+1)/2;
                }    
            }
        }
        cout<<ans1<<' '<<ans2<<endl;
    }
    return 0;
}

 

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!