CodeForces Round#524C -Masha and two friends (区间分割)

版权声明:欢迎评论与转载,转载时请注明出处! https://blog.csdn.net/wjl_zyl_1314/article/details/84504657

原题链接:
http://codeforces.com/contest/1080/problem/C
C. Masha and two friends
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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:
在这里插入图片描述
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 color onto a certain rectangle means that all the cells that belong to the given rectangle become 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!

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

Each of them is described in the following format:

The first line contains two integers n and m (1≤n,m≤109) — 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 t lines, each of which contains two numbers — the number of white and black cells after spilling paint, respectively.

Example
inputCopy
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
outputCopy
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:
在这里插入图片描述
题意:
有一个棋盘,现在先在一个矩形内全部涂成白色,后又在另一块矩形区域里涂成黑色,问现在有多少个白色快和黑色块。
题解:
这道题目直接算会超出数据范围,所以要将矩形分块,对于每一块分别进行讨论。
附上AC代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define LL long long
LL n,m;
LL answ,ansb;
struct rec//定义矩形
{
    LL x1,y1,x2,y2;
    rec(){}
    rec(LL _x1,LL _y1,LL _x2,LL _y2)
    {
        x1=_x1;
        y1=_y1;
        x2=_x2;
        y2=_y2;
    }
}w,b,r[25];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&n,&m);
        scanf("%lld%lld%lld%lld",&w.x1,&w.y1,&w.x2,&w.y2);
        scanf("%lld%lld%lld%lld",&b.x1,&b.y1,&b.x2,&b.y2);
        w.x1--,w.y1--,b.x1--,b.y1--;//由于是区间内部,所以左下角的坐标要减一
        LL x[6],y[6];
        x[0]=0,x[1]=w.x1,x[2]=w.x2,x[3]=b.x1,x[4]=b.x2,x[5]=m;
        y[0]=0,y[1]=w.y1,y[2]=w.y2,y[3]=b.y1,y[4]=b.y2,y[5]=n;
        sort(x,x+6);
        sort(y,y+6);//从大到小,保证矩形的第一个点是在第二个顶点的左下角的
        for(int i=0;i<5;i++)//图形分块
            for(int j=0;j<5;j++)
        {
            r[i*5+j]=rec(x[i],y[j],x[i+1],y[j+1]);
        }
        answ=0;
        ansb=0;//重置
        for(int i=0;i<25;i++)
        {
            LL x1=r[i].x1;
            LL y1=r[i].y1;
            LL x2=r[i].x2;
            LL y2=r[i].y2;
            if(x1>=b.x1&&y1>=b.y1&&x2<=b.x2&&y2<=b.y2)//在黑色区域
            {
                ansb+=(x2-x1)*(y2-y1);
            }
            else if(x1>=w.x1&&y1>=w.y1&&x2<=w.x2&&y2<=w.y2)//在白色区域
            {
                answ+=(x2-x1)*(y2-y1);
            }
            else//在未被涂色区域
            {
                LL tmp=(x2-x1)*(y2-y1);
                if((x1+y1)&1)//如果左下角的格子是黑色
                {
                    ansb+=(tmp+1)>>1;
                    answ+=tmp>>1;
                }
                else//如果左下角的格子是白色
                {
                    answ+=(tmp+1)>>1;
                    ansb+=tmp>>1;
                }
            }
        }
        printf("%lld %lld\n",answ,ansb);
    }
    return 0;
}

欢迎评论!

猜你喜欢

转载自blog.csdn.net/wjl_zyl_1314/article/details/84504657