HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)

时间:2022-12-25 18:00:02

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;
}
}

HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)的更多相关文章

  1. HDU 1828 &sol; POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)

    做这道题之前,建议先做POJ 1151  Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...

  2. HDU 1542:Atlantis(扫描线&plus;线段树 矩形面积并)&ast;&ast;&ast;

    题目链接 题意 给出n个矩形,求面积并. 思路 使用扫描线,我这里离散化y轴,按照x坐标从左往右扫过去.离散化后的y轴可以用线段树维护整个y上面的线段总长度,当碰到扫描线的时候,就可以统计面积.这里要 ...

  3. POJ 1151 Atlantis &lpar;扫描线&plus;线段树&rpar;

    题目链接:http://poj.org/problem?id=1151 题意是平面上给你n个矩形,让你求矩形的面积并. 首先学一下什么是扫描线:http://www.cnblogs.com/scau2 ...

  4. HDU 1828:Picture(扫描线&plus;线段树 矩形周长并)

    题目链接 题意 给出n个矩形,求周长并. 思路 学了区间并,比较容易想到周长并. 我是对x方向和y方向分别做两次扫描线.应该记录一个pre变量,记录上一次扫描的时候的长度,对于每次遇到扫描线统计答案的 ...

  5. poj 1151 &lpar;未完成&rpar; 扫描线 线段树 离散化

    #include<iostream> #include<vector> #include<cmath> #include<algorithm> usin ...

  6. HDU 3260&sol;POJ 3827 Facer is learning to swim(DP&plus;搜索)(2009 Asia Ningbo Regional)

    Description Facer is addicted to a game called "Tidy is learning to swim". But he finds it ...

  7. HDU 3264&sol;POJ 3831 Open-air shopping malls(计算几何&plus;二分)(2009 Asia Ningbo Regional)

    Description The city of M is a famous shopping city and its open-air shopping malls are extremely at ...

  8. HDU 3262&sol;POJ 3829 Seat taking up is tough(模拟&plus;搜索)(2009 Asia Ningbo Regional)

    Description Students often have problems taking up seats. When two students want the same seat, a qu ...

  9. HDU 3268&sol;POJ 3835 Columbus’s bargain(最短路径&plus;暴力枚举)(2009 Asia Ningbo Regional)

    Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...

随机推荐

  1. Interpreter

    #include <string> #include <iostream> #include <stack> using namespace std; stack& ...

  2. oracle用户管理实例

    oracle中的用户角色分为预定义角色和自定义角色. 角色是把常用的权限集中起来形成角色. 授权/分配角色命令 grant 权限/角色 to 用户 收回权限命令: revoke 综合案例: 创建一个用 ...

  3. JS中的 this

    JS中的 this 变化多端,似乎难以捉摸,但实际上对 this 的解读,还是有一定规律的. 分析this,该如何下手呢?下面有一个函数 function show(){ alert(this); } ...

  4. 【jQuery】&lpar;3&rpar;---Jquery操作Dom

                  1 内部插入节点 <body> <ul id="city"> <li id="bj" name=&qu ...

  5. Caffe 使用记录(五):math&lowbar;functions 分析

    本文转载自 Caffe源码(一):math_functions 分析 math_function 定义了caffe 中用到的一些矩阵操作和数值计算的一些函数,这里以float类型为例做简单的分析 1. ...

  6. 虚拟机下安装ubuntu后root密码登录失败的问题

    问题描述: 在虚拟机下安装了ubuntu中要输入用户名,一般情况下大家都会输入一个自己的网名或绰号之类的,密码也在这时设置过了. 但是当安装成功之后,使用命令#su root,然后输入刚才设置的密码, ...

  7. USBDM RS08&sol;HCS08&sol;HCS12&sol;Coldfire V1&comma;2&comma;3&comma;4&sol;DSC&sol;Kinetis Debugger and Programmer -- BDM Construction and Firmware

    Construction. Build the hardware using the information provided in the PCB download. The following a ...

  8. OC 对象调用属性或实例变量或方法的细节。

    1.成员变量可以理解为所有在类的头上声明的,无论是@interface.@implementation下用大括号括起来或者是用@property声明的变量都可以称作这个类的 成员变量,只是在@impl ...

  9. C&plus;&plus;总的const使用说明

    C++总的const使用说明 1. const修饰类成员变量 程序: #include <iostream> using namespace std; class A { public: ...

  10. 【mysql】新方法修改数据库密码以及解决--ERROR 1045 &lpar;28000&rpar;的问题

    之前 有写过一篇修改mysql数据库的密码的一篇随笔, 地址是:http://www.cnblogs.com/sxdcgaq8080/p/5667124.html 但是此次采用原本的老方法,出现了问题 ...