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. MTK android L使用汇顶TP如何使用B协议

    使用B协议上报之前的代码: #include "tpd.h" #include "tpd_custom_gt9xx.h" #ifndef TPD_NO_GPIO ...

  2. Django:(4)Django和Ajax

    向服务器发送请求的途径: 1. 浏览器地址栏,默认get请求 2. form表单: get请求: post请求 3. a标签,默认get请求 4. Ajax:get请求:post请求 Ajax的特点( ...

  3. windows开启远程

    windows开启远程桌面超级简单,跟linux相比太简单了. 补充:有瑕疵,应该是远程中的远程桌面属性打钩,但是W8.1没有这个选项,W7可以,其次创建一个管理员账户,身份是管理员,不是标准用户,要 ...

  4. spring-kafka

    spring-kafka 使用spring-kafka的小伙伴,看过来. 说明 因为spring-kafka封装的比较厉害,可能跟你实际使用起来有很大的差别. 一个简单的消费例子 在spring-bo ...

  5. Spring boot精要

    1.自动配置:针对很多Spring应用程序的常见应用功能,SpringBoot能自动提供相关配置: 2.起步依赖:告诉SpringBoot需要什么功能,他就能引入需要的库: 3.命令行界面:这是Spr ...

  6. css 實現微信聊天類似的氣泡

    要實現這樣的效果 代碼如下: --------------------------------------- <style> .test{width:300px; padding:30px ...

  7. Apple Swift编程语言新手教程

    文件夹 1   简单介绍 2   Swift入门 3   简单值 4   控制流 5   函数与闭包 6   对象与类 7   枚举与结构 1   简单介绍 今天凌晨Apple刚刚公布了Swift编程 ...

  8. 手游产品经理初探(二)从营销角度看loading界面

    近期開始写产品相关的专题,准备从细节入手去思考.总结一些不为人注意的细节地方. 今天给大家分享的是游戏里面都有的loading界面. 还是从几个在Facebook上排名靠前的Casino游戏的load ...

  9. JSP简单练习-包装类综合应用实例

    <%@ page contentType="text/html; charset=gb2312" %><!-- JSP指令标签 --> <%@ pag ...

  10. iOS UI03_LTView

    // //  LTView.h //  OC03_LTView // //  Created by dllo on 15/7/31. //  Copyright (c) 2015年 dllo. All ...