题目链接:http://codeforces.com/contest/1080
题意:给你一个 n*m 的矩形区域,每个格子的颜色为黑或者白。首先用白色染料去染某个区域(x1,y1,x2,y2)(某个矩形区域左下角右上角的坐标),再用黑色的染料去染某个区域(x3,y3,x4,y4),黑色的染料可以覆盖白色的染料染过的区域。求最后黑色和白色格子分别为多少个。
思路:先算白色的格子,黑色的格子即为 n*m - 白色的格子数。白色的格子数 = 最开始矩形区域中白色的格子数 + 染白区域中初始黑色的格子数 - 染黑区域初始白色的格子数 - 染白和染黑交叉区域中初始为黑色的格子数。经观察发现:如果区域的格子数为偶数那么黑色的格子和白色的格子各占一半;如果区域的格子数为奇数,区域左下角坐标(x,y)若 x + y 为奇数,则黑色格子比白色格子多一个,反之白色格子比黑色格子多一个。
AC代码:
#include<bits/stdc++.h>
using namespace std;
#define per(i,a,n) for (int i=a;i<n;i++)
#define rep(i,a,n) for (int i=n-1;i>=a;i--)
#define runfile freopen("E:/AllCode/codeblocks project/Root/data.txt", "r", stdin)
#define stopfile fclose(stdin)
#define tt long long
#define MAX 100050
typedef long long ll;
const int maxn = 100;
const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
//area is odd ,lower left x + y is even, white is one more than black.
//area is odd ,lower left x + y is odd, white is one less than black.
int main()
{
//runfile;
ios::sync_with_stdio(false);
ll t,n,m,x1,x2,x3,x4,y1,y2,y3,y4,i1,i2,j1,j2,ans;
cin>>t;
while(t--)
{
cin>>n>>m;
cin>>x1>>y1>>x2>>y2; cin>>x3>>y3>>x4>>y4;
x1--; y1--; x3--; y3--; ans = 0;
ans += n*m/2; //white number of original map
if((n*m) % 2 == 1) ans++;
ans += (x2-x1)*(y2-y1)/2 ;//number of black in white painted areas
if(((x2-x1)*(y2-y1) % 2 == 1) && ((x1 + y1) % 2 == 1)) ans++;
ans -= (x4-x3)*(y4-y3)/2;//number of white in black painted areas
if(((x4-x3)*(y4-y3) % 2 == 1) && ((x3 + y3) % 2 == 0)) ans--;
//(i1,j1,i2,j2) intersecting parts of white and black
i1 = max(x1,x3); j1 = max(y1,y3);
i2 = min(x2,x4); j2 = min(y2,y4);
if((i2-i1) >= 0 && (j2-j1) >= 0)
{
ans -= (i2-i1)*(j2-j1)/2;//number of black in intersecting parts
if(((i2-i1)*(j2-j1) % 2 == 1) && ((i1+j1) % 2 == 1)) ans--;
}
cout<<ans<<" "<<n*m-ans<<endl;
}
//stopfile;
return 0;
}