zoj 1128 || poj 1151 Atlantis(线段树求矩形面积并)
来源:互联网 发布:北大青鸟java学费多少 编辑:程序博客网 时间:2024/05/20 12:47
纠结了好久了,终于差不多弄懂了,无语啊。以后会更难的,线段树水题都刷差不多了都。
求矩形面积并啊。= =。。。
先把Y坐标离散化,然后用扫描线扫描。进入的结构体标记为1,出去的为-1。所以,当一个节点进入又出去之后(也就是标记为0的时候),这一段线段就又可以用啦。
看网上代码直接存的是 y 坐标,这个不是太好理解,因为往常线段树都是存的下标,后来改了改,更直观点吧。
说的有点模糊,自己研究下代码就好了,网上没啥太好的资料。
#include <queue>#include <stack>#include <math.h>#include <stdio.h>#include <stdlib.h>#include <iostream>#include <limits.h>#include <string.h>#include <algorithm>#define MID(x,y) ((x+y)>>1)#define L(x) (x<<1)#define R(x) (x<<1|1)using namespace std;const int MAX = 205;const double eps = 1e-6;struct rectangle{ double lx,ly,rx,ry;};struct RRR{double x,y1,y2;int flag;};struct Tnode{int l,r,cover; double length;};bool dy(double x,double y){return x > y + eps;}// x > y bool xy(double x,double y){return x < y - eps;}// x < y bool dyd(double x,double y){ return x > y - eps;}// x >= y bool xyd(double x,double y){return x < y + eps;} // x <= y bool dd(double x,double y) {return fabs( x - y ) < eps;} // x == yTnode node[MAX*4]; RRR rr[MAX];double y[MAX];void init(){memset(node,0,sizeof(node));}bool cmp(RRR a,RRR b){return a.x < b.x;}void Build(int t,int l,int r){node[t].l = l;node[t].r = r;if( l == r - 1 ) return ;int mid = MID(l,r);Build(R(t),mid,r);Build(L(t),l,mid);}void len(int t){if( node[t].cover > 0 )node[t].length = y[node[t].r] - y[node[t].l];elseif( node[t].l == node[t].r - 1 )node[t].length = 0.0;elsenode[t].length = node[R(t)].length + node[L(t)].length;}void Updata(int t,RRR p){if( y[node[t].l] == p.y1 && y[node[t].r] == p.y2 ){node[t].cover += p.flag;len(t);return ;}int mid = MID(node[t].l,node[t].r);if( dyd(p.y1,y[mid]) )Updata(R(t),p);elseif( xyd(p.y2,y[mid]) )Updata(L(t),p);else{RRR tmp = p;tmp.y2 = y[mid];Updata(L(t),tmp);tmp = p;tmp.y1 = y[mid];Updata(R(t),tmp);}len(t);}double solve(int n,int cnt){init();Build(1,0,cnt-1);double sum = 0.0;Updata(1,rr[0]);for(int i=1; i<n; i++){sum += (rr[i].x - rr[i-1].x)*node[1].length;Updata(1,rr[i]);}return sum;}int main(){int n;int ind = 1;double x1,x2,y1,y2; while( ~scanf("%d",&n) && n ){int k = 0;for(int i=0; i<n; i++){scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);rr[k].x = x1; rr[k].y1 = y1;rr[k].y2 = y2; rr[k].flag = 1;y[k++] = y1;rr[k].x = x2; rr[k].y1 = y1;rr[k].y2 = y2; rr[k].flag = -1;y[k++] = y2;}sort(rr,rr+k,cmp);sort(y,y+k);int t = k;// 去重后,两个 上限是不一样的啊啊啊啊啊 k = unique(y,y+k) - y;double area = solve(t,k);printf("Test case #%d\nTotal explored area: %.2lf\n\n",ind++,area);}return 0;}
- zoj 1128 || poj 1151 Atlantis(线段树求矩形面积并)
- POJ 1151 Atlantis (线段树求矩形面积并)
- POJ 1151 Atlantis (线段树+扫描线 求矩形面积并)
- POJ-1151-Atlantis-求矩形面积并(线段树+扫描线)
- HDU 1542 Atlantis(线段树求矩形面积并)
- HDU 1542 Atlantis(线段树求矩形面积并)
- POJ 1151 Atlantis 线段树/矩形面积并
- Hdu 1542/ POj 1151 Atlantis 线段树+矩形面积并
- poj 1151 hdu 1542 hoj1119 Atlantis 线段树扫描线求矩形面积并
- POJ 1151 / HDU 1542 Atlantis 线段树求矩形面积并
- poj-1151-Atlantis-线段树求面积并
- [poj 1151] Atlantis:扫描线+线段树求面积并
- poj 1151 线段树+扫描线(Atlantis矩形面积)
- poj 1151 Atlantis(矩形面积并,线段树+离散化)
- hdu 1542(线段树 求矩形面积并)Atlantis
- Hdu 1542 Atlantis 线段树 求矩形面积并
- hdu 1542 Atlantis (线段树求矩形面积并)
- POJ 1151 Atlantis 线段树面积并
- HL7 2.X解析(JAVA版)
- iPhone开发学习笔记--UIScrollView
- PKU 3667
- Oracle RAC Past Image(PI) 说明
- Arx无模态窗口类定义
- zoj 1128 || poj 1151 Atlantis(线段树求矩形面积并)
- 2011-7-29 12:31:18
- java中调用浏览器的代码
- 数据块(Data Block)原理深入剖析
- ASP.NET关于GridView导出Excel
- 自己通过坐标转化,实现的一个按钮。绘图绘制的
- 跟我一起写Makefile:书写规则
- 枚举
- 女生饮酒过量致死