题目:

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. codeforce Gym 100570B ShortestPath Query (最短路SPFA)

    题意:询问单源最短路径,每条边有一个颜色,要求路径上相邻边的颜色不能相同,无重边且边权为正. 题解:因为路径的合法性和边的颜色有关, 所以在做spfa的时候,把边丢到队列中去,松弛的时候注意判断一下颜 ...

  2. 剑指offer38 数字在排序数组中出现的次数

    这种方法没用迭代,而是使用了循环的方式 class Solution { public: int GetNumberOfK(vector<int> data ,int k) { if(da ...

  3. python之*的魔性用法

    1. *在函数中的作用 聚合 在函数定义时聚合 def eat(args): print('我请你吃:',args) eat('蒸羊羔儿') # 输出结果 # 我请你吃: 蒸羊羔儿 打散 在函数执行时 ...

  4. alibaba druid监控页面的使用配置

    一.Maven中添加Durid连接池依赖 <!-- druid连接池 --> <dependency> <groupId>com.alibaba</group ...

  5. Python语言编写脚本时,对日期控件的处理方式

    对日期控件,日期控件的输入控一般是不能手动输入的:把readonly属性去掉就好 其实很简单,我们不去搞时间日期空间,我们把它当成一个普通的input框处理就好了! 但是,很多此类型input框都是禁 ...

  6. 我的offer之路(一)

    目录 1.职业规划. 2.刷题. 3.看书. <剑指offer> <数据结构算法与应用:C++语言描述 > <Effective C++> <C与指针> ...

  7. Ubuntu 18.04 下用命令行安装Sublime

    介绍: 添加来源: $ wget -qO - https://download.sublimetext.com/sublimehq-pub.gpg | sudo apt-key add - $ sud ...

  8. vue-highlightjs的使用小结

    万能的github真主,让我们强大!在vue的项目中想使用highlight.js这样的代码高亮?有人帮助我们实现了vue-highlightjs 安装 yarn add highlight.js - ...

  9. numpy 三个点的使用[...]

    numpy [...]语法简单使用 Python numpy中切片功能与列表切片类似,但功能更加强大 本文主讲numpy中[...]的简单使用,后续工作继续补充. import numpy >& ...

  10. python中打印金字塔和九九乘法表的几种方法

    # 打印九九乘法表for i in range(1,10): for j in range(1,i+1): # x=i*j # print(i,'*',j,'=',x,end=' ') print(' ...