1. 程式人生 > >Masha and two friends(容斥+思維)

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;

}

相關推薦

Masha and two friends(+思維)

                                     Masha and two friends                                                                            

Codeforces Round #524 (Div. 2)C. Masha and two friends(定理)

傳送門 題意:先給出一個n行m列的矩陣,這個矩陣是黑白交錯的,左下角是白的,然後再給出一個矩陣,這個矩陣就全部染成白色的,之後再給出一個矩陣,這個矩陣染成黑色的,資料範圍是1e9,問最後白色塊和黑色塊分別由多少? 題解:首先可以算出這個大矩陣的黑色塊和白色塊分別有多少個,然後我們算下要染成白

+矩陣面積並】Masha and two friends

題目連結 題意: 題目很長,但是其實題意非常簡單,首先是給定一個n*m的方格,然後黑白相間,其中左下角是白色。 然後有兩個人愛塗色,然後A先塗色把他所選中的地方塗成白色,然後B把他選中的地方塗成黑色。 然後問最後的情況白色格子有多少,黑色格子有多少? 小結: 其實這個題以

Codeforces Round #524 (Div. 2) Masha and two friends

題目連結:C. Masha and two friends   一個矩形,黑白塊按規律分佈。A同學選定一個矩形區域,將該區域全塗改成白色;B同學後選定一個區域,將該區域全塗改成黑色。那麼結束後,白塊、黑快的數量是多少。   一個矩形兩個點,左下和右上。有一個規律:如果左下點是白色,那麼以該點為左下的矩形中

codeforces round#524 C. Masha and two friends /// 矩形切割

題目大意: 給定n行m列的黑白棋盤如下   給定矩形的左下點x1 y1和右上點x2 y2將這個區域都塗成白色 再給定矩形的左下點x3 y3和右上點x4 y4將這個區域都塗成黑色 求最後棋盤內有分別多少個白格和黑格   本來不怎麼想發CF賽時AC的題 但是這

CodeForce 524 Div 2 C. Masha and two friends

  題目連結: http://codeforces.com/contest/1080/problem/C 思路:雙向延長兩個矩形方塊的4邊,會形成一個被分割為9塊的更大立方體。 計算所有的9個方框。方框中心的點只有可能在黑框,白框或者在外面。中心點在黑框中則按照黑色渲染了

Codeforces Round #524 (Div. 2) C. Masha and two friends 思路

題目:題目連結 思路:直接計數顯然是不好處理的,但分情況討論只要不寫錯這題是一定可以出的,但這樣基本做完這個題就沒時間做其他題了,但當時我就這麼蠢的這樣做了,比賽一個半小時的時候突然發現一個似乎可行的規律,但因為時間問題沒有證,當時那個思路已經快寫完了也沒有換思路寫,就杯具了,最後那個寫了一坨的程式碼耗了我

Codeforces Round #524 (Div. 2) C. Masha and two friends

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

(模擬)cf#514.C.Masha and two friends

https://codeforces.com/contest/1080/problem/C 比賽的時候手寫相交判斷寫過樣例就交了,發現wa了,然後去網上抄了個板子再交還是wa。自閉 。然後比賽完看錯誤原因發現n,m沒開long long。但是還是發現板子要wa在第39個樣例,手寫

Codeforces Round #524 (Div. 2) Masha and two friends矩形

  題目   題意:      給一個n*m塊大的黑白相間的矩形,在這個舉行中操作,要先把第一個矩形(左下角座標(x1,y2),右上角座標(x2,y2)) 全部塗成白色,再把第二個矩形(左下角座標(x3,y3),右上角座標(x4,y4))  全部塗成黑

Codeforces Round #524-C-Masha and two friends

  題目連結         看了半天程式碼才看出怎麼做的,然後寫了註釋 首先白色和黑色不一定相等,這裡寫了一個求x1y1->x2y2中白色有多少的函式(求黑色用總的減去白色即可)

CF1080C Masha and two friends (矩陣的重疊面積)

---恢復內容開始--- CF1080C Masha and two friends (矩陣的重疊面積) 題目連結:CF1080C 有關在座標軸內矩陣的重疊覆蓋,格點計算都是毒瘤題,但見到了還是要A的 這類題目一般會有一些化繁為簡的技巧 這道題,是有關黑白格點的計算,我們可以利用容斥來做 首先我們

C. Masha and two friends

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&n

CF1080C Masha and two friends

題目:Masha and two friends 思路: 首先這題思路很簡單,模擬+容斥就可以了。 即 最終的白塊數 = 原網格圖的白塊數 + 塗白增加的白塊數 - 塗黑減少的白塊數 - 又塗了白又塗了黑的那一塊減少的白塊數。 這裡重點說下怎麼求兩矩形交,我最開始就

Masha and two friends

題目地址 題解:題意不是很難,對於一個有規律分佈的黑白棋盤,給你兩個操作,第一個操作是一個矩形內全部塗成白,第二個就是全部塗成黑。可以有重疊,問最後的白黑塊有多少個。 黑白棋盤的規律在於(i+j)是偶數就是白,是奇數就是黑。 顯然我們可以單獨處理出矩形內所有的白和黑,預

Codeforces 451E Devu and Flowers【原理+盧卡斯定理】

d+ 題意 while markdown post mark 色相 esp printf 題意:每個箱子裏有\( f[i] \)種顏色相同的花,現在要取出\( s \)朵花,問一共有多少種顏色組合 首先枚舉\( 2^n \)種不滿足條件的情況,對於一個不被滿足的盒子,我們至

Codeforces 483B - Friends and Presents(二分+

con pan const c++ logs () get com end 483B - Friends and Presents 思路:這個博客寫的不錯:http://www.cnblogs.com/windysai/p/4058235.html 代碼: #includ

Codeforces483B. Friends and Presents(二分+原理)

題目連結:傳送門 題目: B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input standard input output standard out

Devu and Flowers lucas定理+原理

原理 容斥原理 title pac cond rst like with lld Devu wants to decorate his garden with flowers. He has purchased n boxes, where the i-th box con

Masha and Bears(翻譯+思維)

ould time ted climb 爸爸 his pac guarantee get Description A family consisting of father bear, mother bear and son bear owns thr