/*线段树扫描线问题,感觉可以作为这类问题的模板。问过师兄以后才过的。。。

按y轴建线段树。*/

struct node{int l, r, c;    //c表示被覆盖的情况,不为0表示被覆盖    double sum, len;    //len表示当前段的长度,sum表示有效长度。就是可以计入求面积的长度}node[N<<2];

struct line{double x;  //记录x坐标    double y1;   double y2;    //y1,y2用来记录平行y轴的线段的两个端点,    int flag;    //标记是左边的边还是右边的边}l[N];

//My Code:

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define L(t) t << 1#define R(t) t << 1 | 1

using namespace std;

const int N = 210;

struct node{int l, r, c;double sum, len;}node[N<<2];

struct line{double x;double y1;double y2;int flag;}l[N];

double y[N];

bool cmp(line a, line b){return a.x < b.x;}

void creat(int t, int l, int r){    node[t].l = l;    node[t].r = r;    node[t].c = 0;    node[t].sum = 0;    node[t].len = y[r] - y[l];if(l + 1 == r)    return ;int mid = (l + r) >> 1;    creat(L(t), l, mid);    creat(R(t), mid, r);}

void updata_sum(int t){if(node[t].c > 0)    node[t].sum = node[t].len;else if(node[t].r != node[t].l + 1)    node[t].sum = node[L(t)].sum + node[R(t)].sum;else    node[t].sum = 0;}

void updata(int t, int l, int r, int c){if(node[t].l == l && node[t].r == r){if(c)    node[t].c ++;else    node[t].c --;        updata_sum(t);return ;    }int mid = (node[t].l + node[t].r) >> 1;if(l >= mid)    updata(R(t), l, r, c);else if(r <= mid)    updata(L(t), l, r, c);else{        updata(L(t), l, mid, c);        updata(R(t), mid, r, c);    }    updata_sum(t);}

int find(double key, int n){int l = 1, r = n, mid;while(l <= r){        mid = (l + r) >> 1;if(y[mid] == key)    return mid;else if(y[mid] > key)    r = mid-1;else    l = mid + 1;    }return 0;}

int main(){//freopen("data.in", "r", stdin);

int t, n, m, i, cas = 0;double x1, x2, y1, y2, ans;while(scanf("%d", &t), t){        m = 1;while(t--){            scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);            y[m] = y1; l[m].x = x1; l[m].y1 = y1; l[m].y2 = y2; l[m].flag = 1; m++;             y[m] = y2; l[m].x = x2; l[m].y1 = y1; l[m].y2 = y2; l[m].flag = 0; m++;        }        sort(y+1, y+m);        sort(l+1, l+m, cmp);        m--; n = 2;for(i = 1; i < m; i++)if(y[i] != y[i+1])    y[n++] = y[i+1];        n--; creat(1, 1, n);for(ans = 0, i = 1; i < m; i++){if(l[i].flag)    updata(1, find(l[i].y1, n), find(l[i].y2, n), 1);else    updata(1, find(l[i].y1, n), find(l[i].y2, n), 0);            ans += node[1].sum * (l[i+1].x - l[i].x);        }        printf("Test case #%d\nTotal explored area: %.2lf\n\n", ++cas, ans);    }return 0;}

转载于:https://www.cnblogs.com/vongang/archive/2011/10/26/2225239.html

POJ_1151 Atlantis(线段树)相关推荐

  1. HDU - 1542 Atlantis(线段树+扫描线)

    题目链接:点击查看 题目大意:给出n个矩形的左下角坐标和右上角坐标,求出其总面积,注意,矩形会重叠,重叠部分只计算一次 题目分析:如果暴力做需要考虑容斥原理,两两矩形组合判断是否重合,十分麻烦而且时间 ...

  2. POJ 1151 Atlantis 线段树+扫描线

    解题思路: 先将y轴进行离散化.n个矩形的2n个横边纵坐标共构成最多2n-1个区间的边界,对这些区间编号,建立起线段树. x轴记录左边和右边,左边时是矩形面积增加,覆盖层数增加边,右边是形面积减少,覆 ...

  3. HDU 1542 Atlantis 线段树+离散化+扫描线

    题意:给出一些矩形的最上角坐标和右下角坐标,求这些矩形的面积并. NotOnlySuccess 线段树专辑中扫描线模板题,弱智的我对着大大的代码看了一下午才搞懂. 具体见思路见注释=.= #inclu ...

  4. hdu 1542 Atlantis (线段树+扫描线)

    http://acm.hdu.edu.cn/showproblem.php?pid=1542 单纯的线段树+扫描线求面积并,需要离散化. code: #include <cstdlib> ...

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

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

  6. 数据结构二之线段树Ⅱ——KiKi‘s K-Number,ball,The Child and Sequence,「雅礼集训 2017 Day1」市场,Atlantis

    值域线段树+势能线段树+扫描线 KiKi's K-Number ball The Child and Sequence 「雅礼集训 2017 Day1」市场 Atlantis KiKi's K-Num ...

  7. 『线段树及扫描线算法 Atlantis』

    入门看这边『线段树 Segment Tree』. 扫描线 扫描线是一种解决一类平面内统计问题的算法,通常会借助线段树来实现,我们通过一道例题来引入这个算法. Atlantis Description ...

  8. hdu-1542 Atlantis(离散化+线段树+扫描线算法)

    题目链接: Atlantis Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/32768 K (Java/Others) ...

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

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

最新文章

  1. Procedure execution failed 2013 - Lost connection to MySQL server during query
  2. JZOJ__Day 6:【普及模拟】神奇的项链(fett)
  3. 【实用工具】之在VS中使用自定义工具配置nasm/yasm
  4. 完成端口中的单句柄数据结构与单IO数据结构的理解与设计
  5. 集群间动态扩展和删除hdfs的datanode和hbase的regionserver
  6. 语音助手的涅槃关头,我们应该完全抛弃屏幕还是选择“语音+图形界面”?
  7. 3D点云数据标注工具推荐
  8. Unity为游戏添加背景音乐
  9. 计算机excel函数试题,2014职称计算机考试Excel试题函数练习题
  10. pytorch搭建分类网络并进行训练和测试
  11. python+django+mysql多用户B2C商城系统毕业设计毕设开题报告
  12. 百度地图和51地图API应用开发
  13. typescript 装饰器
  14. 入行数据科学,这些书一定要看
  15. 简单易上手的理财方法介绍
  16. windows远程桌面互传文件
  17. 虚拟现实会议为何能够代替普通视频会议?
  18. 人生最好走的路就是理想
  19. unity3d 飞碟游戏
  20. Arduino CD4067 16通道模拟量采集

热门文章

  1. [shell]C语言调用shell脚本接口
  2. 算法训练 字符串编辑c语言
  3. The type java.lang.Object cannot be resolved
  4. Java中的日期操作
  5. Elasticsearch Mapping 详解
  6. Spark笔记:复杂RDD的API的理解(上)
  7. 【Flutter】开发之功能篇(七)
  8. 网易云基于Prometheus的微服务监控实践
  9. 目前可能最快的下载百度网盘文件的方法(aria2下载)
  10. Tengine+LUA+Nginx-GridFS+jemalloc编译安装