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. [React] Intro to inline styles in React components

    React lets you use "inline styles" to style your components; inline styles in React are ju ...

  2. [AngualrJS + Webpack] Production Source Maps

    When you uglify your Angular code with Webpack's uglify plugin, debugging your application can be a ...

  3. DEPENDENT SUBQUERY” 和 “SUBQUERY”

    http://blog.163.com/li_hx/blog/static/183991413201642410122327/ mysql> CREATE TABLE t1 (a INT, b ...

  4. iOS-UIResponse之事件响应链及其事件传递

    UIResponse之事件响应链及其事件传递 我们的App与用户进行交互,基本上是依赖于各种各样的事件.一个视图是一个事件响应者,可以处理点击等事件,而这些事件就是在UIResponder类中定义的. ...

  5. Ubuntu上安装jdk,Jboss

    Ubuntu上安装jdk 1.使用wget命令或sft方式从oracle官方下载tar.gz格式的jdk1.7,由于ubuntu不支持rpm安装,需要转换,所以不选择rpm格式的jdk 2.使用tar ...

  6. Android 使用Android Studio + Gradle 或 命令行 进行apk签名打包

    官方文档:https://developer.Android.com/tools/publishing/app-signing.html 1. 默认为debug mode,使用的签名文件在: $HOM ...

  7. 屏蔽Codeforces做题时的Problem tags提示

    当在Codeforces上做题的时,有时会无意撇到右侧的Problem tags边栏,但是原本并不希望能够看到它. 能否把它屏蔽了呢?答案是显然的,我们只需要加一段很短的CSS即可. span.tag ...

  8. jQuery失去焦点的时候注册验证

    //注册验证$('form :input').blur(function () { if ($("#txtName").val() == "") { $(&qu ...

  9. iBatis 的条件查询

    之类以传入ID进行举例 Student.xml 在里面设置一个ID的标志位,设置类型 <select id="selectAllStudentByid" parameterC ...

  10. 表达式:使用API创建表达式树(2)

    一.BlockExpression类:表式一个包含可在其中定义变量的表达式序列的块.是一组表达式,类似于多个委托的 += 后的效果,其返回表达式是最后一个表达式决定.以下是BlockExpressio ...