题目:

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. 小技巧:unicode RLO

    unicode 控制字符 RLO 可以将位于其后的文字翻转. 于是可以被病毒利用. 如图 重命名文件,在gpj前插入unicode RLO,之后若不小心,可能会被欺骗,误以为是jpg文件. 如果修改程 ...

  2. LINQ中AsEnumerable与AsQueryable的区别

    AsEnumerable将一个序列向上转换为一个IEnumerable, 强制将Enumerable类下面的查询操作符绑定到后续的子查询当中:AsQueryable将一个序列向下转换为一个IQuery ...

  3. Bootstrap历练实例:语境色彩的面板

    带语境色彩的面板 使用语境状态类 panel-primary.panel-success.panel-info.panel-warning.panel-danger,来设置带语境色彩的面板,实例如下: ...

  4. Bootstrap历练实例:带表格的面板

    带表格的面板 为了在面板中创建一个无边框的表格,我们可以在面板中使用 class .table.假设有个 <div> 包含 .panel-body,我们可以向表格的顶部添加额外的边框用来分 ...

  5. sql*plus常用指令介紹

    sql*plus常用指令介紹 1.用set指令來設定SQL*Plus的環境參數值 格式: Set 環境參數名 環境參數值 ex:set feedback on set feedback 8.用show ...

  6. Codevs1080 线段树练习

    题目描述 Description 一行N个方格,开始每个格子里都有一个整数.现在动态地提出一些问题和修改:提问的形式是求某一个特定的子区间[a,b]中所有元素的和:修改的规则是指定某一个格子x,加上或 ...

  7. CF-1143D. The Beatles

    题意:有间隔为k的n个点在数轴上,下标为 \(1,k+1, 2*k+1,\cdots (n-1)*k+1\) 首尾相接.设起点为s,步长为L,而现在只知道s距离最近的点的距离为a,和(s+L)距离最近 ...

  8. centos6启动故障排除

    centos6中boot文件被全部删除的故障排除 /boot文件里关于启动的核心文件有三个,/vmlinuz-2.6.32-696.e16.x86_64,initramfs-2.6.32-696.el ...

  9. 蓝牙stack bluez学习(1)Stack Architecture

    Bluez支持的features Core Specification 4.2 (GAP, L2CAP, RFCOMM, SDP, GATT) Classic Bluetooth (BR/EDR) B ...

  10. 怎样处理jmeter中文乱码

    jmeter返回 中文乱码: 1.在jmeter的bin目录下,找到jmeter的配置文件,jmeter.properties,然后把 sampleresult.default.encoding=UT ...