一样的题:HDU 1542

Atlantis
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18148   Accepted: 6902

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 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 求矩形并、线段树+扫描线
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 210 int n;
int tot;
double sum;
double y[N];
double yl[N<<];
double yr[N<<];
int cover[N<<];
double height[N<<];
struct X
{
int pos;
double x,y1,y2;
bool operator <(const X &t)const
{
return x<t.x;
}
}x[N]; void pushup(int l,int r,int rt)
{
if(cover[rt]>) height[rt]=yr[rt]-yl[rt];
else if(l+==r) height[rt]=;
else height[rt]=height[rt<<]+height[rt<<|];
}
void build(int l,int r,int rt)
{
cover[rt]=;
yl[rt]=y[l];
yr[rt]=y[r];
if(l+==r) return;
int m=(l+r)>>;
build(l,m,rt<<);
build(m,r,rt<<|);
}
void update(int l,int r,int rt,double L,double R,int c)
{
if(yl[rt]==L && yr[rt]==R)
{
cover[rt]+=c;
pushup(l,r,rt);
return;
}
int m=(l+r)>>;
if(R<=y[m]) update(l,m,rt<<,L,R,c);
else if(L>=y[m]) update(m,r,rt<<|,L,R,c);
else
{
update(l,m,rt<<,L,y[m],c);
update(m,r,rt<<|,y[m],R,c);
}
pushup(l,r,rt);
}
int main()
{
int i,iCase=;
while(scanf("%d",&n),n)
{
tot=;
sum=;
memset(height,,sizeof(height));
for(i=;i<n;i++)
{
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
y[tot]=y1;
x[tot].x=x1;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=;
y[tot]=y2;
x[tot].x=x2;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=-;
}
sort(x,x+tot);
sort(y,y+tot);
build(,tot-,);
for(i=;i<tot;i++)
{
update(,tot-,,x[i-].y1,x[i-].y2,x[i-].pos);
sum+=height[]*(x[i].x-x[i-].x);
}
printf("Test case #%d\nTotal explored area: %.2f\n\n",iCase++,sum);
}
return ;
}

一样的题:POJ 1389

Area of Simple Polygons
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 3193   Accepted: 1627

Description

There are N, 1 <= N <= 1,000 rectangles in the 2-D xy-plane. The four sides of a rectangle are horizontal or vertical line segments. Rectangles are defined by their lower-left and upper-right corner points. Each corner point is a pair of two nonnegative integers in the range of 0 through 50,000 indicating its x and y coordinates.

Assume that the contour of their union is defi ned by a set S of segments. We can use a subset of S to construct simple polygon(s). Please report the total area of the polygon(s) constructed by the subset of S. The area should be as large as possible. In a 2-D xy-plane, a polygon is defined by a finite set of segments such that every segment extreme (or endpoint) is shared by exactly two edges and no subsets of edges has the same property. The segments are edges and their extremes are the vertices of the polygon. A polygon is simple if there is no pair of nonconsecutive edges sharing a point.

Example: Consider the following three rectangles:

rectangle 1: < (0, 0) (4, 4) >,

rectangle 2: < (1, 1) (5, 2) >,

rectangle 3: < (1, 1) (2, 5) >.

The total area of all simple polygons constructed by these rectangles is 18.

Input

The input consists of multiple test cases. A line of 4 -1's separates each test case. An extra line of 4 -1's marks the end of the input. In each test case, the rectangles are given one by one in a line. In each line for a rectangle, 4 non-negative integers are given. The first two are the x and y coordinates of the lower-left corner. The next two are the x and y coordinates of the upper-right corner.

Output

For each test case, output the total area of all simple polygons in a line. 

Sample Input

0 0 4 4
1 1 5 2
1 1 2 5
-1 -1 -1 -1
0 0 2 2
1 1 3 3
2 2 4 4
-1 -1 -1 -1
-1 -1 -1 -1 

Sample Output

18
10
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 210 int n;
int tot;
double sum;
double y[N];
double yl[N<<];
double yr[N<<];
int cover[N<<];
double height[N<<];
struct X
{
int pos;
double x,y1,y2;
bool operator <(const X &t)const
{
return x<t.x;
}
}x[N]; void pushup(int l,int r,int rt)
{
if(cover[rt]>) height[rt]=yr[rt]-yl[rt];
else if(l+==r) height[rt]=;
else height[rt]=height[rt<<]+height[rt<<|];
}
void build(int l,int r,int rt)
{
cover[rt]=;
yl[rt]=y[l];
yr[rt]=y[r];
if(l+==r) return;
int m=(l+r)>>;
build(l,m,rt<<);
build(m,r,rt<<|);
}
void update(int l,int r,int rt,double L,double R,int c)
{
if(yl[rt]==L && yr[rt]==R)
{
cover[rt]+=c;
pushup(l,r,rt);
return;
}
int m=(l+r)>>;
if(R<=y[m]) update(l,m,rt<<,L,R,c);
else if(L>=y[m]) update(m,r,rt<<|,L,R,c);
else
{
update(l,m,rt<<,L,y[m],c);
update(m,r,rt<<|,y[m],R,c);
}
pushup(l,r,rt);
}
int main()
{
int i;
while()
{
tot=;
sum=;
memset(height,,sizeof(height));
n=;
double x1,y1,x2,y2;
while(scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2) && x1+x2+y1+y2!=-)
{
y[tot]=y1;
x[tot].x=x1;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=;
y[tot]=y2;
x[tot].x=x2;x[tot].y1=y1;x[tot].y2=y2;x[tot++].pos=-;
n++;
}
if(n==) break;
sort(x,x+tot);
sort(y,y+tot);
build(,tot-,);
for(i=;i<tot;i++)
{
update(,tot-,,x[i-].y1,x[i-].y2,x[i-].pos);
sum+=height[]*(x[i].x-x[i-].x);
}
printf("%.0f\n",sum);
}
return ;
}

[POJ 1151] Atlantis的更多相关文章

  1. POJ 1151 Atlantis(扫描线)

    题目原链接:http://poj.org/problem?id=1151 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS   Memory Limit: 10 ...

  2. POJ 1151 Atlantis 矩形面积求交/线段树扫描线

    Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...

  3. POJ 1151 Atlantis(线段树-扫描线,矩形面积并)

    题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...

  4. POJ 1151 Atlantis (扫描线+线段树)

    题目链接:http://poj.org/problem?id=1151 题意是平面上给你n个矩形,让你求矩形的面积并. 首先学一下什么是扫描线:http://www.cnblogs.com/scau2 ...

  5. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  6. POJ 1151 Atlantis 线段树+离散化+扫描线

    这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...

  7. POJ 1151 Atlantis 线段树求矩形面积并 方法详解

    第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...

  8. POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)

    求矩阵的面积并 采用的是区间更新 #include <iostream> #include <stdio.h> #include <string.h> #inclu ...

  9. poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)

    题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 映射到y轴,并且记录下每个的y坐标,并对y坐标进行离散. 然后按照x从左向右扫描. #include <io ...

随机推荐

  1. [getLongestLength] 加和为0的最长子串长度

    点击这里查看原文 假设一个数组仅仅由1和-1组成,求该数组的和为0的最长子串的长度. 例如: {1,-1,1,-1,1,1,1} 输出:4. 昨天机试的时候做到这道题,不会做,今天思考一下. 普通的解 ...

  2. strcpy函数导致release版程序崩溃

    最近在写一个读取模型文件的小程序.很随意的使用了strcpy函数进行char字符数组的拷贝,这个数组是需要传递给PostMessage作为WPARAM的参数.代码部分如下: char pStrCurr ...

  3. ubuntu12.10设置禁止锁屏和屏幕常亮

    1.System Settings -> Brightness and Lock -> Turn off screen... set to "Never" 进入ubun ...

  4. jQuery去掉导航分割线的最后一条竖线

    #top #navigation ul li { float:left; width:120px; background:url(../images/navline.jpg) no-repeat 11 ...

  5. Hash算法初见

    hash算法 (hashmap 实现原理)   Hash ,一般翻译做“ 散列” ,也有直接音译为“ 哈希” 的,就是把任意长度的输入(又叫做预映射, pre-image ),通过散列算法,变换成固定 ...

  6. PHP常见算法-面试篇(1)

    1.冒泡排序 思路分析:在要排序的一组数中,对当前还未排好的序列,从前往后对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒.即,每当两相邻的数比较后发现它们的排序与排序要求相反时,就将 ...

  7. 【BZOJ】1925: [Sdoi2010]地精部落 DP+滚动数组

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1925 题意:输入一个数N(1 <= N <= 4200),问将这些数排列成折线 ...

  8. 2014年度辛星html教程夏季版第五节

    如果读者是一位后台开发者,那么肯定会知道什么叫表单,这里我们就介绍一下前台如何使用表单,表单的使用也是我们编写网页的必须经历的一关,而且,表单也往往是我们网站的漏洞和弱点出现的地方. ******** ...

  9. 素数筛&&欧拉筛

    折腾了一晚上很水的数论,整个人都萌萌哒 主要看了欧拉筛和素数筛的O(n)的算法 这个比那个一长串英文名的算法的优势在于没有多次计算一个数,也就是说一个数只筛了一次,主要是在%==0之后跳出实现的,具体 ...

  10. 给JavaScript初学者的24条最佳实践(转:http://www.cnblogs.com/yanhaijing/p/3465237.html)

    作为“30 HTML和CSS最佳实践”的后续,本周,我们将回顾JavaScript的知识 !如果你看完了下面的内容,请务必让我们知道你掌握的小技巧! 1.使用 === 代替 == JavaScript ...