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 ...
随机推荐
- 技巧:Vimdiff 使用(改)
技巧:Vimdiff 使用(改) 各种 IDE 大行其道的同时,传统的命令行工具以其短小精悍,随手可得的特点仍有很大的生存空间,这篇短文介绍了一个文本比较和合并的小工具:vimdiff.希望能对在 U ...
- 竞赛题解 - [CF 1080D]Olya and magical square
Olya and magical square - 竞赛题解 借鉴了一下神犇tly的博客QwQ(还是打一下广告) 终于弄懂了 Codeforces 传送门 『题目』(直接上翻译了) 给一个边长为 \( ...
- .Net core使用Quartz.Net 实现定时任务
很多情况下,我们需要完成一些定时执行的功能,用很多定时工具,像:hangfire,TimeJob,以及Quartz.net,不过quartz.net 比较精确一些,功能也比较强大,所以我选择了Quar ...
- Django中间件执行顺序
中间件 Django中的中间件是一个轻量级.底层的插件系统,可以介入Django的请求和响应处理过程,修改Django的输入或输出.中间件的设计为开发者提供了一种无侵入式的开发方式,增强了Django ...
- mysqld_safe之三言两语
today,one buddy in IMG wechat group 2 asked "why i've installed the MySQL 5.7 on linux serv ...
- linux 学习第七天
一.bash 使用(for循环.while循环) 1.1.批量添加用户 1.2.查看用户是否存在 A.cut -d : -f 1 /etc/passwd B.id dream (id 用户名称) C ...
- wordpress | 网站访问速度优化方案(Avada)
一.谷歌字体 原因: Wordpress系统默认使用谷歌字体,在国内谷歌域名被屏蔽,所以导致操作反应慢. 解决方法: 对于后台:找到Wordpress这个文件 /wp-includes/script- ...
- Dubbo 安装监控中心
一.Dubbo 安装服务管理控制台 1.在官方Github下载Dubbo OPS 2.下载incubator-dubbo-ops源码后,解压修改配置文件Zookeeper注册中心地址 3.maven打 ...
- Thinkphp5 使用composer中seeder播种机
前因: 前几天,客户要求做一个会员问答的系统,我就按流程做了,到了需要调用数据库数据时,觉得一个个添加又有点笨~ 解决过程: 后来查了查手册,看看国外blog案例,我搞出来了个不错的方法~~~ 我的使 ...
- ethereum(以太坊)(十四)--Delete
pragma solidity ^0.4.10; contract Delete{ /* delete可用于任何变量(除mapping),将其设置成默认值 bytes/string:删除所有元素,其长 ...