[POJ 1151] Atlantis
一样的题:HDU 1542
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 18148 | Accepted: 6902 |
Description
Input
The input file is terminated by a line containing a single 0. Don't process it.
Output
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
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3193 | Accepted: 1627 |
Description
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
Output
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的更多相关文章
- POJ 1151 Atlantis(扫描线)
题目原链接:http://poj.org/problem?id=1151 题目中文翻译: POJ 1151 Atlantis Time Limit: 1000MS Memory Limit: 10 ...
- POJ 1151 Atlantis 矩形面积求交/线段树扫描线
Atlantis 题目连接 http://poj.org/problem?id=1151 Description here are several ancient Greek texts that c ...
- POJ 1151 Atlantis(线段树-扫描线,矩形面积并)
题目链接:http://poj.org/problem?id=1151 题目大意:坐标轴上给你n个矩形, 问这n个矩形覆盖的面积 题目思路:矩形面积并. 代码如下: #include<stdio ...
- POJ 1151 Atlantis (扫描线+线段树)
题目链接:http://poj.org/problem?id=1151 题意是平面上给你n个矩形,让你求矩形的面积并. 首先学一下什么是扫描线:http://www.cnblogs.com/scau2 ...
- hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- POJ 1151 Atlantis 线段树+离散化+扫描线
这次是求矩形面积并 /* Problem: 1151 User: 96655 Memory: 716K Time: 0MS Language: G++ Result: Accepted */ #inc ...
- POJ 1151 Atlantis 线段树求矩形面积并 方法详解
第一次做线段树扫描法的题,网搜各种讲解,发现大多数都讲得太过简洁,不是太容易理解.所以自己打算写一个详细的.看完必会o(∩_∩)o 顾名思义,扫描法就是用一根想象中的线扫过所有矩形,在写代码的过程中, ...
- POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)
求矩阵的面积并 采用的是区间更新 #include <iostream> #include <stdio.h> #include <string.h> #inclu ...
- poj 1151 Atlantis (离散化 + 扫描线 + 线段树 矩形面积并)
题目链接题意:给定n个矩形,求面积并,分别给矩形左上角的坐标和右上角的坐标. 分析: 映射到y轴,并且记录下每个的y坐标,并对y坐标进行离散. 然后按照x从左向右扫描. #include <io ...
随机推荐
- SQL 不同的数据类型
SQL 不同的数据类型 1.SQL TEXT 2.SQL VARCHAR(SIZE) VARCHAR(X) Case: user name, email, country, subject, pass ...
- Codevs 1684 垃圾陷阱
1684 垃圾陷阱 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 卡门--农夫约翰极其珍视的一条Holsteins奶牛--已经落了 ...
- 解决fontawesome-webfont 被拦截的问题
我们最近的项目是java web项目,前端采用了fontawesome-webfont,项目部署之后,图标都显示不出来,在网上学习了一大圈,找到了一个解决方案可行: web.xml中配置 ...
- div重叠不变形
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- Node.js和MongoDB - MongoJS入门
第一次尝试翻译外国牛人的博文,希望大家喜欢. 本文源码详见:https://github.com/njaulj/mongojs 一点都不夸大的说,近年来node.js和mongodb的确是大放异彩,在 ...
- Style 的优先级
Dependency Property(简称DP)是WPF的核心,Style就是基于Dependency Property的,关于DP的内幕,请参见深入WPF--依赖属性.Style中的Setter就 ...
- Oracle的rowid结构解析
SQL> select rowid,deptno from dept; ROWID DEPTNO ------------------ ---------- A ...
- MaskedTextBox控件实现输入验证
Mask属性可以验证用户在文本中输入数据的格式 this.maskedTextBox1.Mask = "000000-00000000-000A";//身份证号码18位 this. ...
- 【JPA】两种不同的实现jpa的配置方法
两种不同的实现jpa的配置方法 第一种: com.mchange.v2.c3p0.ComboPooledDataSource datasource.connection.driver_class=co ...
- Pair Project: Elevator Scheduler [电梯调度算法的实现和测试]:思考题——谢勤政11061197
第一题: 大楼里面的电梯一般分区域,或考虑思考题第四题的情况,运行楼层不一样的电梯属于不同的区域.然后在接口IRequest和IPassenger还有IElevator里面都加上int area这个属 ...