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 mm from 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 (1≤t≤1031≤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 (1≤n,m≤1091≤n,m≤109) — the size of the board.

The second line contains four integers x1x1, y1y1, x2x2, y2y2 (1≤x1≤x2≤m,1≤y1≤y2≤n1≤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 (1≤x3≤x4≤m,1≤y3≤y4≤n1≤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
  1. 5
  2. 2 2
  3. 1 1 2 2
  4. 1 1 2 2
  5. 3 4
  6. 2 2 3 2
  7. 3 1 4 3
  8. 1 5
  9. 1 1 5 1
  10. 3 1 5 1
  11. 4 4
  12. 1 1 4 2
  13. 1 3 4 4
  14. 3 4
  15. 1 2 4 2
  16. 2 1 3 3
output

Copy
  1. 0 4
  2. 3 9
  3. 2 3
  4. 8 8
  5. 4 8

题目链接:http://codeforces.com/contest/1080/problem/C

题目大意:给定一个n x m大小的由黑白方格组成的矩阵,左下角(1,1)处为白色方格,每个相邻方格颜色都不一样。再给你两个矩形的左下角和右上角坐标,把第一个的矩形内的所有方格染成白色,第二个矩形内的所有方格染成黑色。输出最后矩阵中有多少个白色方格和黑色方格。

思路:
这道题n和m都比较大,所以我们找一下排列规律,我们发现,对于给定的矩形,首先判断左下角的颜色是黑色还是白色,可以用左下角的坐标(x,y)中 (x+y) mod 2 的值来判断。
然后判断给定矩形内的方块个数,如果是奇数,则整个矩形内左下角颜色个数等于矩形所有方块数除以2加1,否则黑白各占一半。
由以上两点规律我们可以先分别处理整个矩阵的黑白颜色,再处理第一次染成白色时的矩形,再处理第二次染成黑色时的矩形。但是注意题目难点在于,给定的两个矩形可能是相交的。我们最后要特殊处理一下相交区域,因为相交区域的存在使得我们少加了一些黑色的格子,相应的多加了一些白色的格子。
判断两个矩阵相交,由于给的两个矩形都给的是左下角和右上角的坐标(第一个矩形:(x1,y1),(x2,y2) 和 第二个矩形:(x3,y3),(x4,y4))。所以我们根据这个来判断,我们设

n1=min(max(x1,x2),max(x3,x4));
m1=min(max(y1,y2),max(y3,y4));
n2=max(min(x1,x2),min(x3,x4));
m2=max(min(y1,y2),min(y3,y4));

然后判断如果 n1>=n2 && m1>=m2 ,则两个矩形相交。

AC代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<cmath>
  5. #include<algorithm>
  6. #include<string>
  7. #include<queue>
  8. #include<vector>
  9. #include<set>
  10. #include<map>
  11. #define closeio std::ios::sync_with_stdio(false)
  12. using namespace std;
  13. typedef long long ll;
  14. const int maxn=;
  15. ll n,m,x1,y1,x2,y2,x3,y3,x4,y4;
  16. int main()
  17. {
  18. int t;
  19. cin>>t;
  20. while(t--)
  21. {
  22. scanf("%I64d%I64d",&n,&m);
  23. scanf("%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2);
  24. scanf("%I64d%I64d%I64d%I64d",&x3,&y3,&x4,&y4);
  25. ll ans1 = , ans2 = ;//ans1白色个数,ans2是黑色个数
  26. if(n% == && m% == )
  27. {
  28. ans1 = n*m/+;
  29. ans2 = n*m/;
  30. }
  31. else
  32. {
  33. ans1 = ans2 = n*m/;
  34. }
  35.  
  36. if((x1+y1)%==)//左下角为白色
  37. {
  38. ans2 -= (x2-x1+)*(y2-y1+)/;
  39. ans1 += (x2-x1+)*(y2-y1+)/;
  40. }
  41. else//左下角为黑色
  42. {
  43. if((x2-x1+)% == && (y2-y1+)% == )
  44. {
  45. ans2 -= ((x2-x1+)*(y2-y1+)/+);
  46. ans1 += ((x2-x1+)*(y2-y1+)/+);
  47. }
  48. else
  49. {
  50. ans2 -= (x2-x1+)*(y2-y1+)/;
  51. ans1 += (x2-x1+)*(y2-y1+)/;
  52. }
  53. }
  54. if((x3+y3)%==)//左下角为白色
  55. {
  56. if((x4-x3+)% == && (y4-y3+)% == )
  57. {
  58. ans2 += ((x4-x3+)*(y4-y3+)/+);
  59. ans1 -= ((x4-x3+)*(y4-y3+)/+);
  60. }
  61. else
  62. {
  63. ans2 += (x4-x3+)*(y4-y3+)/;
  64. ans1 -= (x4-x3+)*(y4-y3+)/;
  65. }
  66. }
  67. else//左下角为黑色
  68. {
  69. ans2 += ((x4-x3+)*(y4-y3+)/);
  70. ans1 -= ((x4-x3+)*(y4-y3+)/);
  71. }
  72. //处理相交
  73. ll n1,n2,m1,m2;
  74. n1=min(max(x1,x2),max(x3,x4));
  75. m1=min(max(y1,y2),max(y3,y4));
  76. n2=max(min(x1,x2),min(x3,x4));
  77. m2=max(min(y1,y2),min(y3,y4));
  78. if(n1>=n2&&m1>=m2)
  79. {
  80. swap(n1,n2);//这里需交换使后续计算为正值
  81. swap(m1,m2);
  82.  
  83. if((n1+m1)%==)//左下角为白色
  84. {
  85. ans2 += (n2-n1+)*(m2-m1+)/;
  86. ans1 -= (n2-n1+)*(m2-m1+)/;
  87. }
  88. else//左下角为黑色
  89. {
  90. if((n2-n1+)% == && (m2-m1+)% == )
  91. {
  92. ans2 += ((n2-n1+)*(m2-m1+)/+);
  93. ans1 -= ((n2-n1+)*(m2-m1+)/+);
  94. }
  95. else
  96. {
  97. ans2 += (n2-n1+)*(m2-m1+)/;
  98. ans1 -= (n2-n1+)*(m2-m1+)/;
  99. }
  100. }
  101. }
  102. cout<<ans1<<' '<<ans2<<endl;
  103. }
  104. return ;
  105. }

Codeforces Round #524 (Div. 2) C. Masha and two friends(矩形相交)的更多相关文章

  1. Codeforces Round #524 (Div. 2) C. Masha and two friends

    C. Masha and two friends 题目链接:https://codeforc.es/contest/1080/problem/C 题意: 给出一个黑白相间的n*m的矩阵,现在先对一个子 ...

  2. Codeforces Round #524 (Div. 2) C. Masha and two friends(思维+计算几何?)

    传送门 https://www.cnblogs.com/violet-acmer/p/10146350.html 题意: 有一块 n*m 的棋盘,初始,黑白块相间排列,且左下角为白块. 给出两个区间[ ...

  3. Codeforces Round #524 (Div. 2) C. Masha and two friends 几何:判断矩形是否相交以及相交矩形坐标

    题意 :给出一个初始的黑白相间的棋盘  有两个人  第一个人先用白色染一块矩形区域 第二个人再用黑色染一块矩形区域 问最后黑白格子各有多少个 思路:这题的关键在于求相交的矩形区间 给出一个矩形的左下和 ...

  4. Codeforces Round #524 (Div. 2) C. Masha and two friends 思路

    题目:题目链接 思路:直接计数显然是不好处理的,但分情况讨论只要不写错这题是一定可以出的,但这样基本做完这个题就没时间做其他题了,但当时我就这么蠢的这样做了,比赛一个半小时的时候突然发现一个似乎可行的 ...

  5. Codeforces Round #524 (Div. 2)(前三题题解)

    这场比赛手速场+数学场,像我这样读题都读不大懂的蒟蒻表示呵呵呵. 第四题搞了半天,大概想出来了,但来不及(中途家里网炸了)查错,于是我交了两次丢了100分.幸亏这次没有掉rating. 比赛传送门:h ...

  6. Codeforces Round #524 (Div. 2) Masha and two friends矩形

    题目 题意:    给一个n*m块大的黑白相间的矩形,在这个举行中操作,要先把第一个矩形(左下角坐标(x1,y2),右上角坐标(x2,y2)) 全部涂成白色,再把第二个矩形(左下角坐标(x3,y3), ...

  7. Codeforces Round #524 (Div. 2) F. Katya and Segments Sets(主席树)

    https://codeforces.com/contest/1080/problem/F 题意 有k个区间,区间的种类有n种,有m个询问(n,m<=1e5,k<=3e5),每次询问a,b ...

  8. Codeforces Round #524 (Div. 2) E. Sonya and Matrix Beauty(字符串哈希,马拉车)

    https://codeforces.com/contest/1080/problem/E 题意 有一个n*m(<=250)的字符矩阵,对于每个子矩阵的每一行可以任意交换字符的顺序,使得每一行每 ...

  9. Codeforces Round #524 (Div. 2) Solution

    A. Petya and Origami Water. #include <bits/stdc++.h> using namespace std; #define ll long long ...

随机推荐

  1. 【bzoj5016】[Snoi2017]一个简单的询问 莫队算法

    题目描述 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计算区间[l,r]中,数字x出现了多少次. 输入 第一行,一个数字N,表 ...

  2. 【洛谷】【动态规划+单调队列】P1725 琪露诺

    [题目描述:] 在幻想乡,琪露诺是以笨蛋闻名的冰之妖精. 某一天,琪露诺又在玩速冻青蛙,就是用冰把青蛙瞬间冻起来.但是这只青蛙比以往的要聪明许多,在琪露诺来之前就已经跑到了河的对岸.于是琪露诺决定到河 ...

  3. etcd 删除

    vim /etc/sysconfig/flanneld FLANNEL_ETCD_ENDPOINTS="https://192.168.30.241:2379,https://192.168 ...

  4. List集合和iterator并发异常处理

    一:List接口: 子类:ArrayList   LinkedList 特点:Unlike sets, lists typically allow duplicate elements.不像set集合 ...

  5. kaggle _Titanic: Machine Learning from Disaster

    A Data Science Framework: To Achieve 99% Accuracy https://www.kaggle.com/ldfreeman3/a-data-science-f ...

  6. Spring源码分析(十五)获取单例

    本文结合<Spring源码深度解析>来分析Spring 5.0.6版本的源代码.若有描述错误之处,欢迎指正. 之前我们讲解了从缓存中获取单例的过程,那么,如果缓存中不存在已经加载的单例be ...

  7. helm-locate 使用 everything

    emacs里不需任何设置,只要在电脑的环境变量里加上everything的目录即可. 1.下载es.exe (http://www.voidtools.com/es.zip) 2.把解压出的es.ex ...

  8. weex中UISegmentControl实现及遇到的问题

    在最近主导的一个项目中,App端的实现使用了weex.通过近一个月的实践,我们发现如果对于人机交互较少的App,即使较少前端经验的人也能迅速进入开发(当然需要一定时间 才能上手weex).在开发的时候 ...

  9. centos配置静态ip地址

    1.输入以下命令: vim /etc/sysconfig/network-scripts/ifcfg-eth0 2.注释掉BOOTPROTO=dhcp 3.添加如下内容: ONBOOT=yes 表示开 ...

  10. 在Linux中连接android设备

    1.用usb线连接电脑和android设备,在命令行中输入lsusb可查看采用usb连接到电脑的设备 找到设备的vendor ID. 如上图: "Bus 002 Device 007: ID ...