当前位置: 首页 > news >正文

扫描线概览

poj 1151:

可以说是计算几何的扫描线入门题吧。挺简单的,线段树建树部分要想想。其他就看码力了。

  1 //13435314    ooyyloo    1151    Accepted    748K    0MS    G++    2079B    2014-09-12 16:16:35
  2 #include <cstdio>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <set>
  6 #define mp make_pair
  7 #define dbg(x) cout<<#x<<"="<<x<<endl
  8 #define foreach(it,v) for(__typeof(v.begin()) it=v.begin();it!=v.end();it++)
  9 using namespace std;
 10 const int maxn=201;
 11 
 12 struct line{
 13     double x,y1,y2;
 14     int v;
 15     bool operator< (const line &a) const { return x<a.x; }
 16 }wu[maxn];
 17 
 18 int n,nn;
 19 double ans;
 20 set<double> sety;
 21 double hashy[maxn]; int hpo;
 22 
 23 //seg tree
 24 double sh[maxn<<2]; int cov[maxn<<2];
 25 void build(int k,int l,int r)
 26 {
 27     sh[k]=cov[k]=0;
 28     if (l+1==r) return;
 29     int mid=l+r>>1;
 30     build(k<<1,l,mid);
 31     build(k<<1|1,mid,r);
 32 }
 33 void update(int k,int l,int r)
 34 {
 35     if (cov[k])      sh[k]=hashy[r]-hashy[l];
 36     else if (l+1==r) sh[k]=cov[k]?hashy[r]-hashy[l]:0;
 37     else              sh[k]=sh[k<<1]+sh[k<<1|1];
 38 }
 39 void modify(int k,int l,int r,int ll,int rr,int v)//we just care about father
 40 {
 41     if (l>=ll&&r<=rr)
 42     {
 43         cov[k]+=v;
 44         update(k,l,r);
 45         return;
 46     }
 47     int mid=l+r>>1;
 48     if (ll<mid) modify(k<<1,l,mid,ll,rr,v);
 49     if (rr>mid) modify(k<<1|1,mid,r,ll,rr,v);
 50     update(k,l,r);
 51 }
 52 
 53 
 54 void init()
 55 {
 56     nn=n<<1;
 57     sety.clear();
 58 
 59     double x1,y1,x2,y2;
 60     for (int i=1;i<=n;i++)
 61     {
 62         scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
 63         wu[i].x=x1; wu[i].y1=y1; wu[i].y2=y2; wu[i].v=1;
 64         wu[i+n].x=x2; wu[i+n].y1=y1; wu[i+n].y2=y2; wu[i+n].v=-1;
 65         sety.insert(y1); sety.insert(y2);
 66     }
 67     hpo=0;
 68     foreach(it,sety) hashy[++hpo]=*it;
 69     //for (auto x:sety) hashy[++hpo]=x;
 70 }
 71 int bs(double d)
 72 {
 73     int l=1,r=hpo,mid;
 74     while (l<=r)
 75     {
 76         mid=l+r>>1;
 77         if (d>hashy[mid]) l=mid+1;
 78         else               r=mid-1;
 79     }
 80     return l;
 81 }
 82 void solve()
 83 {
 84     ans=0.0;
 85     sort(wu+1,wu+1+nn);
 86     modify(1,1,hpo,bs(wu[1].y1),bs(wu[1].y2),wu[1].v);
 87     for (int z=2;z<=nn;z++)
 88     {
 89         ans+=sh[1]*(wu[z].x-wu[z-1].x);
 90         modify(1,1,hpo,bs(wu[z].y1),bs(wu[z].y2),wu[z].v);
 91     }
 92 }
 93 int main()
 94 {
 95     for (int tt=1;scanf("%d",&n)!=EOF;tt++)
 96     {
 97         if (!n) break;
 98         init();
 99         build(1,1,hpo);
100         solve();
101         printf("Test case #%d\n",tt);
102         printf("Total explored area: %.2f\n",ans);
103         puts("");
104     }
105     return 0;
106 }
//scanning line 1

 

转载于:https://www.cnblogs.com/monmonde/p/3968612.html

相关文章:

  • 模拟地与数字地(转)
  • 转函数重载之const
  • jeroMq示例之[2] [req-rep-envelopes msg identity]
  • IOS开发之 归档总结
  • 创建App IDs时选择App ID Prefix才能勾选push notifications
  • [Linux] day07——查看及过滤文本
  • TranslateAnimation详解
  • CSS编码设置篇utf-8与gb2312互转换
  • 552 you must authentication
  • 安裝PHPBB
  • ZOJ 3329 期望DP
  • C语言 21-结构体
  • Java学习笔记2:当构造方法有多个参数时考虑使用Builder
  • Perl:Perl的一些应用例子。
  • 指针传递参数_for chris
  • python3.6+scrapy+mysql 爬虫实战
  • 【RocksDB】TransactionDB源码分析
  • 2018一半小结一波
  • 5分钟即可掌握的前端高效利器:JavaScript 策略模式
  • Apache的基本使用
  • E-HPC支持多队列管理和自动伸缩
  • JAVA 学习IO流
  • js
  • markdown编辑器简评
  • Meteor的表单提交:Form
  • PV统计优化设计
  • Shell编程
  • SpriteKit 技巧之添加背景图片
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 从伪并行的 Python 多线程说起
  • 对象管理器(defineProperty)学习笔记
  • 前端性能优化--懒加载和预加载
  • 如何胜任知名企业的商业数据分析师?
  • puppet连载22:define用法
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • 如何正确理解,内页权重高于首页?
  • ​第20课 在Android Native开发中加入新的C++类
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #1015 : KMP算法
  • #pragma预处理命令
  • (2020)Java后端开发----(面试题和笔试题)
  • (html转换)StringEscapeUtils类的转义与反转义方法
  • (简单) HDU 2612 Find a way,BFS。
  • (一)ClickHouse 中的 `MaterializedMySQL` 数据库引擎的使用方法、设置、特性和限制。
  • (轉貼) 資訊相關科系畢業的學生,未來會是什麼樣子?(Misc)
  • .bat批处理(十):从路径字符串中截取盘符、文件名、后缀名等信息
  • .net FrameWork简介,数组,枚举
  • .NET Framework杂记
  • .NET I/O 学习笔记:对文件和目录进行解压缩操作
  • @我的前任是个极品 微博分析
  • [20171102]视图v$session中process字段含义
  • [2019.3.5]BZOJ1934 [Shoi2007]Vote 善意的投票
  • [202209]mysql8.0 双主集群搭建 亲测可用
  • [Android]使用Android打包Unity工程
  • [HXPCTF 2021]includer‘s revenge