HDU1542Atlantis(扫描线)

题目链接

题目大意:给你n个覆盖矩形,问最后覆盖的面积。

解题思路:将每一个矩形拆成两条线段,一条是+1的,还有一条是减1的。然后扫描先从上往下扫描,碰到加1的那条线段,那么这条线段范围内的节点的覆盖信息就+1,直到碰到减1这个线段范围内的节点的覆盖信息都须要减1。

这样说可能理解不了,就能够画画矩形然后画下扫描线在理解理解。然后就是须要离散化的建树,由于这里是都double型的。所谓离散化建树的意思就是如今每一个叶子节点代表的是一段区间。而不是单独的一点。

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <algorithm> using namespace std;
const int maxn = 205; #define lson(x) (x<<1)
#define rson(x) ((x<<1) | 1) struct Node { int l, r, add;
double s;
void set (int l, int r, double s, int add) { this->l = l;
this->r = r;
this->s = s;
this->add = add;
}
}node[4 * maxn]; struct Line { double x, y1, y2;
int flag;
Line (double x, double y1, double y2, int flag) { this->x = x;
this->y1 = y1;
this->y2 = y2;
this->flag = flag;
} bool operator < (const Line &l) const { return x < l.x;
}
}; int n;
vector<Line> L;
vector<double> pos; void pushup(int u) { if (node[u].add)
node[u].s = pos[node[u].r + 1] - pos[node[u].l];
else if (node[u].l == node[u].r)
node[u].s = 0;
else
node[u].s = node[lson(u)].s + node[rson(u)].s;
} void build (int u, int l, int r) { node[u].set (l, r, 0, 0);
if (l == r)
return;
int m = (l + r)>>1;
build(lson(u), l, m);
build(rson(u), m + 1, r);
} void update (int u, int l, int r, int v) { if (node[u].l >= l && node[u].r <= r) {
node[u].add += v;
pushup(u);
return;
} int m = (node[u].l + node[u].r)>>1;
if (l <= m)
update (lson(u), l, r, v);
if (r > m)
update (rson(u), l, r, v);
pushup(u);
} void init () { pos.clear();
L.clear();
double x1, y1, x2, y2; for (int i = 0; i < n; i++) {
scanf ("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
pos.push_back(y1);
pos.push_back(y2);
L.push_back(Line(x1, y1, y2, 1));
L.push_back(Line(x2, y1, y2, -1));
} sort (pos.begin(), pos.end());
sort (L.begin(), L.end());
pos.erase(unique(pos.begin(), pos.end()), pos.end()); build(1, 0, (int)pos.size() - 1);
} double solve() { init();
double ans = 0;
for (int i = 0; i < (int)L.size() - 1; i++) { int l = lower_bound(pos.begin(), pos.end(), L[i].y1) - pos.begin();
int r = lower_bound(pos.begin(), pos.end(), L[i].y2) - pos.begin();
update (1, l, r - 1, L[i].flag);
// printf ("%.2lf\n", node[1].s);
ans += node[1].s * (L[i + 1].x - L[i].x);
}
return ans;
} int main () { int T = 0;
double x1, y1, x2, y2;
while (scanf ("%d", &n) && n) { printf ("Test case #%d\n", ++T);
printf ("Total explored area: %.2lf\n\n", solve());
}
return 0;
}

HDU1542Atlantis(扫描线)的更多相关文章

  1. HDU1542--Atlantis(扫描线)

    给N个矩形的端点坐标,求矩形覆盖面积和. 原理很简单,从左到右扫描,线段树记录的是纵向覆盖的长度.区间更新.因为坐标是实数而且很大,所以需要离散化. WA+RE+CE+MLE+...一共错了二十多次. ...

  2. HDU1542-Atlantis【离散化&线段树&扫描线】个人认为很全面的详解

    刚上大一的时候见过这种题,感觉好牛逼哇,这都能算 如今已经不打了,不过适当写写题保持思维活跃度还是不错的,又碰到这种题了,想把它弄出来 说实话,智商不够,看了很多解析,花了4.5个小时才弄明白 网上好 ...

  3. 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)

    D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...

  4. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  5. HUD 4007 [扫描线][序]

    /* 大连热身B题 不要低头,不要放弃,不要气馁,不要慌张 题意: 坐标平面内给很多个点,放置一个边长为r的与坐标轴平行的正方形,问最多有多少个点在正方形内部. 思路: 按照x先排序,然后确定x在合法 ...

  6. Atitit 图像扫描器---基于扫描线

    Atitit 图像扫描器---基于扫描线 调用范例 * @throws FileExistEx */ public static void main(String[] args) throws Fil ...

  7. 扫描线+堆 codevs 2995 楼房

    2995 楼房  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 黄金 Gold 题解       题目描述 Description 地平线(x轴)上有n个矩(lou)形(fan ...

  8. 【BZOJ-4059】Non-boring sequences 线段树 + 扫描线 (正解暴力)

    4059: [Cerc2012]Non-boring sequences Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 440  Solved: 16 ...

  9. 【BZOJ-4422】Cow Confinement 线段树 + 扫描线 + 差分 (优化DP)

    4422: [Cerc2015]Cow Confinement Time Limit: 50 Sec  Memory Limit: 512 MBSubmit: 61  Solved: 26[Submi ...

随机推荐

  1. devops issue

    1.Nginx(refercence:https://zhuanlan.zhihu.com/p/24382606) summary: DJANGO_PROJECT = /home/django/dja ...

  2. js实现音量拖拽的效果模拟

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>js ...

  3. [LUOGU] P1962 斐波那契数列

    求斐波那契第n项. [f(n-1) f(n)] * [0,1] = [f(n) f(n+1)] [1,1] 由此原理,根据矩阵乘法的结合律,用快速幂算出中间那个矩阵的n次方即可. 快速幂本质和普通快速 ...

  4. 树莓派搭建Seafile个人网盘

    步骤一.安装Seafile依赖包 yum install python-setuptools python-ldap python-memcached MySQL-python mariadb mar ...

  5. linux配置网桥

    实现环境: centos7   Linux devstack01 3.10.0-693.el7.x86_64 1.创建br配置文件,ifcfg-br2 cp ifcfg-br0 ifcfg-br2 2 ...

  6. tiny4412u-boot烧写及根文件系统制作(不进入终端问题)

    http://m.blog.csdn.net/article/details?id=51400196(转) VMware12 环境:ubuntu12.4 开发板:tiny4412 首先烧写bootlo ...

  7. 杭电 2647 Reward (拓扑排序反着排)

    Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he wants to ...

  8. MySQL之federated

    由于夸服务器查询的限制,federated能够使得所有的表像是在同一台服务器上查询 (show engines-->no-->在my.ini里面添加fedrated) 经过测试,在开启fe ...

  9. ES6(字符串)

    ES6新增字符串特性 一.Unicode的表示法 当码值>2个字节(0xff) 即第一个数字未处理,不显示 处理这种超过2字节的情况,用{}包起来即可 二.API 1.ES5中 码值>2个 ...

  10. luoguT21778 过年

    差分一下上线段树 #include <iostream> #include <cstdio> #include <vector> using namespace s ...