hdu 1255 覆盖的面积 矩阵面积交

题意:

求n个矩阵面积交

分析:

其实和求矩阵面积并是一样的思想

我们只需增加一个nsum数组储存区间中覆盖2次及以上的长度就行

#include <bits/stdc++.h>
#define maxn 200002
#define tmp (st<<1)
#define mid ((l+r)>>1)
#define lson l,mid,tmp
#define rson mid+1,r,tmp|1
using namespace std;
int cnt[maxn<<2];
double sum[maxn<<2];
double nsum[maxn<<2];
double x[maxn];
struct Seg{
    double h,l,r;
    int s;
    Seg(){}
    Seg(double a,double b,double c,int d):l(a),r(b),h(c),s(d){}
    bool operator<(const Seg &cmp)const{
        return h<cmp.h;
    }
}ss[maxn];
void push_up(int st,int l,int r){
    if(cnt[st]==1){
        sum[st]=x[r+1]-x[l];
        nsum[st]=sum[tmp]+sum[tmp|1];
    }
    else if(cnt[st]>=2)sum[st]=nsum[st]=x[r+1]-x[l];
    else if(l==r)nsum[st]=sum[st]=0;
    else {
        sum[st]=sum[tmp]+sum[tmp|1];
        nsum[st]=nsum[tmp]+nsum[tmp|1];
    }
}
void update(int L,int R,int c,int l,int r,int st){
    if(L<=l&&r<=R){
        cnt[st]+=c;
        push_up(st,l,r);
        return ;
    }
    if(L<=mid)update(L,R,c,lson);
    if(R>mid)update(L,R,c,rson);
    push_up(st,l,r);
}
int main(){
    int n,m,loop;
    scanf("%d",&loop);
    while(loop--){
        scanf("%d",&n);
        double a,b,c,d,ans=0;
        m=0;
        while(n--){
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            x[m]=a;
            ss[m++]=Seg(a,c,b,1);
            x[m]=c;
            ss[m++]=Seg(a,c,d,-1);
        }
        sort(x,x+m);
        sort(ss,ss+m);
        for(int i=0;i<m;++i){
            int l=lower_bound(x,x+m,ss[i].l)-x;
            int r=lower_bound(x,x+m,ss[i].r)-x-1;
            update(l,r,ss[i].s,0,m-1,1);
            ans+=nsum[1]*(ss[i+1].h-ss[i].h);
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值