2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)
There are nnn rectangles on the plane. The problem is to find the area of the union of these rectangles. Note that these rectangles might overlap with each other, and the overlapped areas of these rectangles shall not be counted more than once. For example, given a rectangle AAA with the bottom left corner located at (0,0)(0, 0)(0,0) and the top right corner at (2,2)(2, 2)(2,2), and the other rectangle BBB with the bottom left corner located at (1,1)(1,1)(1,1) and the top right corner at (3,3)(3,3)(3,3), it follows that the area of the union of AAA and BBB should be 777, instead of 888.
Although the problem looks simple at the first glance, it might take a while to figure out how to do it correctly. Note that the shape of the union can be very complicated, and the intersected areas can be overlapped by more than two rectangles.
Note:
(1) The coordinates of these rectangles are given in integers. So you do not have to worry about the floating point round-off errors. However, these integers can be as large as 1,000,0001,000,0001,000,000.
(2) To make the problem easier, you do not have to worry about the sum of the areas exceeding the long integer precision. That is, you can assume that the total area does not result in integer overflow.
Input Format
Several sets of rectangles configurations. The inputs are a list of integers. Within each set, the first integer (in a single line) represents the number of rectangles, n, which can be as large as 100010001000. After n, there will be n lines representing the n rectangles; each line contains four integers <a,b,c,d><a, b, c, d><a,b,c,d> , which means that the bottom left corner of the rectangle is located at (a,b)(a, b)(a,b), and the top right corner of the rectangle is located at (c,d)(c, d)(c,d). Note that integers aaa, bbb, ccc, ddd can be as large as 1,000,0001,000,0001,000,000.
These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n=0n = 0n=0 (zero) signifies the end of input.
Output Format
For each set of the rectangles configurations appeared in the input, calculate the total area of the union of the rectangles. Again, these rectangles might overlap each other, and the intersecting areas of these rectangles can only be counted once. Output a single star '*' to signify the end of outputs.
样例输入
2
0 0 2 2
1 1 3 3
3
0 0 1 1
2 2 3 3
4 4 5 5
0
样例输出
7
3
* 直接套模板
参考博客:POJ 1151 Atlantis(重叠矩阵面积和=离散化)
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=;
struct Node//矩形
{
double x1,y1,x2,y2;
}nodes[maxn];
double x[maxn],y[maxn];
bool mp[maxn][maxn]; int find(double *x,double val,int n)//在数组x中找到val值的位置
{
int L=,R=n-;
while(R>=L)
{
int mid=L+(R-L)/;
if(x[mid]==val) return mid;
else if(x[mid]>val) R=mid-;
else L=mid+;
}
return -;
} int main()
{
int n,num1,num2;
while(~scanf("%d",&n))
{
if(n==){printf("*\n");break;}
num1=num2=;//num1记录有多少个不同x值,num2记录y的
memset(mp,,sizeof(mp));
for(int i=;i<n;++i)
{
scanf("%lf%lf%lf%lf",&nodes[i].x1,&nodes[i].y1,&nodes[i].x2,&nodes[i].y2);
x[num1++]=nodes[i].x1;
x[num1++]=nodes[i].x2;
y[num2++]=nodes[i].y1;
y[num2++]=nodes[i].y2;
}
sort(x,x+num1);
sort(y,y+num2);
num1=unique(x,x+num1)-x;//去重
num2=unique(y,y+num2)-y;//去重 for(int i=;i<n;++i)
{
//找出第i个原始大矩形覆盖的小矩形范围
int L_x=find(x,nodes[i].x1,num1);
int R_x=find(x,nodes[i].x2,num1);
int L_y=find(y,nodes[i].y1,num2);
int R_y=find(y,nodes[i].y2,num2); for(int j=L_x;j<R_x;++j)
for(int k=L_y;k<R_y;++k)
mp[j][k]=true;
}
long long int ans=;
for(int i=;i<num1;++i)
for(int j=;j<num2;++j)if(mp[i][j])
ans += (x[i+]-x[i])*(y[j+]-y[j]);
printf("%lld\n",ans);
}
return ;
}
2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)的更多相关文章
- 2017 ACM/ICPC 南宁区 网络赛 Overlapping Rectangles
2017-09-24 20:11:21 writer:pprp 找到的大神的代码,直接过了 采用了扫描线+线段树的算法,先码了,作为模板也不错啊 题目链接:https://nanti.jisuanke ...
- 2017ICPC南宁赛区网络赛 Minimum Distance in a Star Graph (bfs)
In this problem, we will define a graph called star graph, and the question is to find the minimum d ...
- 2017ICPC南宁赛区网络赛 The Heaviest Non-decreasing Subsequence Problem (最长不下降子序列)
Let SSS be a sequence of integers s1s_{1}s1, s2s_{2}s2, ........., sns_{n}sn Each integer i ...
- 2017ICPC南宁赛区网络赛 Train Seats Reservation (简单思维)
You are given a list of train stations, say from the station 111 to the station 100100100. The passe ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles
There are nn rectangles on the plane. The problem is to find the area of the union of these rectangl ...
- 2017ICPC北京赛区网络赛 Minimum(数学+线段树)
描述 You are given a list of integers a0, a1, …, a2^k-1. You need to support two types of queries: 1. ...
- 2017ICPC北京赛区网络赛 Visiting Peking University(简单思维)
描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0 ...
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
随机推荐
- 把javabean复制到另一个javabean 使用BeanUtils.copyProperties(a,b) 复制
该方法对于两种不同的jar包有两种不同的意义 ,a,b通常是两个结构相似的javabean,注意:a,b里的定义类型名称必须一致才能复制 引用的是org.springframework.beans 则 ...
- vue项目 sockjs-node一直报错问题
vue3下 vue.config.js中 devServer: { host: '0.0.0.0', port: 8080, proxy: { '/': { target: 'http://127.0 ...
- HDU-6386-最短路
Age of Moyu Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Tot ...
- CRM WEB UI 03搜索界面新建按钮调到详细界面
这个和上一个差不多,简单说下: 1.因为NEW是在创建搜索界面的时候加的,所以此时只需在结果界面重定义NEW事件: method EH_ONNEW. OP_NEW( ). endmethod. 2.结 ...
- virtualbox 中centOS在不能ssh
这个重要跟虚拟机的网络设置有关系.废话不多说. 针对一个网卡的形式.可以如下进行配置 1.网络-- 连接方式还选择“网络地址转换(NAT)” 其他不变,展开高级,设置端口转发 主机IP设为本机IP, ...
- Python爬虫原理
前言 简单来说互联网是由一个个站点和网络设备组成的大网,我们通过浏览器访问站点,站点把HTML.JS.CSS代码返回给浏览器,这些代码经过浏览器解析.渲染,将丰富多彩的网页呈现我们眼前: 一.爬虫是什 ...
- java爬虫进阶 —— ip池使用,iframe嵌套,异步访问破解
写之前稍微说一下我对爬与反爬关系的理解 一.什么是爬虫 爬虫英文是splider,也就是蜘蛛的意思,web网络爬虫系统的功能是下载网页数据,进行所需数据的采集.主体也就是根据开始的超链接,下 ...
- win7下使用U盘安装双系统(Ubuntu-17)
1.首先下载Ubuntu镜像文件,下载地址:http://mirrors.neusoft.edu.cn/ 2.下载 U盘操作系统安装工具- Universal USB Installer ,下载地址: ...
- SQL 常用判断语句
我们在做sql更新时,为防止sql重复执行报错,需要对所需要执行的对象进行判断是否存在: 常用判断脚本如下: 判断视图是否存在 IF object_id('viewname') IS not NULL ...
- css3 前端开发
一.前缀: -moz(例如 -moz-border-radius)用于Firefox -webkit(例如:-webkit-border-radius)用于Safari和Chrome. 二.CSS3圆 ...