poj1151平面矩形面积并,双离散化
2016-07-22 10:01
260 查看
线段树维护扫描线,表示这里的线段树维护多个矩形覆盖同一段区间时不好搞
这里的线段树需要灵活一点
扫描线记录当前扫描所在位置,扫描了多少区域(线段树维护),乘上距离,相加得答案即可
线段树维护,加一条边,在区间内加
删除一条边,区间内删除,tree【i】.s记录区间内被多少线段覆盖
如果没有被线段覆盖,则通过左右子树被线段覆盖的长度相加得到
struct seg//扫描线线段树的一般框架,关键是有一个被覆盖次数,以及特有计算长度的函数
{
int a,b;
int len;
int s; //被覆盖的次数
};
暴力:
离散化后,数据范围小,直接枚举,标记所有可行点就行
这里的线段树需要灵活一点
扫描线记录当前扫描所在位置,扫描了多少区域(线段树维护),乘上距离,相加得答案即可
线段树维护,加一条边,在区间内加
删除一条边,区间内删除,tree【i】.s记录区间内被多少线段覆盖
如果没有被线段覆盖,则通过左右子树被线段覆盖的长度相加得到
struct seg//扫描线线段树的一般框架,关键是有一个被覆盖次数,以及特有计算长度的函数
{
int a,b;
int len;
int s; //被覆盖的次数
};
#include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<cstdlib> using namespace std; int n,kase=0; struct aa { double x,l,r;int t; bool operator <(const aa &b)const { return x<b.x; } }a[2009]; struct aaa { int l,r,s;//s表示覆盖情况,覆盖了几层 double len;//表示真实长度 }tree[1009];//线段树 double hs[209];//y坐标的hash void build(int i,int l,int r) { tree[i].l=l;tree[i].r=r; tree[i].len=tree[i].s=0; if (l+1==r) return ; int mid=(l+r)>>1; build(i<<1,l,mid); build(i<<1|1,mid,r); } void pan(int i)//计算长度,如果被覆盖,则为总长,否则为左右儿子长度之和 { if (tree[i].s) tree[i].len=hs[tree[i].r]-hs[tree[i].l];//在线段树中,有的区间是被多个矩形覆盖 else //这里的s,表示整个区间被覆盖了几次 if (tree[i].l+1==tree[i].r) tree[i].len=0; //因为保证删除后和插入之前一样,所有没有 else tree[i].len=tree[i<<1].len+tree[i<<1|1].len; //标记下传,QAQ感觉思路有点乱 } void updata(int i,int l,int r,int t)//插入或删除一条线段 { if (tree[i].l==l&&tree[i].r==r) { tree[i].s+=t; pan(i); return ; } int mid=(tree[i].l+tree[i].r)>>1; if (mid>=r) updata(i<<1,l,r,t); else if (mid<=l) updata(i<<1|1,l,r,t); else updata(i<<1,l,mid,t),updata(i<<1|1,mid,r,t); pan(i); } void work() { double aa,bb,cc,dd; int tot=0; for (int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&aa,&bb,&cc,&dd); a[++tot].x=aa;a[tot].l=bb;a[tot].r=dd;a[tot].t=1;hs[tot]=bb; a[++tot].x=cc;a[tot].l=bb;a[tot].r=dd;a[tot].t=-1;hs[tot]=dd; } sort(hs+1,hs+tot+1); sort(a+1,a+tot+1); int nn=unique(hs+1,hs+tot+1)-hs-1; build(1,1,nn); int l,r; double ans=0; for (int i=1;i<=tot;i++) { l=lower_bound(hs+1,hs+nn+1,a[i].l)-hs; r=lower_bound(hs+1,hs+nn+1,a[i].r)-hs; ans+=(a[i].x-a[i-1].x)*tree[1].len; updata(1,l,r,a[i].t); } printf("Test case #%d\n",++kase); printf("Total explored area: %.2lf\n\n",ans); } int main() { while (scanf("%d",&n)!=EOF) { if (n==0) break; sizeof(a,0,sizeof(a)); sizeof(hs,0,sizeof(hs)); sizeof(tree,0,sizeof(tree)); work(); } return 0; }
暴力:
离散化后,数据范围小,直接枚举,标记所有可行点就行
#include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> using namespace std; double x[109],y[109],xx[109],yy[109]; int n,kase=0; double hsy[209],hsx[209]; bool f[209][209]; void work() { int tot=0; for (int i=1;i<=n;i++) { scanf("%lf%lf%lf%lf",&x[i],&y[i],&xx[i],&yy[i]); hsx[++tot]=x[i];hsy[tot]=y[i]; hsx[++tot]=xx[i];hsy[tot]=yy[i]; } sort(hsx+1,hsx+tot+1); sort(hsy+1,hsy+tot+1); int nx=unique(hsx+1,hsx+tot+1)-hsx-1; int ny=unique(hsy+1,hsy+tot+1)-hsy-1; double ans=0; int a,b,c,d; for (int i=1;i<=n;i++) { a=lower_bound(hsx+1,hsx+nx+1,x[i])-hsx; b=lower_bound(hsx+1,hsx+nx+1,xx[i])-hsx; c=lower_bound(hsy+1,hsy+ny+1,y[i])-hsy; d=lower_bound(hsy+1,hsy+ny+1,yy[i])-hsy; for (int i=a;i<b;i++) for (int j=c;j<d;j++) f[i][j]=true; } for (int i=1;i<nx;i++) for (int j=1;j<ny;j++) if (f[i][j]) ans+=(hsy[j+1]-hsy[j])*(hsx[i+1]-hsx[i]); printf("Test case #%d\n",++kase); printf("Total explored area: %.2lf\n\n",ans); return ; } int main() { while (scanf("%d",&n)!=EOF) { if (n==0) break; memset(hsx,0,sizeof(hsx)); memset(hsy,0,sizeof(hsy)); memset(f,false,sizeof(f)); work(); } return 0; }
相关文章推荐
- POJ 2506 - Tiling
- Java自定义异常
- iOS开发之AFNetworking 3.0.4使用
- Mac OS X中Apache开启ssl
- linux系统指令集
- thinkphp报错No input file specified. (对于隐藏url中的index.php 是如何做到的?)
- GDB 修改当前判断函数的返回值(即修改寄存器的值)的方法
- django 简单的邮件系统
- Ubuntu下Apache开启rewrite模块
- android基础——广播
- opencv学习之一
- RSA算法原理(一)
- Problem I : Palindrome Partitioning I
- 匿名方法 Lambda表达式
- 只为成功找方法,不为失败找借口! javaweb学习总结(十)——HttpServletRequest对象(一)
- HorseMap
- win10无法启动承载网络
- IDEA使用git oschina提交本地项目
- Light OJ-----1336数论-----整数拆分推论
- HDU 2037 今年暑假不AC