Codeforces Round #524-C-Masha and two friends

题目链接

看了半天代码才看出怎么做的,然后写了注释

首先白色和黑色不一定相等,这里写了一个求x1y1->x2y2中白色有多少的函数(求黑色用总的减去白色即可)

首先一开始白色有Getwhite-1,1->n,m个,

然后浇上白色的漆,那么x1y1->x2y2中的黑色变成白色,

也就是white += Getblack-x1y1->x2y2

再然后浇上黑色的漆,

如果两次的矩阵没有交叉的话,减去第二个矩阵中的白色(被黑色漆浇上自然不是白色的了)

white-=Getwhite-x3y3->x4y4

如果有交叉的话,再减掉交叉矩阵中的黑色,

(因为那一片中,是白色--->黑色,而上面算的是黑白->黑,自然是少考虑了黑色的)

white -= Getblack-x5y5->x6y6

这样白色的就算出来了,黑色用总数减掉白色就好

求矩阵的交叉部分的矩阵和求白色块的多少是个难点(对于我来说,然而看到代码之后就很简单了)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int t,n,m,x1,x2,x3,x4,y1,y2,y3,y4,x5,x6,y5,y6;
ll white,black;
ll Getwhite(int x1,int y1,int x2,int y2){
    int c = (x1+y1)%2==0;
    if((x2-x1+1)%2==0||(y2-y1+1)%2==0)c = 0;
    return 1ll*(x2-x1+1)*(y2-y1+1)/2+c;
}
ll Getblack(int x1,int y1,int x2,int y2){
    return 1ll*(x2-x1+1)*(y2-y1+1) - Getwhite(x1,y1,x2,y2);
}
int main()
{
    cin>>t;
    while(t--){
        cin>>n>>m;
        cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
        white = Getwhite(1,1,n,m);///at the start
        white += Getblack(x1,y1,x2,y2);///get the white (subtract the black)
        white -= Getwhite(x3,y3,x4,y4);///get the black
        x5 = max(x1,x3),x6 = min(x2,x4);
        y5 = max(y1,y3),y6 = min(y2,y4);
        if(x5<=x6 && y5<=y6)///if exist cross
            white -= Getblack(x5,y5,x6,y6);///subtract the black
        black = 1ll*n*m - white;
        cout<<white<<" "<<black<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Du_Mingm/article/details/84454160