Description

Ted has a new house with a huge window. In this big summer, Ted decides to decorate the window with some posters to prevent the glare outside. All things that Ted can find are rectangle posters. 
However, Ted is such a picky guy that in every poster he finds something ugly. So before he pastes a poster on the window, he cuts a rectangular hole on that poster to remove the ugly part. Ted is also a careless guy so that some of the pasted posters may overlap when he pastes them on the window. 
Ted wants to know the total area of the window covered by posters. Now it is your job to figure it out. 
To make your job easier, we assume that the window is a rectangle located in a rectangular coordinate system. The window’s bottom-left corner is at position (0, 0) and top-right corner is at position (50000, 50000). The edges of the window, the edges of the posters and the edges of the holes on the posters are all parallel with the coordinate axes. 
 

Input

The input contains several test cases. For each test case, the first line contains a single integer N (0<N<=50000), representing the total number of posters. Each of the following N lines contains 8 integers x1, y1, x2, y2, x3, y3, x4, y4, showing details about one poster. (x1, y1) is the coordinates of the poster’s bottom-left corner, and (x2, y2) is the coordinates of the poster’s top-right corner. (x3, y3) is the coordinates of the hole’s bottom-left corner, while (x4, y4) is the coordinates of the hole’s top-right corner. It is guaranteed that 0<=xi, yi<=50000(i=1…4) and x1<=x3<x4<=x2, y1<=y3<y4<=y2. 
The input ends with a line of single zero. 
 

Output

For each test case, output a single line with the total area of window covered by posters.

题目大意:墙上有n张长方形的纸,每张纸都被挖掉了一个长方形(有可能贴着原长方形的边界),给这n个长方形的坐标及被挖掉的长方形的坐标,问这些纸一共覆盖了多少面积。

思路:把每个被挖掉一块的长方形分成4块或以下的真·长方形(怎么分随便你能AC就行),然后就是普通的扫描线+线段树的问题了。至于离散化,点数和数据范围一样大就省了。至于初始化线段树我想应该是不用的,如果代码是对的理论上来讲每做完一组数据,线段树都是空的才对。

代码(HDU 484MS/POJ 532MS):

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long LL; const int MAXN = ; struct Line {
int y, x_st, x_ed, flag;
Line() {}
Line(int y, int x_st, int x_ed, int flag):
y(y), x_st(x_st), x_ed(x_ed), flag(flag) {}
bool operator < (const Line &rhs) const {
return y > rhs.y;
}
}; Line a[MAXN * ];
int tree[MAXN * ], sum[MAXN * ];
LL ans; void update(int x, int l, int r, int tl, int tr, int t) {
int lc = x << , rc = lc ^ ;
if(tl <= l && r <= tr) {
tree[x] += t;
if(tree[x] > ) sum[x] = r - l;
else if(r - l == ) sum[x] = ;
else sum[x] = sum[lc] + sum[rc];
}
else {
int mid = (l + r) >> ;
if(tl < mid) update(lc, l, mid, tl, tr, t);
if(tr > mid) update(rc, mid, r, tl, tr, t);
if(tree[x] == ) sum[x] = sum[lc] + sum[rc];
else sum[x] = r - l;
}
} void solve(int n) {
sort(a, a + n);
ans = ;
for(int i = ; i < n; ++i) {
if(i > ) ans += (a[i - ].y - a[i].y) * LL(sum[]);
update(, , , a[i].x_st, a[i].x_ed, a[i].flag);
}
} int main() {
int n, x[], y[];
while(scanf("%d", &n) != EOF) {
if(n == ) break;
int cnt = ;
for(int i = ; i <= n; ++i) {
for(int j = ; j <= ; ++j) scanf("%d%d", &x[j], &y[j]);
if(x[] != x[]) {
a[cnt++] = Line(y[], x[], x[], );
a[cnt++] = Line(y[], x[], x[], -);
}
if(x[] != x[]) {
a[cnt++] = Line(y[], x[], x[], );
a[cnt++] = Line(y[], x[], x[], -);
}
if(y[] != y[]) {
a[cnt++] = Line(y[], x[], x[], );
a[cnt++] = Line(y[], x[], x[], -);
}
if(y[] != y[]) {
a[cnt++] = Line(y[], x[], x[], );
a[cnt++] = Line(y[], x[], x[], -);
}
}
solve(cnt);
cout<<ans<<endl;
}
}

最新文章

  1. JavaScript系列文章:自动类型转换
  2. Winform TextBox中只能输入数字的几种常用方法(C#)
  3. replace
  4. Java并发编程学习笔记(一)——线程安全性
  5. WPF学习之路(十一)布局(续)
  6. atittit.表单验证的实现方式以及原理本质以及选型以及自定义兼容easyui dsl规则的表单验证
  7. 【军哥谈CI框架】之入门教程之第二讲:分析CI结构和CI是怎么工作的
  8. SQLite之写一个表
  9. Struts2笔记——struts常用标签
  10. hdoj 5386 Cover
  11. 在安装软件CAJViewer时出现,“错误1327。无效驱动器:F:
  12. css-盒模型,浮动,定位之间的关系
  13. vs 2010 引用DLL 遇到问题
  14. [ios2] 利用钥匙串,在应用里保存用户密码的方法 【转】
  15. What is “Mock You” :Raise,callback,verify [转载]
  16. mysql中使用enum,如何获取所有可能的值
  17. eventproxy 介绍这款好用的工具,前端事件式编程的思维
  18. Bootstrap3基础 img-responsive 响应式图片
  19. BT1120中的串行传输
  20. Jenkins配置项目

热门文章

  1. JavaScript Event Loop和微任务、宏任务
  2. Jquery拼图
  3. HTML-CSS的几种布局
  4. nginx的docker化部署
  5. 阿里云Windows远程连接出现身份验证错误,要求的函数不正确”的报错。
  6. php_Trait
  7. ElasticSearch 集群安装,简单使用
  8. Java学习笔记十八:Java面向对象的三大特性之封装
  9. 005---Linux文件与目录管理
  10. C# 面试题 (三)