poj1151:http://poj.org/problem?id=1151

题意:求矩形面积的并
题解:扫描线加线段树

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;//注意这里的初始化,不能开小了,是200条边
int num;//记录x坐标的个数
struct Node{
double l;//边的左端点
double r;//边的又端点
int tp;//是出度边还是入度边
double y;//边的纵坐标
bool operator <(Node a) const{//按照纵坐标给边进行排序,一个比较器
return y<a.y;
}
}line[*maxn];
double arr[*maxn];//用于离散化
struct Edge{
int left;
int right;
int flag;//标记该点是加入还是删除
double sum;
}node1[maxn*];
void build(int l,int r,int idx){//建树
node1[idx].left=l;
node1[idx].right=r;
if(l==r){
node1[idx].flag=;
node1[idx].sum=;
return;
}
int mid=(l+r)/;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
node1[idx].sum=node1[idx<<].sum+node1[idx<<|].sum;
}
void update(int l,int r,int f,int idx){//更新
if(node1[idx].left==node1[idx].right){
node1[idx].flag+=f;
if(node1[idx].flag)node1[idx].sum=arr[node1[idx].right+]-arr[node1[idx].left];//注意这里的家一操作
if(!node1[idx].flag)node1[idx].sum=;
return ;
}
int mid=(node1[idx].left+node1[idx].right)/;
if(mid>=r)update(l,r,f,idx<<);
else if(mid<l)update(l,r,f,idx<<|);
else{
update(l,mid,f,idx<<);
update(mid+,r,f,idx<<|);
}
node1[idx].sum=node1[idx<<].sum+node1[idx<<|].sum;//pushup上去
}
int binaryserach(double x){//二分查找,也可以写成递归的形是
int l,r,mid;
l=,r=num+;
while (r-l>){
mid=(l+r)>>;
if (arr[mid]<=x) l=mid;
else r=mid;
}
return l;
}
int main(){
int n;double x1,y1,x2,y2,ans;
int t=;
while(~scanf("%d",&n)&&n){
num=;
for(int i=;i<=n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
line[*i-].l=x1;line[*i-].r=x2;line[*i-].y=y1;line[*i-].tp=;
line[*i].l=x1;line[*i].r=x2;line[*i].y=y2;line[*i].tp=-;
arr[++num]=x1;arr[++num]=x2;//离散化
}
sort(arr+,arr+num+);//排序
n*=;ans=;
sort(line+,line+n+);
build(,n,);
for(int i=;i<=n;i++){
ans+=node1[].sum*(line[i].y-line[i-].y);
int l=binaryserach(line[i].l);
int r=binaryserach(line[i].r)-;//注意这里是要得到区间,不是单个的点,所以要加1
update(l,r,line[i].tp,);
}
printf("Test case #%d\n",t++);
printf("Total explored area: %.2f\n\n",ans);
}
}

Atlantis的更多相关文章

  1. [POJ1151]Atlantis

    [POJ1151]Atlantis 试题描述 There are several ancient Greek texts that contain descriptions of the fabled ...

  2. 线段树---Atlantis

    题目网址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=110064#problem/A Description There are se ...

  3. hdu 1542 Atlantis(线段树,扫描线)

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

  4. 【POJ】1151 Atlantis(线段树)

    http://poj.org/problem?id=1151 经典矩形面积并吧.....很简单我就不说了... 有个很神的地方,我脑残没想到: 将线段变成点啊QAQ这样方便计算了啊 还有个很坑的地方, ...

  5. HDU 1542 Atlantis(线段树扫描线+离散化求面积的并)

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

  6. POJ 1542 Atlantis(线段树 面积 并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1542 参考网址:http://blog.csdn.net/sunmenggmail/article/d ...

  7. [POJ 1151] Atlantis

    一样的题:HDU 1542 Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18148   Accepted ...

  8. 【HDU 1542】Atlantis 矩形面积并(线段树,扫描法)

    [题目] Atlantis Problem Description There are several ancient Greek texts that contain descriptions of ...

  9. 【POJ1151】【扫描线+线段树】Atlantis

    Description There are several ancient Greek texts that contain descriptions of the fabled island Atl ...

  10. hdu 1542 Atlantis(段树&amp;扫描线&amp;面积和)

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

随机推荐

  1. Notepad++中的复活节彩蛋(easter egg)

    Notepad++是windows下的一个强大的文本编辑器; 它免费,开源, 比windows自带的记事本强大百倍; 我自己的感觉: 1.打开超大的文本文件,比方100M大小的文本格式的数据,记事本会 ...

  2. 刚才建立一个 swift 中文讨论社区,欢迎大家參与讨论

    http://www.chinaswift.me 主要目的是收集 swift学习资源

  3. [Javascript] Creating an Immutable Object Graph with Immutable.js Map()

    Learn how to create an Immutable.Map() through plain Javascript object construction and also via arr ...

  4. spring-data-redis工程

    官方文档:http://docs.spring.io/spring-data/data-redis/docs/current/reference/html/redis.html The Spring ...

  5. async 与 await异步编程活用基础

    [本文转自:http://www.cnblogs.com/x-xk/archive/2013/06/05/3118005.html  作者:肅] 好久没写博客了,时隔5个月,奉上一篇精心准备的文章,希 ...

  6. RxJava使用场景小结

    一.Scheduler线程切换 这种场景经常会在“后台线程取数据,主线程展示”的模式中看见 Observable.just(1, 2, 3, 4) .subscribeOn(Schedulers.io ...

  7. Java基础知识强化06:使用BigDecimal计算阶乘1+1/2!+1/3!+……

    package himi.hebao04; import java.math.BigDecimal; public class TestDemo07 { public static void main ...

  8. node 搭建开发框架express

    参考地址: http://www.itnose.net/detail/6095003.html 开发环境 E:\project> node -v v0.10.30 E:\project> ...

  9. MVC ViewData和ViewBag

        视图数据可以通过ViewBag属性访问,它主要是为了从Controller到view进行传值用的,类似有所使用的ViewData[] 字典类.对于ViewBag是如此的强大,意味着你能动态的s ...

  10. 请教如何实现UITextField值变化的实时监视

    上网搜索以后发现基本的处理方法大概有三种1.KVO方式[textField addObserver:self forKeyPath:@"text" options:0 contex ...