There are nn 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 AA with
the bottom left corner located at (0,
0)(0,0) and
the top right corner at (2,
2)(2,2),
and the other rectangle BB with
the bottom left corner located at (1,1)(1,1) and
the top right corner at (3,3)(3,3),
it follows that the area of the union of AA and BB should
be 77,
instead of 88.

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,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 10001000.
After n, there will be n lines representing the n rectangles; each line contains four integers <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),
and the top right corner of the rectangle is located at (c,
d)(c,d).
Note that integers aa, bb, cc, dd can
be as large as 1,000,0001,000,000.

These configurations of rectangles occur repetitively in the input as the pattern described above. An integer n
= 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
*

题目来源

2017
ACM-ICPC 亚洲区(南宁赛区)网络赛

线段树加扫描线模板题,队友给力!!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; #define lz 2*u,l,mid
#define rz 2*u+1,mid+1,r
const int maxn=4222;
double sum[maxn];
int flag[maxn];
double X[maxn]; struct Node
{
double lx, rx, y;
int s;
Node(){};
Node(double lx_, double rx_, double y_, int s_)
{
lx=lx_, rx=rx_, y=y_, s=s_;
}
bool operator <(const Node &S) const
{
return y<S.y;
}
}line[maxn]; int find(double tmp, int n)
{
int l=1, r=n, mid;
while(l<=r)
{
mid=(l+r)>>1;
if(X[mid]==tmp) return mid;
else if(X[mid]<tmp) l=mid+1;
else r=mid-1;
}
} void push_up(int u, int l, int r)
{
if(flag[u]) sum[u]=X[r+1]-X[l];
else if(l==r) sum[u]=0;
else sum[u]=sum[2*u]+sum[2*u+1];
} void Update(int u, int l, int r, int tl, int tr, int c)
{
if(tl<=l&&r<=tr)
{
flag[u]+=c;
push_up(u,l,r);
return ;
}
int mid=(l+r)>>1;
if(tr<=mid) Update(lz,tl,tr,c);
else if(tl>mid) Update(rz,tl,tr,c);
else
{
Update(lz,tl,mid,c);
Update(rz,mid+1,tr,c);
}
push_up(u,l,r);
} int main()
{
// freopen("in.txt","r",stdin);
int n,tcase=0;
while(cin >> n,n)
{
int num=0;
memset(flag,0,sizeof(flag));
memset(sum,0,sizeof(sum));
for(int i=0; i<n; i++)
{
double x1,x2,y1,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[++num]=Node(x1,x2,y1,1);
X[num]=x1;
line[++num]=Node(x1,x2,y2,-1);
X[num]=x2;
}
sort(X+1,X+num+1);
sort(line+1,line+num+1);
int k=1;
for(int i=2; i<=num; i++)
if(X[i]!=X[i+1]) X[++k]=X[i];
double ans=0;
for(int i=1; i<num; i++)
{
int l=find(line[i].lx,k);
int r=find(line[i].rx,k)-1;
Update(1,1,k,l,r,line[i].s);
ans+=sum[1]*(line[i+1].y-line[i].y);
}
// printf("Test case #%d\n",++tcase);
// printf("Total explored area: %.2lf\n\n",ans);
printf("%.0lf\n",ans);
}
printf("*\n");
return 0;
}

2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Overlapping Rectangles的更多相关文章

  1. 2017ICPC南宁赛区网络赛 Overlapping Rectangles(重叠矩阵面积和=离散化模板)

    There are nnn rectangles on the plane. The problem is to find the area of the union of these rectang ...

  2. 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 ...

  3. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  4. 2017 ACM/ICPC 南宁区 网络赛 Overlapping Rectangles

    2017-09-24 20:11:21 writer:pprp 找到的大神的代码,直接过了 采用了扫描线+线段树的算法,先码了,作为模板也不错啊 题目链接:https://nanti.jisuanke ...

  5. 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)

    摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...

  6. ICPC 2018 徐州赛区网络赛

    ACM-ICPC 2018 徐州赛区网络赛  去年博客记录过这场比赛经历:该死的水题  一年过去了,不被水题卡了,但难题也没多做几道.水平微微有点长进.     D. Easy Math 题意:   ...

  7. Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)

    参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...

  8. [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题

    第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...

  9. 2016 ACM/ICPC亚洲区大连站-重现赛 解题报告

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5979 按AC顺序: I - Convex Time limit    1000 ms Memory li ...

随机推荐

  1. Linux下汇编语言学习笔记21 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  2. Linux下汇编语言学习笔记14 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  3. poj —— 1274 The Perfect Stall

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26274   Accepted: 116 ...

  4. Nginx教程收集

    学习要系统,最推荐的方式是看书. 下面是收集的一些Nginx教程: https://www.gitbook.com/book/yinsigan/nginx/details http://www.ngi ...

  5. mvn解决jar包冲突

    转自:http://blog.csdn.net/guanglihuan/article/details/50512855 对于Jar包冲突问题,我们开发人员经常都会有碰到,当我们使用一些jar包中的类 ...

  6. SQL PATINDEX检索

    语法格式:PATINDEX ( '%pattern%' , expression ) 返回pattern字符串在表达式expression里第一次出现的位置,起始值从1开始算. pattern字符串在 ...

  7. tsdb import 相关

    今天一直在做opentsdb 大量导入数据的工作. 中间遇到了一些值得记录的问题, 这里随手记一下 明天好好整理 1. 多进程logger python的logging模块不支持多进程,但我们可以用s ...

  8. Serv-u 10.3 的图文安装教程及使用方法

    现在很多windows服务器都是使用serv_u作为ftp服务器,一般情况下我们使用Serv-U FTP Server v6.4.0.6,这里以serv_u 10.3为例介绍下,方便需要的朋友   一 ...

  9. [React] Make Compound React Components Flexible

    Our current compound component implementation is great, but it's limited in that users cannot render ...

  10. Centos下mahout安装与配置

    对于Mahout的安装与配置,须要一个前提.就是hadoop已经安装. 假设没有安装能够參考. http://blog.csdn.net/u012965373/article/details/4533 ...