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. JSON 数据的系统解析

    - (IBAction)jsonSystemButtonDidClicked:(UIButton *)sender { self.JSONArray = [NSMutableArray array]; ...

  2. [置顶] JQuery插件学习教程

    这是JQuery其它常用插件的视频教程,包括validate插件,.comet插件等.同时有大量实例项目,如果你是喜欢JQuery的童鞋千万不要错过. 教程的内容有: 1_validate插件(1) ...

  3. [Angular 2] 9. Replace ng-modle with #ref & events

    Let's say you want to write a simple data bing app. when you type in a text box, somewhere in the ap ...

  4. [React + webpack] hjs-webpack

    You can easily spend hours configuring the perfect dev environment with all the latest hotness like ...

  5. 嵌入式Linux系统Bootloader启动调试技术(回想)

    嵌入式系统搭建过程中,对于系统平台搭建project师最初的一步一般是移植Bootloader ,当然移植有几个级别,通常最常见的是參考的EVM 的硬件有了改动(如更改了FLASH ,更改了SDRAM ...

  6. 第二篇:从 GPU 的角度理解并行计算

    前言 本文从使用 GPU 编程技术的角度来了解计算中并行实现的方法思路. 并行计算中需要考虑的三个重要问题 1. 同步问题 在操作系统原理的相关课程中我们学习过进程间的死锁问题,以及由于资源共享带来的 ...

  7. 关于String的hashCode

    String str=new String("abc"); String str2="abc"; System.out.println(str.hashCode ...

  8. +load,+initialize原理

    +load,+initialize原理 1.load 父类的load方法在子类load方法之前调用,分类的load方法在原来类load方法之后调用,依赖类的load方法会在自己之前调用,总之所有的类的 ...

  9. 转换字符串格式,可用于sql in

    /// <summary> /// 转换字符串格式,可用于sql in /// </summary> /// <param name="lst"> ...

  10. oracle中创建表时添加注释

    CREATE TABLE t1(id varchar2(32) primary key,name VARCHAR2(32) ,age VARCHAR2(32) );comment on column ...