题目:

Problem Description

There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even include maps of parts of the island. But unfortunately, these maps describe different regions of Atlantis. Your friend Bill has to know the total area for which maps exist. You (unwisely) volunteered to write a program that calculates this quantity.

Input

The input file consists of several test cases. Each test case starts with a line containing a single integer n (1<=n<=100) of available maps. The n following lines describe one map each. Each of these lines contains four numbers x1;y1;x2;y2 (0<=x1<x2<=100000;0<=y1<y2<=100000), not necessarily integers. The values (x1; y1) and (x2;y2) are the coordinates of the top-left resp. bottom-right corner of the mapped area.

The input file is terminated by a line containing a single 0. Don’t process it.

Output

For each test case, your program should output one section. The first line of each section must be “Test case #k”, where k is the number of the test case (starting with 1). The second one must be “Total explored area: a”, where a is the total explored area (i.e. the area of the union of all rectangles in this test case), printed exact to two digits to the right of the decimal point.

Output a blank line after each test case.

Sample Input

2
10 10 20 20
15 15 25 25.5
0

Sample Output

Test case #1 Total explored area: 180.00

题解:

扫描线的模板题目···具体见http://blog.csdn.net/lwt36/article/details/48908031,%%%%%%%

唯一需要注意的是线段树的l到r区间其实代表的是离散化后l到r+1这一段区间·····,因为最小的单位区间表示的是一条线段···而不是一个点

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int N=;
struct node
{
double l,r,h;
int op;
}line[N];
int n,m,tot,cnt[N*],T;
double ans,sum[N*],b[N];
inline bool cmp(node a,node b)
{
return a.h<b.h;
}
inline void update(int k,int l,int r)
{
if(cnt[k]) sum[k]=b[r+]-b[l];
else if(l==r) sum[k]=;
else sum[k]=sum[k*]+sum[k*+];
}
inline void modify(int k,int l,int r,int x,int y,int w)
{
if(l>=x&&r<=y)
{
cnt[k]+=w;update(k,l,r);
return;
}
int mid=(l+r)/;
if(x<=mid) modify(k*,l,mid,x,y,w);
if(y>mid) modify(k*+,mid+,r,x,y,w);
update(k,l,r);
}
int main()
{
//freopen("a.in","r",stdin);
while(true)
{
scanf("%d",&n);
if(n==) break;T++;
double X1,X2,Y1,Y2;tot=;
for(int i=;i<=n;i++)
{
scanf("%lf%lf%lf%lf",&X1,&Y1,&X2,&Y2);
line[++tot].l=X1,line[tot].r=X2,line[tot].h=Y1;line[tot].op=;b[tot]=X1;
line[++tot].l=X1,line[tot].r=X2,line[tot].h=Y2;line[tot].op=-;b[tot]=X2;
}
sort(line+,line+tot+,cmp);
sort(b+,b+tot+);
m=unique(b+,b+tot+)-b-;ans=;
memset(cnt,,sizeof(cnt));
memset(sum,,sizeof(sum));
for(int i=;i<tot;i++)
{
int Le=lower_bound(b+,b+m+,line[i].l)-b;
int Ri=lower_bound(b+,b+m+,line[i].r)-b;
if(Le<Ri) modify(,,m,Le,Ri-,line[i].op);
ans+=sum[]*(line[i+].h-line[i].h);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",T,ans);
}
return ;
}
 

算法复习——扫描线(hdu1542)的更多相关文章

  1. C#冒泡算法复习

    C#冒泡算法复习 冒泡算法的意思:每一趟找到一个最小或最大的数放到最后面,比较总数的n-1次(因为比较是2个双双比较的) 第一层循环表示进行比较的次数,总共要比较(数的)-1次 (因为比较是2个双双比 ...

  2. C语言排序算法复习

    排序算法有很多种,这里在复习和分析的基础上,做一个自己的总结: 首先要知道有哪些排序算法,google一下,有云C语言7大经典排序算法(也有8大).主要包括冒泡排序,快速排序,选择排序,插入排序,希尔 ...

  3. KMP算法复习【+继续学习】

    离NOIP还剩12天,本蒟蒻开始准备复习了. 先来个KMP[似乎我并没有写过KMP的blog] KMP KMP算法是解决字符串匹配问题的一个算法,主要是单对单的字符串匹配加速,时间复杂度O(m + n ...

  4. 算法复习周------“动态规划之‘最长公共子序列’”&&《计蒜课》---最长公共子串题解

    问题描述: 这个问题其实很容易理解.就是给你两个序列X={x1,x2,x3......xm} Y={y1,y2,y3......ym},要求找出X和Y的一个最长的公共子序列. 例:Xi={A, B, ...

  5. K-Means聚类和EM算法复习总结

    摘要: 1.算法概述 2.算法推导 3.算法特性及优缺点 4.注意事项 5.实现和具体例子 6.适用场合 内容: 1.算法概述 k-means算法是一种得到最广泛使用的聚类算法. 它是将各个聚类子集内 ...

  6. 通过“回文字算法”复习C++语言。

    一.什么是回文字 给定一个字符串,从前往后读和从后往前读,字符串序列不变.例如,河北省农村信用社的客服电话是“96369”,无论从后往前读,还是从前后往后读,各个字符出现的位置不变. 二.功能实现 ( ...

  7. 【转】常用算法复习及实现(C++版)

    一.霍夫曼树实现 给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree).哈夫曼树是带权路径长度最短的树,权值较大 ...

  8. OI算法复习

    搜集一些算法,赛前背一背有好处的 转自各大网站 前排感谢:hzwer.风了咕凉 前辈...Orz 快速读入: int read() { ,f=;char ch=getchar(); ;ch=getch ...

  9. OI算法复习汇总

    各大排序 图论: spfa floyd dijkstra *拉普拉斯矩阵 hash表 拓扑排序 哈夫曼算法 匈牙利算法 分块法 二分法 费马小定理: a^(p-1) ≡1(mod p) 网络流 二分图 ...

随机推荐

  1. 认识CoreData—初识CoreData

    http://www.cocoachina.com/ios/20160729/17245.html 这段时间公司一直比较忙,和组里小伙伴一起把公司项目按照之前逻辑重写了一下.由于项目比较大,还要兼顾之 ...

  2. 2018.2.12 PHP 如何读取一亿行的大文件

    PHP 如何读取一亿行的大文件 我们可能在很多场景下需要用 PHP 读取大文件,之后进行处理,如果你没有相关的经验可以看下,希望能给你带来一些启发. 模拟场景 我们有一个 1亿 行,大小大概为 3G ...

  3. 解决TS报错Property 'style' does not exist on type 'Element'

    在使用queryselector获取一个dom元素,编译时却报错说property 'style' does not exist on type 'element'. 原因:这是typescript的 ...

  4. Java中的ArrayList类和LinkedList

    集合的体系: ----------| Collection 单列集合的根接口----------------| List 如果实现了List接口的集合类,具备的特点: 有序,可重复.--------- ...

  5. 括号匹配性检测C语言实现

    #include <stdio.h> #define SIMPLE_KUOHAO "(()1231qeqw)(@#$)" #define COMPLEX_KUOHAO ...

  6. QQ 发送邮件

    之前也发布过一篇QQ发邮件的,后来那种方法在阿里云服务器中报错了,查了好久才发现,是阿里云的服务器把 25 端口 给封杀了.现在重新做了个功能. public static string UserNa ...

  7. 【费用流】bzoj1834: [ZJOI2010]network 网络扩容

    还是稍微记一下这个拆点模型吧 Description 给定一张有向图,每条边都有一个容量C和一个扩容费用W.这里扩容费用是指将容量扩大1所需的费用. 求:  1.在不扩容的情况下,1到N的最大流:  ...

  8. 忘记root密码怎么办-单用户模式修改root密码

    忘记root密码怎么办-单用户模式修改root密码================================= 1,开机3秒内按下向下的方向键,目的是为了不让它进入系统,而是停留在开机界面. 2 ...

  9. windows charles 抓包https请求

    charles证书     2.设置host和端口 3.浏览器访问即可抓到https的请求  

  10. 如何封装RESTful Web Service

    所谓Web Service是一个平台独立的,低耦合的,自包含的.可编程的Web应用程序,有了Web Service异构系统之间就可以通过XML或JSON来交换数据,这样就可以用于开发分布式的互操作的应 ...