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. 【freertos】011-信号量、互斥量及优先级继承机制源码分析

    目录 前言 11.1 任务同步 11.2 信号量概念 11.3 二值信号量 11.3.1 二值信号量概念 11.3.2 优先级翻转 11.3.3 二值信号量运作机制 11.4 计数信号量 11.4.1 ...

  2. 关于python导入数据库excel数据时出现102, b"Incorrect syntax near '.15562'.DB-Lib error message 20018, severity 1的问题总结

    1.对于在使用python导入sqlsever时,出现102, b"Incorrect syntax near '.15562'.DB-Lib error message 20018, se ...

  3. electron vue

    vue create project vue add vue-cli-plugin-electron-builder node_modules\@vue\cli-service\lib\config\ ...

  4. 基于camunda开源流程引擎如何实现会签及会签原理解析

    一.背景 市场上比较有名的开源流程引擎有osworkflow.jbpm.activiti.flowable.camunda.由于jbpm.activiti.flowable这几个流程引擎出现的比较早, ...

  5. archlinux-小米pro15_2020款使用archlinux(MX350显卡驱动安装)

    1.官网下载archlinux ISO镜像 https://archlinux.org/download/   使用磁力链接下载 2.使用软碟通将镜像写入U盘,制作成U盘启动盘 3.进入BIOS 关掉 ...

  6. cookie、session、tooken

    一.cookie 的诞生 首先需要知道Http协议的无状态连接的,即这一次请求和上一次请求是没有任何关系的,互不认识的,没有关联的. 服务端,既不知道上一次请求和这一次请求的关联,也无法知道哪一个客户 ...

  7. 迭代阈值收缩算法ISTA,背后的思想与具体推到过程

  8. JDBCToolsV3 :DAO

    编写文件和步骤 ①,bean模块:数据类Course,包含数据的class,封装数据类型; ②,DAO:1)定义对数据的操作接口,及规定标准(包含怎样的操作).例如:CourseDAO数据库操作的接口 ...

  9. 一切皆为字节和字节输出流_OutputStream类&FileOutputStream类介绍

    一切皆为字节 一切文件数据(文本.图片.视频等)在存储时,都是以二进制数字的形式保存,都一个一个的字节,那么传输时一样如此.所以,字节流可以传输任意文件数据.在操作流的时候,我们要时刻明确,无论使用什 ...

  10. 静态同步方法和解决线程安全问题_Lock锁

    静态的同步方法锁对象是谁?不能是thisthis是创建对象之后产生的,静态方法优先于对象静态方法的锁对象是本类的cLass属性-->class文件对象(反射) 卖票案例出现了线程安全问题 卖出了 ...