POJ 1151 Atlantis

来源:互联网 发布:西安丝路软件 编辑:程序博客网 时间:2024/05/22 15:13

题目大意:

给出n个矩形,形式是左下点和右上点。求它们的面积并。



解题思路:

扫描线算法,对Y进行扫描,线段树查询Y轴扫描某段距离后X轴一共有多长的距离有边,并计算面积。



下面是代码:

#include <set>#include <map>#include <queue>//#include <math.h>#include <vector>#include <string>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>#define eps 1e-8#define pi acos(-1.0)#define inf 107374182#define inf64 1152921504606846976#define lc l,m,tr<<1#define rc m + 1,r,tr<<1|1#define iabs(x)  ((x) > 0 ? (x) : -(x))#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (SIZE))#define clearall(A, X) memset(A, X, sizeof(A))#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))#define memcopyall(A, X) memcpy(A , X ,sizeof(X))#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )using namespace std;struct node2{    int num;    double y,l,r;} edge[1000];double tempx[1000],binx[1000];double x1,x2,y1,y2,ans;int cntx,n;bool cmp(node2 a,node2 b){    return a.y<b.y;}int binnum(double num){    int ll=0,m,rr=cntx-1;    while(rr>ll)    {        m=(ll+rr)>>1;        if(binx[m]==num)return m;        else if(binx[m]<num)ll=m+1;        else rr=m-1;    }    return ll;}struct node1{    double disnow;    int cnt;} node[205<<2];inline void PushUp(int l,int r,int tr){    if(node[tr].cnt)node[tr].disnow=binx[r+1]-binx[l];    else if(l==r)node[tr].disnow=0;    else node[tr].disnow=node[tr<<1].disnow+node[tr<<1|1].disnow;}void update(int L,int R,int num,int l,int r,int tr){    if(L<=l&&r<=R)    {        node[tr].cnt+=num;        PushUp(l,r,tr);        return ;    }    int m=(l+r)>>1;    if(L<=m)update(L,R,num,l,m,tr<<1);    if(m<R)update(L,R,num,m+1,r,tr<<1|1);    PushUp(l,r,tr);}int main(){    int case1=1;    while(scanf("%d",&n),n)    {        for(int i=0; i<n; i++)        {            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);            tempx[2*i]=x1;            tempx[2*i+1]=x2;            edge[2*i].l=x1;            edge[2*i].r=x2;            edge[2*i+1]=edge[2*i];            edge[2*i].y=y1;            edge[2*i+1].y=y2;            edge[2*i].num=1;            edge[2*i+1].num=-1;        }        sort(tempx,tempx+2*n);        binx[0]=tempx[0];        cntx=1;        for(int i=1; i<2*n; i++)        {            if(tempx[i]!=binx[cntx-1])            {                binx[cntx++]=tempx[i];            }        }        sort(edge,edge+2*n,cmp);        clearall(node,0);        ans=0;        x1=binnum(edge[0].l);        x2=binnum(edge[0].r);        x2--;        update(x1,x2,edge[0].num,0,cntx-2,1);        y1=edge[0].y;        for(int i=1; i<2*n; i++)        {            ans+=node[1].disnow*(edge[i].y-y1);            y1=edge[i].y;            x1=binnum(edge[i].l);            x2=binnum(edge[i].r);            x2--;            update(x1,x2,edge[i].num,0,cntx-2,1);        }        printf("Test case #%d\n",case1++);        printf("Total explored area: %.2lf\n\n",ans);    }    return 0;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 两岁宝宝不说话怎么办? 4岁儿童说话结巴怎么办 3岁宝宝说话结巴怎么办 6岁儿童舌头短怎么办 5岁宝宝说话结巴怎么办 两岁宝宝说话晚怎么办 6岁儿童说话结巴怎么办 2岁宝宝呕吐拉稀怎么办 2岁宝宝突然呕吐怎么办 2岁宝宝呕吐发烧怎么办 2岁宝宝呕吐厉害怎么办 1岁宝宝半夜呕吐怎么办 2岁半宝宝呕吐怎么办 2岁宝宝半夜呕吐怎么办 两岁宝宝一直吐怎么办 原画师老了以后怎么办 孩子不想上学怎么办怎么去说服 嫉妒别人比我好怎么办 三岁宝宝爱打人怎么办 1岁宝宝喜欢打人怎么办 ps图层解锁不了怎么办 沈腾结婚马丽怎么办 延长甲没有纸托怎么办 高考第一志愿没录取怎么办 电子画颜料干了怎么办 数字画颜料干了怎么办 彩砂纸画不好了怎么办 宝宝吃了油画棒怎么办 2岁宝宝不爱刷牙怎么办 两岁宝宝不刷牙怎么办 1岁宝宝不爱刷牙怎么办 3岁宝宝不肯刷牙怎么办 20岁没学历迷茫怎么办 四岁了不长头发怎么办 17岁掉头发严重怎么办 头发很油,又少怎么办 25岁头发变稀怎么办 宝宝头发少又黄怎么办 头旋附近头发少怎么办 25岁掉头发严重怎么办 2岁宝宝头发稀少怎么办