Masha and two friends(容斥+思维)

                                     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 n and a width of m

The rows on the chessboard are numbered from 1 to n from bottom to top. The columns are numbered from 1 to m from left to right. Therefore, each cell can be specified with the coordinates (x,y), where x is the column number, and y is the row number (do not mix up).

Let us call a rectangle with coordinates (a,b,c,d) a rectangle lower left point of which has coordinates (a,b) , and the upper right one — (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) . Then after him Denis spilled black paint on the rectangle (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 (1≤t≤1031≤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 (1≤n,m≤10^9 ) — the size of the board.

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

The third line contains four integers x3 , y3 , x4 , y4 (1≤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.

Input

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

0 4
3 9
2 3
8 8
4 8

Note

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:

题意:

给你一个n*m的棋盘,最初(1,1)上为白色,而且每个相邻的块颜色都不同。
之后有两次操作,
第一次操作给出x1,y2,x2,y2
将(x1,y1,x2,y2)这个矩形涂为白色
第二次操作给出x3,y3,x4,y4
将(x3,y3,x4,y4)这个矩形涂为黑色
后涂得会覆盖之前的颜色。问最终的棋盘上黑色和白色的个数

题解:

1.先调用函数求出所给区域中白色快的数量

2.未考虑重合部分白色块的数量=总的白色块数量+(第一次选定区域的整个部分-第一次选定区域中的白色部分)(因为全部被染成白色了)-第二次选定区域中的白色部分(因为被染成黑色了)

3.考虑重合部分

一共有五种可能,其中能重合的只有四种,画画图就会发现关系

 xx1=max(x1,x3);
 xx2=min(x2,x4);
 yy1=max(y1,y3);
 yy2=min(y2,y4);

要想保证重合需要满足(xx1<=xx2 && yy1<=yy2) 

答案=未考虑重合部分白色块的数量-整个重叠部分+重叠部分中白色的部分

 

注意:这个题有个坑点,对于初始状态白色块与黑色快的数量不相同 如下图所示

 

小技巧: ans=((x2-x1+1)*(y2-y1+1)+1)/2; 向上取整的操作 cile(m/n)=(m+n-1)/n;

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <cstring>
#include <string>
#include <queue>
#include <deque>
#include <stack>
#include <stdlib.h>
#include <list>
#include <map>
#include <utility>
#include <set>
#include <bitset>
#include <vector>
#define pi acos(-1.0)
#define inf 0x3f3f3f3f
#define linf 0x3f3f3f3f3f3f3f3fLL
#define ms(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
int min3(int a,int b,int c){return min(min(a,b),c);}
int max3(int a,int b,int c){return max(max(a,b),c);}
int gcd(int x, int y){if(y==0)return x;return gcd(y, x%y);}
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();return x*f;};

ll ws(ll x1,ll y1,ll x2,ll y2)///返回白色的数量
{
    ll ans=0;
    if((x2-x1+1)%2==0 || (y2-y1+1)%2==0)
        ans=(x2-x1+1)*(y2-y1+1)/2;
    else if((x1+y1)%2)/// 表示左下角(x1,y1)为黑色的
        ans=(x2-x1+1)*(y2-y1+1)/2;
    else ans=(x2-x1+1)*(y2-y1+1) - (x2-x1+1)*(y2-y1+1)/2;  ///白的=总的-黑的;
    /// ans=((x2-x1+1)*(y2-y1+1)+1)/2; 向上取整的操作 cile(m/n)=(m+n-1)/n;
    return ans;

}

int main()
{
    int t;
    ll n,m;
    ll x1,y1,x2,y2;
    ll x3,y3,x4,y4;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&m);
        scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);
        scanf("%lld%lld%lld%lld",&x3,&y3,&x4,&y4);

        ///应用容斥原理
        ll ans1;
        ans1=ws(1,1,n,m)+((x2-x1+1)*(y2-y1+1)-ws(x1,y1,x2,y2))-ws(x3,y3,x4,y4);


        ///白色=总的白色+(第一次选定区域的整个部分-第一次选定区域中的白色部分)(因为全部被染成白色了)-第二次选定区域中的白色部分(因为被染成黑色了)这是未考虑重合部分

        ///考虑重合部分
        ll xx1=max(x1,x3);
        ll xx2=min(x2,x4);
        ll yy1=max(y1,y3);
        ll yy2=min(y2,y4);


        if(xx1<=xx2 && yy1<=yy2)
        {
            ans1-=(xx2-xx1+1)*(yy2-yy1+1);///减去整个重叠部分+重叠部分中白色的部分
            ans1+=ws(xx1,yy1,xx2,yy2);
        }
        ll ans2=n*m-ans1;

        printf("%lld %lld\n",ans1,ans2);

    }
	return 0;

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值