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 ...
随机推荐
- Nodejs中获取参数以及处理参数
先看题干效果 在这里我们建了一个表单 填入表单需要提交的信息 对两个参数进行获取和一个加法计算 表单html代码 <form action='http://localhost:8080' met ...
- ABAP术语-Transaction
Transaction 原文:http://www.cnblogs.com/qiangsheng/archive/2008/03/19/1112804.html Logical process in ...
- Kafka 推荐网站
Kafka系列文章 [Kafka设计解析(一)- Kafka背景及架构介绍](http://www.jasongj.com/2015/03/10/KafkaColumn1/) [Kafka设计解析(二 ...
- php 冒泡法 排序
<?php /** * php 冒泡法 * @param $arr * @param string $order 排序符 * @return $arr */ function orderarr( ...
- Python学习 :集合
集合 Set 集合的创建 集合的创建只有一种方式 集合中的元素必须是不可变的数据类型 集合是无序的,可以通过 for 循环来遍历或者迭代器进行筛选 s=set('xiaoming') s1=['ale ...
- 数据结构之 AVL个人笔记
从这位前辈的博客园中学习的数据结构:https://www.cnblogs.com/skywang12345/ 非常感谢这位前辈. 以下文章摘录于 :skywang12345的博客园:转载请注明出处: ...
- Python3爬虫(十七) Scrapy框架(一)
Infi-chu: http://www.cnblogs.com/Infi-chu/ 1.框架架构图: 2.各文件功能scrapy.cfg 项目的配置文件items.py 定义了Item数据结构,所有 ...
- 如何在golang中打印grpc详细日志
最近捣鼓fabric,在一个tls证书问题上纠结挺久,连接orderer服务时候,grpc日志总是冷冰冰的显示这个信息 Orderer Client Status Code: (2) CONNECTI ...
- hive 打印日志
hive -hiveconf hive.root.logger=INFO,console -e 'select 1' hive 打印log ,有时hive 在配置时默认不会将mapper reduce ...
- 为什么我要放弃javaScript数据结构与算法(第七章)—— 字典和散列表
本章学习使用字典和散列表来存储唯一值(不重复的值)的数据结构. 集合.字典和散列表可以存储不重复的值.在集合中,我们感兴趣的是每个值本身,并把它作为主要元素.而字典和散列表中都是用 [键,值]的形式来 ...