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. Vue基础篇之 插槽 slot

  2. 2021.05.03【NOIP提高B组】模拟 总结

    比较水的一场比赛,却不能 AK T1 有 \(n\) 次,每次给 \(A_i,B_i\) 问以 \(i\) 结尾的 \(A,B\) 的匹配中最大和的最小值 问最大和的最小值,却不用二分. 如果暴力排序 ...

  3. 总结 到 GDOI 2021 这个阶段

    截止本蒟蒻第一次体验省选(虽然是普及组) 本蒟蒻已经有了许多收获,却也有很多不足 优点 对一些学过的知识掌握还行 没了 缺点 会却做不出来 有一些题不难,却想不到正解 如 Day2 T1 ,就是移一下 ...

  4. 10分钟快速部署camunda BPM开源版

    安装部署Camunda BPM有多种方式,基于Camunda独立web应用程序安装部署是最简单的一种方式,您只需要有tomcat即可. 本文档将指导您安装和配置Camunda独立web应用程序,快速体 ...

  5. VMware 安装 Anolis OS

    安装时参考的以下文章 VMware Workstation Pro 虚拟机安装 VMware Workstation Pro 安装 龙蜥操作系统(Anolis OS) Anolis OS 8 安装指南 ...

  6. springboot执行流程

    构造方法初始化,创建一个新的实例,这个应用程序的上下文要从指定的来源加载bean public SpringApplication(ResourceLoaderresourceLoader,Class ...

  7. java: 程序包org.springframework.boot不存在

    如果你的settings中的maven配置没问题的话,尝试下面这个 在控制台输入  mvn idea:idea  重构一下

  8. 换根 DP 学习笔记

    前言 没脑子选手什么都不会. 正文 先来写一下换根 DP 的特点或应用方面: 不同的点作为树的根节点,答案不一样. 求解答案时要求出每一个节点的信息. 无法通过一次搜索完成答案的求解,因为一次搜索只能 ...

  9. 2019 CSP-J 初赛解析

    题面,成绩不是真实水平,就挑重点说一说 老师给的解析 T5 这是二分查找,属于是我的代码理解不太对 我的理解 #include<iostream> using namespace std; ...

  10. 159_模型_Power BI 地理分析之形状地图

    159_模型_Power BI 地理分析之形状地图 声明以下地图元素仅供学习交流所用,如需地图公开使用请提前做好报审工作. 一.背景 当企业的体量达到一定体量的时候,保持稳定的增长是非常重要的事情.本 ...