Poj 1151 Atlantis

来源:互联网 发布:绿霸安全软件 编辑:程序博客网 时间:2024/05/29 15:24

一个简单的矩形面积并

因为矩形的数量少于150,所以不需要线段树优化,只要暴力就好了


扫描线的时候对于标记数组我们每次把事件点更新上去之后要去统计的是大于0的位置的总长度


其他的好像没有什么要注意的

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn = 150;#define LL long longLL lists[maxn*2];int valer[maxn*2];struct event{    LL l,r;    LL tim;    int add;    void init(LL ls,LL rs,LL ts,int a){        l = ls;        r = rs;        tim = ts;        add = a;    }};bool cmp(event a,event b){    if(a.tim != b.tim)        return a.tim < b.tim;    return a.add < b.add;}event arr[maxn *2];void adder(LL L,LL R,int n,int v){     int l = lower_bound(lists,lists+n,L)-lists;     int r = lower_bound(lists,lists+n,R)-lists;     for(int i=l;i<r;i++)        valer[i]+=v;}LL caler(int n){    LL ret = 0;    for(int i=0;i<=n;i++){        if(valer[i])         ret += lists[i+1] - lists[i];    }    return ret;}int main(){    int n;    int icase = 1;    while(~scanf("%d",&n) && n){        LL x1,y1,x2,y2;        double X1,X2,Y1,Y2;        int len = 0;        int m = 0;        lists[m++] = -1;        while(n--){            scanf("%lf %lf %lf %lf",&X1,&Y1,&X2,&Y2);            x1 = X1*100;            x2 = X2*100;            y1 = Y1*100;            y2 = Y2*100;            arr[len++].init(y1,y2,x1,1);            arr[len++].init(y1,y2,x2,-1);            lists[m++] = y1;            lists[m++] = y2;        }        sort(lists,lists+m);        m = unique(lists,lists+m)-lists;        sort(arr,arr+len,cmp);        memset(valer,0,sizeof(valer));        LL ans = 0;        adder(arr[0].l,arr[0].r,m,arr[0].add);        for(int i=1;i<len;i++){            if(arr[i].tim != arr[i-1].tim){                ans += caler(m) * (arr[i].tim - arr[i-1].tim + 0);            }            adder(arr[i].l,arr[i].r,m,arr[i].add);        }        double anw = ans *1.0/10000;        printf("Test case #%d\n",icase++);        printf("Total explored area: %.2f\n\n",anw);    }    return 0;}


0 0
原创粉丝点击