题意

给定$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. c# 事实证明,abstract类除了不能用new实例化和类没什么区别

    abstract类是抽象类,不能够实例化,大家都知道,abstract类往往和接口interface一块儿使用,针对接口中一些公共的方法进行实现,然后实体类去继承抽象类和接口.虽然abstract类不 ...

  2. 我要AFO啦好伤感啊

    我要AFO啦 虽然一直很垃圾 但是也很开心 接下来我要去学物理啦 原因是今天早上没有吃早餐?! 就这样把~ 白白

  3. 【BZOJ1560】【JSOI2009】火星藏宝图 [DP]

    火星藏宝图 Time Limit: 10 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description Input Output Samp ...

  4. 【HDU】6148 Valley Numer 数位DP

    [算法]数位DP [题意]定义V-number为从左到看单位数字未出现先递增后递减现象的数字,求0~N中满足条件的数字个数.T<=200,lenth(n)<=100 [题解]百度之星201 ...

  5. 12.13记录//QQDemo示例程序源代码

            笔记的完整版pdf文档下载地址: https://www.evernote.com/shard/s227/sh/ac692160-68c7-4149-83ea-0db5385e28b0 ...

  6. JS的prototype和__proto__

    一.prototype和__proto__的概念 prototype是函数的一个属性(每个函数都有一 个prototype属性),这个属性是一个指针,指向一个对象.它 是显示修改对象的原型的属性. _ ...

  7. bzoj 1058 bst

    因为是数列的维护,所以我们可以考虑用splay来维护,每次在x插入的时候就在x+1前面插入就行了,然后用bst来维护两问的答案,但是应该会tle.我们来考虑这个问题的性质,首先因为这个数列没有删除操作 ...

  8. fundamentals of the jQuery library

    1.why is jquery Only 32kB minified and gzipped. Can also be included as an AMD module Supports CSS3 ...

  9. python基础===时间处理模块

    时间模块 Python中有很多方便我们处理时间信息的模块 time 模块 datetime 模块 pytz 模块 dateutil 模块 这里我们着重介绍的是前两种 time模块 time.time( ...

  10. 自己动手一步步安装Linux系统

    自己动手一步步安装Linux系统 http://502245466.blog.51cto.com/7559397/1291910/