HDU 3265/POJ 3832 Posters(扫描线+线段树)(2009 Asia Ningbo Regional)
Description
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 ends with a line of single zero.
Output
题目大意:墙上有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)的更多相关文章
- HDU 1828 / POJ 1177 Picture (线段树扫描线,求矩阵并的周长,经典题)
做这道题之前,建议先做POJ 1151 Atlantis,经典的扫描线求矩阵的面积并 参考连接: http://www.cnblogs.com/scau20110726/archive/2013/0 ...
- HDU 1542:Atlantis(扫描线+线段树 矩形面积并)***
题目链接 题意 给出n个矩形,求面积并. 思路 使用扫描线,我这里离散化y轴,按照x坐标从左往右扫过去.离散化后的y轴可以用线段树维护整个y上面的线段总长度,当碰到扫描线的时候,就可以统计面积.这里要 ...
- POJ 1151 Atlantis (扫描线+线段树)
题目链接:http://poj.org/problem?id=1151 题意是平面上给你n个矩形,让你求矩形的面积并. 首先学一下什么是扫描线:http://www.cnblogs.com/scau2 ...
- HDU 1828:Picture(扫描线+线段树 矩形周长并)
题目链接 题意 给出n个矩形,求周长并. 思路 学了区间并,比较容易想到周长并. 我是对x方向和y方向分别做两次扫描线.应该记录一个pre变量,记录上一次扫描的时候的长度,对于每次遇到扫描线统计答案的 ...
- poj 1151 (未完成) 扫描线 线段树 离散化
#include<iostream> #include<vector> #include<cmath> #include<algorithm> usin ...
- HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)
Description Facer is addicted to a game called "Tidy is learning to swim". But he finds it ...
- HDU 3264/POJ 3831 Open-air shopping malls(计算几何+二分)(2009 Asia Ningbo Regional)
Description The city of M is a famous shopping city and its open-air shopping malls are extremely at ...
- HDU 3262/POJ 3829 Seat taking up is tough(模拟+搜索)(2009 Asia Ningbo Regional)
Description Students often have problems taking up seats. When two students want the same seat, a qu ...
- HDU 3268/POJ 3835 Columbus’s bargain(最短路径+暴力枚举)(2009 Asia Ningbo Regional)
Description On the evening of 3 August 1492, Christopher Columbus departed from Palos de la Frontera ...
随机推荐
- C++ 内存、new与malloc分配内存区别?
一关于内存 1.内存分配方式 内存分配方式有三种: (1)从静态存储区域分配.内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在.例如全局变量,static变量. (2)在栈上创建. ...
- axios api封装
import axios from 'axios'; import qs from 'qs'; const host = 'url' axios.defaults.baseURL = host; // ...
- 验证XML文档的范例代码
如果想变成自己的,就把这里的xml文档名替换成自己xml文档名 var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc. ...
- Hadoop 动态扩容 增加节点
基础准备 在基础准备部分,主要是设置hadoop运行的系统环境 修改系统hostname(通过hostname和/etc/sysconfig/network进行修改) 修改hosts文件,将集群所有节 ...
- 20190129-‘abcdefgh’里面挑出3个字母进行组合,一共有多少组合
一. 百度面试题‘abcdefgh’里面挑出3个字母进行组合,一共有多少组合,要求3个字母中不能有重复的组合,三个字母同时出现的次数只能出现一次,如出现了abc就不能出现cab,bca等 思路: 1. ...
- “子查询返回的值不止一个。当子查询跟随在 =、!=、<、<=、>、>= 之后,或子查询用作表达式时,这种情况是不允许的。”SQL查询错误解析
为了实现下述代码,首先得有数据库和相应的表格,本文用的是https://blog.csdn.net/qaz13177_58_/article/details/5575711/中的案例,即先用链接中那些 ...
- spark----词频统计(一)
利用Linux系统中安装的spark来统计: 1.选择目录,并创建一个存放文本的目录,将要处理的文本保存在该目录下以供查找操作: ① cd /usr/local ②mkdir mycode ③ cd ...
- [Real World Haskell翻译]第23章 GUI编程使用gtk2hs
第23章 GUI编程使用gtk2hs 在本书中,我们一直在开发简单的基于文本的工具.虽然这些往往是理想的接口,但有时图形用户界面(GUI)是必需的.有几个Haskell的GUI工具包是可用的.在本章中 ...
- 20145202马超《java》【课堂实验】P98测试
当时在加水印所以没来得及提交,然而我回宿舍第一时间就提交了,希望老师额能够看到
- 厦门Uber优步司机奖励政策(1月4日~1月10日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...