You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
/*油漆面积
X星球的一批考古机器人正在一片废墟上考古。该区域的地面坚硬如石、平整如镜。
管理人员为方便,建立了标准的直角坐标系。每个机器人都各有特长、身怀绝技。它们感兴趣的内容也不相同。
经过各种测量,每个机器人都会报告一个或多个矩形区域,作为优先考古的区域。
矩形的表示格式为(x1,y1,x2,y2),代表矩形的两个对角点坐标。
为了醒目,总部要求对所有机器人选中的矩形区域涂黄色油漆。
小明并不需要当油漆工,只是他需要计算一下,一共要耗费多少油漆。
其实这也不难,只要算出所有矩形覆盖的区域一共有多大面积就可以了。
注意,各个矩形间可能重叠。
本题的输入为若干矩形,要求输出其覆盖的总面积。
输入格式:
第一行一个整数n表示有多少个矩形(1<=n<10000)
接下来的n行每行有4个整数x1 y1 x2 y2空格分开表示矩形的两个对角顶点坐标。
(0<= x1,y1,x2,y2 <=10000)
输出格式:
一行一个整数,表示矩形覆盖的总面积面积。
例如,
测试用例:
2
2 2 9 5
6 1 12 9
答案:
60
输入:
3
1 5 10 10
3 1 20 20
2 7 15 17
程序应该输出:
340
再例如,
输入:
3
5 2 10 6
2 7 12 10
8 1 15 15
程序应该输出:
128
I 暴力解法
https://www.bilibili.com/video/BV1VV411z7S2?p=11
II 高端解法
https://www.bilibili.com/video/BV1VV411z7S2?p=12
线段树+扫描线 模板题 (百度一下线段树+扫描线,可以看到还有很多求周长的东西~)
*/
using namespace std;
const int N = 1e4 + 10; //矩形的数量
int n, sum;
// 65536kb /1024 =64mb
// int p[N][N]; //10000*10000*4 byte =4 0000 0000 byte /1024/1024=381MB
// 0 1
bool p[N][N]; //这里需要估算一下空间因为int是8个bit,bool是1个bit。
//模拟,对于(x1,y1)->(x2,y2)内的所有单元格,都标识为已被刷过油漆
//注意一下这里坐标与单元格的映射关系,就把每个单元格的左下角坐标认为是单元格的编号
void paint(int x1, int y1, int x2, int y2) {
for (int i = x1; i < x2; i++)
for (int j = y1; j < y2; j++)
p[i][j] = 1;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
paint(x1, y1, x2, y2);
}
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
if (p[i][j]) sum++;
cout << sum << endl;
return 0;
}