题意

给定$n​$个矩形$(x_1,y_1,x_2,y_2)​$,求这$n​$个矩形的面积并

题解

扫描线裸题,可以不用线段树维护,$O(n^2)$是允许的。

#include <cstdio>
#include <cstring>
#include <algorithm>
using std::sort;
using std::unique;
using std::lower_bound; const int N = 1e2 + 10;
int n, m, tot;
double ans, x1[N], y1[N], x2[N], y2[N], raw[N << 1], tmp[N << 1];
struct Node {
double x, dy, uy, g;
inline bool operator < (const Node &a) const { return x < a.x; }
} cy[N << 1]; int cnt;
double val[N << 3]; int fg[N << 3]; inline void update(int o, int l, int r) {
if(fg[o]) val[o] = raw[r + 1] - raw[l];
else if(l == r) val[o] = 0;
else val[o] = val[o << 1] + val[o << 1 | 1];
}
void modify (int ml, int mr, int k, int o = 1, int l = 1, int r = m) {
if(l >= ml && r <= mr) {
fg[o] += k, update(o, l, r);
return ;
}
int mid = (l + r) >> 1, lc = o << 1, rc = lc | 1;
if(ml <= mid) modify(ml, mr, k, lc, l, mid);
if(mr > mid) modify(ml, mr, k, rc, mid + 1, r);
update(o, l, r);
} int main () {
while(scanf("%d", &n) != EOF) {
if(!n) break; ++tot; ans = m = cnt = 0;
memset(val, 0, sizeof val), memset(fg, 0, sizeof fg);
for(int i = 1; i <= n; ++i) {
scanf("%lf%lf%lf%lf", x1 + i, y1 + i, x2 + i, y2 + i);
tmp[++m] = y1[i], tmp[++m] = y2[i];
}
sort(&tmp[1], &tmp[m + 1]); m = unique(&tmp[1], &tmp[m + 1]) - tmp - 1;
for(int i = 1; i <= n; ++i) {
int ind1 = lower_bound(&tmp[1], &tmp[m + 1], y1[i]) - tmp;
int ind2 = lower_bound(&tmp[1], &tmp[m + 1], y2[i]) - tmp;
raw[ind1] = y1[i], raw[ind2] = y2[i], y1[i] = ind1, y2[i] = ind2;
}
for(int i = 1; i <= n; ++i) {
cy[++cnt] = (Node){x1[i], y1[i], y2[i], 1};
cy[++cnt] = (Node){x2[i], y1[i], y2[i], -1};
} sort(&cy[1], &cy[cnt + 1]);
for(int i = 1; i <= cnt; ++i) {
modify(cy[i].dy, cy[i].uy - 1, cy[i].g);
ans += val[1] * (cy[i + 1].x - cy[i].x);
}
printf("Test case #%d\nTotal explored area: %.2lf\n\n", tot, ans);
}
return 0;
}

Poj1151&HDU1542 Atlantis(扫描线+线段树)的更多相关文章

  1. [POJ1151][HDU1542]Atlantis(线段树,扫描线)

    英文题面,我就只放个传送门了. Solution  题意是算矩形面积并,这是扫描线算法能解决的经典问题. 算法的大致思想是,把每一个矩形拆成上边和下边(以下称作扫描线),每条扫描线有四个参数l,r,h ...

  2. [HDU1542]Atlantis(扫描线+线段树)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  3. 【POJ1151】Atlantis(线段树,扫描线)

    [POJ1151]Atlantis(线段树,扫描线) 题面 Vjudge 题解 学一学扫描线 其实很简单啦 这道题目要求的就是若干矩形的面积和 把扫描线平行于某个轴扫过去(我选的平行\(y\)轴扫) ...

  4. hdu1542 Atlantis (线段树+扫描线+离散化)

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  5. poj1151 Atlantis——扫描线+线段树

    题目:http://poj.org/problem?id=1151 经典的扫描线问题: 可以用线段树的每个点代表横向被矩形上下边分割开的每一格,这样将一个矩形的出现或消失化为线段树上的单点修改: 每个 ...

  6. hdu-1542 Atlantis(离散化+线段树+扫描线算法)

    题目链接: Atlantis Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others) ...

  7. 【POJ1151】【扫描线+线段树】Atlantis

    Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...

  8. POJ 1151 Atlantis (扫描线+线段树)

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

  9. hdu1542 Atlantis(扫描线+线段树+离散)矩形相交面积

    题目链接:点击打开链接 题目描写叙述:给定一些矩形,求这些矩形的总面积.假设有重叠.仅仅算一次 解题思路:扫描线+线段树+离散(代码从上往下扫描) 代码: #include<cstdio> ...

  10. HDU 3642 - Get The Treasury - [加强版扫描线+线段树]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3642 Time Limit: 10000/5000 MS (Java/Others) Memory L ...

随机推荐

  1. [Luogu 1160] 队列安排

    Luogu 1160 队列安排 链表H2O H2O H2O模板. 太久不写链表,忘干净了,竟调了半个晚上. 保留备用. #include <cstdio> #include <cst ...

  2. mvc Dapper_Report_Down_ExcelFile

    一.基于Aspose.Cells.Dapper导出Excel Dapper的Query返回要不是对象的IEnumerable,要不是Dynamic的IEnumerable,都不适合不用反射就能够动态获 ...

  3. iOS多线程 iOS开发Demo(示例程序)源代码

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址(2013年12月29日更新版)   iOS程序源代码下载链接:01.大任务.zip22 ...

  4. cocos2dx中启用lua脚本

    AppDelegate 的 applicationDidFinishLaunching 方法中加载Lua引擎 bool AppDelegate::applicationDidFinishLaunchi ...

  5. 前端&后端程序员必备的Linux基础知识

    一 从认识操作系统开始 1.1 操作系统简介 我通过以下四点介绍什么操作系统: 操作系统(Operation System,简称OS)是管理计算机硬件与软件资源的程序,是计算机系统的内核与基石: 操作 ...

  6. 01背包问题的延伸即变形 (dp)

    对于普通的01背包问题,如果修改限制条件的大小,让数据范围比较大的话,比如相比较重量而言,价值的范围比较小,我们可以试着修改dp的对象,之前的dp针对不同的重量限制计算最大的价值.这次用dp针对不同的 ...

  7. js按值及引用传递中遇到的小问题

    有人闲的蛋疼,非要在函数中使用如下方式传值,尼玛一下把我搞糊涂了.于是决定发挥打破沙锅问到底的精神搞清楚它. var a = 1,b = [], c = {}; function f(a, b, c) ...

  8. javascript中数据属性与访问器属性

    1.数据属性 Configurable:true|false,表示能否通过delete将属性删除,默认为true.当把属性的Configurable设置为false后,该属性不能通过delete删除, ...

  9. python中filter函数

    python中filter()函数   filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断 ...

  10. Webmin LFD to LFI

    Webmin < 1.290 / Usermin < 1.220 - Arbitrary File Disclosure (Perl) https://www.exploit-db.com ...