zoukankan      html  css  js  c++  java
  • 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 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 mmfrom 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
    
    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:

    题意:在n*m的矩形中,每次给你两个两个矩形,第一次涂成白色,第二次涂成黑色,

          问最后的白色和黑色色块有多少个?

    题解:从题目中,我们可以知道,n*m的矩形中,白色的个数就是a*b/2+((a*b)&1);

          而黑色就是n*m-白色的个数

          然后怎么求出给定矩形中黑白色的个数呢?我们并不知道其中黑色和白色的个数那个多

          这里有两种方法:

          1、这时你可以根据x+y是奇偶来判断左下的方块是黑白(黑为奇数),就知道哪个多了

          2、根据矩形相加减,即w(x2,y2)-w(x1-1,y2)-w(x2,y1-1)+w(x1-1,y1-1)

          接着如果两个矩形相交了,那么情况又会不一样。重点是我们要求出相交的矩形面积。

          坐标(max(x1,x3),max(y1,y3))和(min(x2,x4),min(y2,y4))这个矩形就是相交的矩形,

               记得要判断一下是否相交!然后在相交矩形中我们黑色少算了里面原来的黑色,白色多算了

               原来的黑色(在第一步中被染成白色后,在第二步我们以为是黑色,忘记染了)

    代码:

    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #include<stdio.h>
    #include<math.h>
    #include<queue>
    #include<stack>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int,int> PII;
    #define mod 1000000007
    #define pb push_back
    #define mk make_pair
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    //head
    ll w(ll a,ll b)
    {
        return a*b/2+((a*b)&1);
    }
    ll _w(ll x1,ll y1,ll x2,ll y2)//白色
    {
        return w(x2,y2)-w(x1-1,y2)-w(x2,y1-1)+w(x1-1,y1-1);
    }
    ll _b(ll x1,ll y1,ll x2,ll y2)//黑色
    {
        return (y2-y1+1)*(x2-x1+1)-_w(x1,y1,x2,y2);
    }
    int main()
    {
        int n,m;
        int x1,x2,x3,x4,y1,y2,y3,y4;
        int t;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d %d",&n,&m);
            scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);
            ll numw=w(n,m);//白色
            ll numb=(ll)n*m-w(n,m);//
            numb-=_b(x1,y1,x2,y2);numw+=_b(x1,y1,x2,y2);
            numb+=_w(x3,y3,x4,y4);numw-=_w(x3,y3,x4,y4);
            int _x1=max(x1,x3),_y1=max(y1,y3);
            int _x2=min(x2,x4),_y2=min(y2,y4);
            if(_x1<=_x2&&_y1<=_y2)
            {
                numb+=_b(_x1,_y1,_x2,_y2);
                numw-=_b(_x1,_y1,_x2,_y2);
            }
            printf("%lld %lld
    ",numw,numb);
        }
        return 0;
    }

                                                                                                      

  • 相关阅读:
    【原创】大数据基础之Hadoop(3)yarn数据收集与监控
    【原创】运维基础之Docker(7)关于docker latest tag
    【原创】大数据基础之ElasticSearch(4)es数据导入过程
    【原创】大叔经验分享(44)hdfs副本数量
    【转】IAR IDE for MSP430、8051、ARM等平台的结合使用
    写驱动的步骤
    【转】IAR for STM8介绍、下载、安装与注册
    KEIL中函数定义存在但go to definition却不跳转的原因
    FatFs
    学习2__STM32--汉字显示
  • 原文地址:https://www.cnblogs.com/zhgyki/p/10073792.html
Copyright © 2011-2022 走看看