热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

CodeforcesRound#524(Div.2)CMashaandtwofriends(矩形相交)

题目传送门C.Mashaandtwofriendstimelimitpertest1secondmemorylimitpertest256megabytesinputstandar

题目传送门

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 mmfrom 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 (1t1031≤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 (1n,m1091≤n,m≤109) — the size of the board.

The second line contains four integers x1x1, y1y1, x2x2, y2y2 (1x1x2m,1y1y2n1≤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 (1x3x4m,1y3y4n1≤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

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

output
Copy

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:

题意:在n*m的矩形中,每次给你两个两个矩形,第一次涂成白色,第二次涂成黑色,

      问最后的白色和黑色色块有多少个?

题解:从题目中,我们可以知道,n*m的矩形中,白色的个数就是a*b/2+((a*b)&1);

      而黑色就是n*m-白色的个数

      然后怎么求出给定矩形中黑白色的个数呢?我们并不知道其中黑色和白色的个数那个多

      这里有两种方法:

      1、这时你可以根据x+y是奇偶来判断左下的方块是黑白(黑为奇数),就知道哪个多了

      2、根据矩形相加减,即w(x2,y2)-w(x1-1,y2)-w(x2,y1-1)+w(x1-1,y1-1)

      接着如果两个矩形相交了,那么情况又会不一样。重点是我们要求出相交的矩形面积。

      坐标(max(x1,x3),max(y1,y3))和(min(x2,x4),min(y2,y4))这个矩形就是相交的矩形,

           记得要判断一下是否相交!然后在相交矩形中我们黑色少算了里面原来的黑色,白色多算了

           原来的黑色(在第一步中被染成白色后,在第二步我们以为是黑色,忘记染了)

代码:

 

#include
#include
<string.h>
#include

#include

#include

#include

#include

using namespace std;
typedef
long long ll;
typedef unsigned
long long ull;
typedef
long long ll;
typedef pair
<int,int> PII;
#define mod 1000000007
#define pb push_back
#define mk make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
//head
ll w(ll a,ll b)
{
return a*b/2&#43;((a*b)&1);
}
ll _w(ll x1,ll y1,ll x2,ll y2)
//白色
{return w(x2,y2)-w(x1-1,y2)-w(x2,y1-1)&#43;w(x1-1,y1-1);
}
ll _b(ll x1,ll y1,ll x2,ll y2)
//黑色
{return (y2-y1&#43;1)*(x2-x1&#43;1)-_w(x1,y1,x2,y2);
}
int main()
{
int n,m;int x1,x2,x3,x4,y1,y2,y3,y4;int t;scanf("%d",&t);while(t--){scanf("%d %d",&n,&m);scanf("%d %d %d %d %d %d %d %d",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4);ll numw&#61;w(n,m);//白色ll numb&#61;(ll)n*m-w(n,m);//
numb-&#61;_b(x1,y1,x2,y2);numw&#43;&#61;_b(x1,y1,x2,y2);numb&#43;&#61;_w(x3,y3,x4,y4);numw-&#61;_w(x3,y3,x4,y4);int _x1&#61;max(x1,x3),_y1&#61;max(y1,y3);int _x2&#61;min(x2,x4),_y2&#61;min(y2,y4);if(_x1<&#61;_x2&&_y1<&#61;_y2){numb&#43;&#61;_b(_x1,_y1,_x2,_y2);numw-&#61;_b(_x1,_y1,_x2,_y2);}printf("%lld %lld\n",numw,numb);}return 0;
}

 

 

 

                                                                                                  


转:https://www.cnblogs.com/zhgyki/p/10073792.html



推荐阅读
  • 本文讨论了使用差分约束系统求解House Man跳跃问题的思路与方法。给定一组不同高度,要求从最低点跳跃到最高点,每次跳跃的距离不超过D,并且不能改变给定的顺序。通过建立差分约束系统,将问题转化为图的建立和查询距离的问题。文章详细介绍了建立约束条件的方法,并使用SPFA算法判环并输出结果。同时还讨论了建边方向和跳跃顺序的关系。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • 李逍遥寻找仙药的迷阵之旅
    本文讲述了少年李逍遥为了救治婶婶的病情,前往仙灵岛寻找仙药的故事。他需要穿越一个由M×N个方格组成的迷阵,有些方格内有怪物,有些方格是安全的。李逍遥需要避开有怪物的方格,并经过最少的方格,找到仙药。在寻找的过程中,他还会遇到神秘人物。本文提供了一个迷阵样例及李逍遥找到仙药的路线。 ... [详细]
  • 本文介绍了Codeforces Round #321 (Div. 2)比赛中的问题Kefa and Dishes,通过状压和spfa算法解决了这个问题。给定一个有向图,求在不超过m步的情况下,能获得的最大权值和。点不能重复走。文章详细介绍了问题的题意、解题思路和代码实现。 ... [详细]
  • STL迭代器的种类及其功能介绍
    本文介绍了标准模板库(STL)定义的五种迭代器的种类和功能。通过图表展示了这几种迭代器之间的关系,并详细描述了各个迭代器的功能和使用方法。其中,输入迭代器用于从容器中读取元素,输出迭代器用于向容器中写入元素,正向迭代器是输入迭代器和输出迭代器的组合。本文的目的是帮助读者更好地理解STL迭代器的使用方法和特点。 ... [详细]
  • 本文介绍了九度OnlineJudge中的1002题目“Grading”的解决方法。该题目要求设计一个公平的评分过程,将每个考题分配给3个独立的专家,如果他们的评分不一致,则需要请一位裁判做出最终决定。文章详细描述了评分规则,并给出了解决该问题的程序。 ... [详细]
  • 本文介绍了UVALive6575题目Odd and Even Zeroes的解法,使用了数位dp和找规律的方法。阶乘的定义和性质被介绍,并给出了一些例子。其中,部分阶乘的尾零个数为奇数,部分为偶数。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • Android源码深入理解JNI技术的概述和应用
    本文介绍了Android源码中的JNI技术,包括概述和应用。JNI是Java Native Interface的缩写,是一种技术,可以实现Java程序调用Native语言写的函数,以及Native程序调用Java层的函数。在Android平台上,JNI充当了连接Java世界和Native世界的桥梁。本文通过分析Android源码中的相关文件和位置,深入探讨了JNI技术在Android开发中的重要性和应用场景。 ... [详细]
  • 本文讨论了一个数列求和问题,该数列按照一定规律生成。通过观察数列的规律,我们可以得出求解该问题的算法。具体算法为计算前n项i*f[i]的和,其中f[i]表示数列中有i个数字。根据参考的思路,我们可以将算法的时间复杂度控制在O(n),即计算到5e5即可满足1e9的要求。 ... [详细]
  • 本文讨论了如何在codeigniter中识别来自angularjs的请求,并提供了两种方法的代码示例。作者尝试了$this->input->is_ajax_request()和自定义函数is_ajax(),但都没有成功。最后,作者展示了一个ajax请求的示例代码。 ... [详细]
  • Ihaveaworkfolderdirectory.我有一个工作文件夹目录。holderDir.glob(*)>holder[ProjectOne, ... [详细]
  • 使用C++编写程序实现增加或删除桌面的右键列表项
    本文介绍了使用C++编写程序实现增加或删除桌面的右键列表项的方法。首先通过操作注册表来实现增加或删除右键列表项的目的,然后使用管理注册表的函数来编写程序。文章详细介绍了使用的五种函数:RegCreateKey、RegSetValueEx、RegOpenKeyEx、RegDeleteKey和RegCloseKey,并给出了增加一项的函数写法。通过本文的方法,可以方便地自定义桌面的右键列表项。 ... [详细]
  • 本文介绍了一种划分和计数油田地块的方法。根据给定的条件,通过遍历和DFS算法,将符合条件的地块标记为不符合条件的地块,并进行计数。同时,还介绍了如何判断点是否在给定范围内的方法。 ... [详细]
  • 本文介绍了解决二叉树层序创建问题的方法。通过使用队列结构体和二叉树结构体,实现了入队和出队操作,并提供了判断队列是否为空的函数。详细介绍了解决该问题的步骤和流程。 ... [详细]
author-avatar
两枚小瓶盖
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有