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. WC2021 题目清单

    Day2 上午 <IOI题型与趣题分析> 来源 题目 完成情况 备注 IOI2002 Day1T1 Frog 已完成 IOI2002 Day1T2 Utopia IOI2002 Day1T ...

  2. DOM树

    一.DOM树一共有12种节点类型,常用的有4种: 1.Document类型(document节点)--DOM的"入口点" 2.Element节点(元素节点)--HTML标签,树构建 ...

  3. 专家PID控制仿真学习

    目录 专家控制 专家系统 专家控制 学习笔记,用于记录学习 资料:<智能控制>(第四版)--刘金琨 专家系统 一.专家系统的定义 专家系统是一类包含知识和推理的智能计算机程序,其内部包含某 ...

  4. 关于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 ...

  5. vs code nginx php xdebug配置

    终于把VSCODE XDEBUG配置搞定了 分享一下 1. VSCODE  安装插件 PHP Debug( Debug supprot for PHP with XDebug); 2. VSCODE ...

  6. Mac安装Brew包管理系统

    Mac安装Brew包管理系统 前言 为什么需要安装brew 作为一个开发人员, 习惯了使用centos的yum和ubuntu的apt, 在mac中有没有这两个工具的平替? 有, 就是Brew. Bre ...

  7. Nacos配置失败(java.lang.IllegalStateException: failed to req API:/nacos/v1/ns/instance after all server)

    解决: nacos服务器过载,可以删掉nacos文件夹下的data文件夹,重新启动.

  8. 常见的git命令和git->github错误

    相关命令 git remote git remote add origin xxx (xxx为仓库链接) 给这个链接取一个名字,为origin git pull git pull <远程主机名& ...

  9. 爬虫(2) - Requests(1) | Requests模块的深度解析

    1.Requests 安装与请求方法 requests官方文档:https://docs.python-requests.org/zh_CN/latest/,官方文档不知道为什么挂了,访问不了.我找了 ...

  10. 服务器与Ajax

    前端相关的技术点 HTML   主要用来实现页面的排版布局 CSS   主要用来实现页面的样式美化 JavaScript   主要用来实现前端功能特效 Ajax基础知识铺垫 客户端与服务器 通信协议( ...