CF#524-C
题目链接
http://codeforces.com/contest/1080/problem/C
题意
给出一个n*m规格的矩形方块,这个矩形方块初始状态是严格的黑白相间的,现在有两次操作,第一次操作是将一块区域的矩形全部变为白色,第二次操作是将一块区域的矩形覆盖成黑色,要求的是经过两次操作之后白色方块和黑色方块的数量,矩形的区域用两个坐标表示,分别是左下角坐标和右上角坐标。
思路
代码
1 #include<iostream> 2 using namespace std; 3 4 int max(int a, int b) 5 { 6 return a > b ? a : b ; 7 } 8 9 int min(int a, int b) 10 { 11 return a < b ? a : b ; 12 } 13 14 long long getArea(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4) //计算相交面积 15 { 16 if((min(x2, x4) - max(x1, x3) >= 0) && (min(y2, y4) - max(y1, y3) >= 0)) 17 { 18 return (min(x2, x4) - max(x1, x3) + 1) * (min(y2, y4) - max(y1, y3) + 1); 19 } 20 else 21 return 0; 22 } 23 24 struct Area 25 { 26 long long wnum, bnum; 27 }; 28 29 Area getNum(int x1, int y1, int x2, int y2) //求矩形区域内的白、黑数量 30 { 31 long long a = (x2 - x1 + 1); 32 long long b = (y2 - y1 + 1); 33 long long sum = a * b; 34 Area ans; 35 if((x1 % 2 == 0 && y1 % 2 == 0) || (x1 % 2 == 1 && y1 % 2 == 1)) 36 { 37 ans.bnum = sum / 2; 38 ans.wnum = sum - ans.bnum; 39 40 } 41 else 42 { 43 ans.wnum = sum / 2; 44 ans.bnum = sum - ans.wnum; 45 } 46 return ans; 47 } 48 49 int main() 50 { 51 int t; 52 int n, m; 53 int x1, y1, x2, y2, x3, y3, x4, y4; 54 Area first, second, add, ans; 55 cin >> t; 56 while(t--) 57 { 58 cin >> n >> m; 59 ans = getNum(1, 1, m, n); 60 61 cin >> x1 >> y1 >> x2 >> y2; 62 first = getNum(x1, y1, x2, y2); 63 64 cin >> x3 >> y3 >> x4 >> y4; 65 second = getNum(x3, y3, x4, y4); 66 67 if(getArea(x1, y1, x2, y2, x3, y3, x4, y4)) //存在相交 68 { 69 add = getNum(max(x1, x3), max(y1, y3), min(x2, x4), min(y2, y4)); 70 } 71 else 72 { 73 add.wnum = add.bnum = 0; 74 } 75 76 ans.wnum += first.bnum; 77 ans.bnum -= first.bnum; 78 79 /*相交部分:白→白→黑 80 黑→白→黑*/ 81 ans.wnum -= (add.wnum + add.bnum); 82 ans.bnum += (add.wnum + add.bnum); 83 84 ans.wnum -= (second.wnum - add.wnum); 85 ans.bnum += (second.wnum - add.wnum); 86 cout << ans.wnum << " " << ans.bnum << endl; 87 } 88 return 0; 89 }