A template of discretization + scaning line + segment tree.

It's easy to understand, but a little difficult for coding as it has a few details.

#include"Head.cpp"

const int N=207;

double x[N<<1];

struct Point{
double l,r,h;
int w;
bool operator< (const Point &b)const{
return h < b.h;
}
}a[N<<2]; struct SegmentTree{
int l,r,w;
double len;
}t[N<<3]; #define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r inline void Pushup(int rt){
if(t[rt].w != 0)
t[rt].len = x[t[rt].r+1] - x[t[rt].l];
else if(t[rt].l == t[rt].r)
t[rt].len = 0;
else // t[rt].w == 0
t[rt].len = t[rt<<1].len + t[rt<<1|1].len;
} inline void Build(int rt,int l,int r){
t[rt] = (SegmentTree){l, r, 0, 0};
if(l == r) return;
int mid = (l>>1) + (r>>1) + (l&r&1);
Build(lson),Build(rson);
} inline void Updata(int rt,int L,int R,int val){
if(t[rt].l >= L && t[rt].r <= R){
t[rt].w += val;
Pushup(rt);
return;
}
int mid = (t[rt].l>>1) + (t[rt].r>>1) +(t[rt].l&t[rt].r&1);
if(L <= mid) Updata(rt<<1, L, R, val);
if(R > mid) Updata(rt<<1|1, L, R, val);
Pushup(rt);
} inline int FindPos(int l,int r,double val){
int mid;
while(l<=r){
mid = (l>>1) + (r>>1) + (l&r&1);
if(x[mid] > val)
r = mid - 1;
else if(x[mid] < val)
l = mid + 1;
else // x[mid] == val
break;
}
return mid;
} int main(){
int task=0;
int n;
while(~scanf("%d",&n) && n!=0){
int cnt=0; R(i,1,n){
double x_1,x_2,y_1,y_2;
scanf("%lf%lf%lf%lf",&x_1,&y_1,&x_2,&y_2); // 1 -> floor, -1 -> ceiling
x[++cnt] = x_1;
a[cnt] = (Point){x_1, x_2, y_1, 1};
x[++cnt] = x_2;
a[cnt] = (Point){x_1, x_2, y_2, -1};
} sort(x+1, x+cnt+1);
sort(a+1, a+cnt+1);
Build(1, 1, cnt); double ans = 0;
R(i,1,cnt-1){
int l = FindPos(1, cnt, a[i].l),
r = FindPos(1, cnt, a[i].r) - 1;
Updata(1, l, r, a[i].w);
ans += t[1].len * (a[i+1].h - a[i].h);
} printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++task, ans);
} return 0;
}
/*
2
10 10 20 20
15 15 25 25.5
0
*/

HDU 1542/POJ 1151 Atlantis (scaning line + segment tree)的更多相关文章

  1. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

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

  2. 扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]

    学习链接:http://blog.csdn.net/lwt36/article/details/48908031 学习扫描线主要学习的是一种扫描的思想,后期可以求解很多问题. 扫描线求矩形周长并 hd ...

  3. hdu 1542 & & poj 1151

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

  4. POJ 1151 Atlantis(扫描线)

    题目原链接:http://poj.org/problem?id=1151 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS   Memory Limit: 10 ...

  5. [POJ 1151] Atlantis

    一样的题:HDU 1542 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18148   Accepted ...

  6. POJ 1151 Atlantis(线段树-扫描线,矩形面积并)

    题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...

  7. POJ 1151 Atlantis 矩形面积求交/线段树扫描线

    Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...

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

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

  9. POJ 1151 Atlantis 线段树+离散化+扫描线

    这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...

随机推荐

  1. MongoDB 各项命名规范

    每日一句 Progress is the result of a bunch of failures. 进步是不断失败的成果. 概述 MongoDB涉及到的一些比如集合啥的命令规范. 集合的命名规范 ...

  2. 深入C++02:深入学习C++还必须掌握的基础

    深入学习C++还必须掌握的基础 掌握形参带默认的函数 1.给默认值方向:从右向左给默认值: 2.调用效率:如果传默认值或者立即数(不需要从容器或内存取取的数字)的话都是直接将数字直接push进栈:没有 ...

  3. [CF1073G]LCP问题

    题意:给一个长n的字符串S,q组询问,每组给两个集合A,B.求集合A中的点和集合B中的点所有组合情况的lcp的和. 思路: 好像比较常规,可是代码能力差还是调了1.5h.主要还是虚树板子不熟(加入的时 ...

  4. 关于p命名空间和c命名空间 外加一个context

    P命名空间注入 : 需要在头文件中加入约束文件 导入约束 : xmlns:p="http://www.springframework.org/schema/p" 如 xmlns=& ...

  5. Redis中的原子操作(2)-redis中使用Lua脚本保证命令原子性

    Redis 如何应对并发访问 使用 Lua 脚本 Redis 中如何使用 Lua 脚本 EVAL EVALSHA SCRIPT 命令 SCRIPT LOAD SCRIPT EXISTS SCRIPT ...

  6. CabloyJS究竟是一款什么样的框架

    CabloyJS是什么样的框架 CabloyJS 是一款自带工作流引擎的 Node.js 全栈框架,一款面向开发者的低代码开发平台,更是一款兼具低代码的开箱即用和专业代码的灵活定制的 PAAS 平台 ...

  7. 开发工具-RSA加解密

    更新日志 2022年6月10日 初始化链接. https://toolb.cn/rsa

  8. 打字速度单位WPM、KPM定义与计算方法

    国际通行的打字速度单位是WPM,用来量度打字速度的快慢.另外还有相关的KPM.CPM.KPH等打字速度单位,下面一一介绍. ----WPM------------------------------- ...

  9. go-zero微服务实战系列(五、缓存代码怎么写)

    缓存是高并发服务的基础,毫不夸张的说没有缓存高并发服务就无从谈起.本项目缓存使用Redis,Redis是目前主流的缓存数据库,支持丰富的数据类型,其中集合类型的底层主要依赖:整数数组.双向链表.哈希表 ...

  10. (数据科学学习手札139)geopandas 0.11版本重要新特性一览

    本文示例代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 大家好我是费老师,就在几天前,geopandas ...