`
Coco_young
  • 浏览: 120123 次
  • 性别: Icon_minigender_1
  • 来自: 湖南长沙
社区版块
存档分类
最新评论

[离散化+线段树+扫描线]POJ_1151_Atlantis

 
阅读更多

题意:求矩形面积并

前天直接用“矩形切割”(我也不知道我那方法是不是矩形切割,姑且打上引号吧),过掉了这个题,那个猥琐的算法达到了O(n^3),如果题目数据很大的话,还是不行的,所以这两天还是继续学习扫描线,发现网上的很多文章讲的都不算很清楚,于是翻除了1999年陈宏的论文,开始研究,最终总算搞定了扫描线这个玩意儿。

注意点:要理解论文中提到的测度(即线段并的长度)和连续段的含义(求矩形周长并需要用到),这两点很关键,然后建树的时候用段树比较好一点,用点树我还没想到这么处理,建段树的话,数组稍微开大点(最好是5倍左右),否则会悲剧的。。

各种蛋疼:最后忘记去掉freopen..WA数次,蛋碎。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAXN = 5555;
struct Line{
    double l,r,h;
    int s;
    Line(){}
    Line(double ll,double rr,double hh,int ss):l(ll),r(rr),h(hh),s(ss){}
    bool operator < (const Line &l) const {
        return h<l.h;
    }
}L[MAXN<<2];
double X[MAXN<<2],sum[MAXN<<2];
int cnt[MAXN<<2],num_x,n,line_num;
#define lson l,m,rt<<1
#define rson m,r,rt<<1|1
void pushUP(int rt,int l,int r){
    if(cnt[rt]){
        sum[rt] = X[r]-X[l];
    }else{
        sum[rt] = sum[rt<<1]+sum[rt<<1|1];
    }
}
void update(int L,int R,int s,int l,int r,int rt){
    if(L<=l&&R>=r){
        cnt[rt]+=s;
        pushUP(rt,l,r);
        return;
    }
    //cout<<L<<" "<<R<<" "<<l<<" "<<r<<endl;system("pause");
    int m = (l+r)>>1;
    if(m>L)update(L,R,s,lson);
    if(m<R)update(L,R,s,rson);
    pushUP(rt,l,r);
}
int search(double a[],double k){
    int l=0,r=num_x-1,m;
    while(l<=r){
        m = (l+r)>>1;
        if(a[m]==k)return m;
        else if(k<a[m])r=m-1;
        else l=m+1;
    }
    return -1;
}
int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int cas=0;
    while(scanf("%d",&n),n){
        num_x = line_num = 0;
        for(int i=0;i<n;i++){
            double a,b,c,d;
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            X[num_x++] = a;
            X[num_x++] = c;
            L[line_num++] = Line(a,c,b,1);
            L[line_num++] = Line(a,c,d,-1);
        }
        sort(X,X+num_x);
        sort(L,L+line_num);
        int size = 1;
        for(int i=1;i<num_x;i++)if(X[i]!=X[i-1])X[size++]=X[i];
        num_x = size;
        memset(cnt,0,sizeof(cnt));
        memset(sum,0,sizeof(sum));
        double ans = 0;
        int l,r;
        l = search(X,L[0].l);
        r = search(X,L[0].r);
        update(l,r,L[0].s,0,num_x-1,1);
        for(int i=1;i<line_num;i++){
            ans+=sum[1]*(L[i].h-L[i-1].h);
            l = search(X,L[i].l);
            r = search(X,L[i].r);
            update(l,r,L[i].s,0,num_x-1,1);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",++cas,ans);
    }
    return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics