栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

zoj 1128 Atlantis

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

zoj 1128 Atlantis

#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 #%dnTotal explored area: %.2lfnn",ind++,area);}return 0;}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/378184.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号