模板,线段树,面积并

具体的思路有空补上,先贴代码

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int const nMax = 100000;
struct T{
    int y[2];
    double f[2];
    double x;
    int left;
    void make(double a,double b,double c,int d){
        x=a,f[0]=b,f[1]=c;
        left = d;
    }
    bool operator < (const T& a) const {return x < a.x;}
} node[nMax];
struct A{
    double x;
    int id;
    bool operator < (const A& b) const {return x < b.x;}
};
A tmp[nMax];
double f[nMax];
int cov[nMax << 2];
double len[nMax<<2];
int flag[nMax << 2];
int n,m;
#define lson (u<<1)
#define rson ((u<<1)+1)
void build(int u,int l,int r){
    cov[u] = len[u] = 0;
    if(1+l==r) return ;
    int m=(l+r)>>1;
    build(lson,l,m);
    build(rson,m,r);
}

void setlen(int u,int l,int r){
    if(cov[u]>0) len[u] = f[r]-f[l];
    else {
        if(r==l+1) len[u]=0;
        else len[u] = len[lson] + len[rson];
    }
}

void ins(int u,int l,int r,int a,int b,int k){
    if(l==a && r==b){
        cov[u] += k;
        setlen(u,l,r);
        return ;
    }
    int m = (l+r) >> 1;
    if(b<=m)
        ins(lson,l,m,a,b,k);
    else if(a>=m)
        ins(rson,m,r,a,b,k);
    else
    {
        ins(lson,l,m,a,m,k);
        ins(rson,m,r,m,b,k);
    }
    setlen(u,l,r);
}
double x[2],y[2];
double const eps = 1.0e-9;
int dcmp(double x) {
    if(x>-eps && x<eps) return 0;
    return x > 0 ? 1 : -1;
}
int main() {
    int cas = 1;
    while(scanf("%d",&n),n){
        m = 1;
        for(int i=0;i<n;i++) {
            scanf("%lf%lf%lf%lf",&x[0],&y[0],&x[1],&y[1]);
            node[m++].make(x[0],y[0],y[1],1);
            node[m++].make(x[1],y[0],y[1],-1);
        }
        int cnt = 0;
        for(int i=1;i<m;i++) {
            tmp[cnt].x=node[i].f[0];tmp[cnt].id=-i;cnt++;
            tmp[cnt].x=node[i].f[1];tmp[cnt].id=i;cnt++;
        }
        sort(tmp,tmp+cnt);
        double now=tmp[0].x;int cur=0;
        for(int i=0;i<cnt;i++) {
            if(dcmp(tmp[i].x-now)){
                now = tmp[i].x;
                cur ++;
            }
            if(tmp[i].id>0) {
                node[tmp[i].id].y[1] = cur;
            }else node[-tmp[i].id].y[0] = cur;
            f[cur] = now;
        }
        build(1,0,cur);
        sort(node+1,node+m);
        double ans=0;
        for(int i=1;i<m-1;i++) {
            ins(1,0,cur,node[i].y[0],node[i].y[1],node[i].left);
            ans += len[1] * (node[i+1].x-node[i].x);
        }
        printf("Test case #%d\nTotal explored area: %.2lf\n\n",cas++,ans);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值