hdu 1255 覆盖的面积

链接:点击打开链接

求覆盖两次和两次以上的面积,扫描线加线段树。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 5010
#define SB 1e-8
struct node1{
    double x,y1,y2;
    int flag;
    }a[N];
struct node2{
    double down,up,x;
    int cover,child;
    }anode[N];
double y[N],yd[N];
int t,m,n;
int cmp(node1 a,node1 b){
    return a.x<b.x;
    }
void bulid(int l,int r,int n){
    anode[n].down=yd[l];
    anode[n].up=yd[r];
    anode[n].child=1;
    anode[n].cover=0;
    if(l==r-1){
        anode[n].child=0;
        return;
        }
    int mid=(l+r)>>1;
    bulid(l,mid,2*n);
    bulid(mid,r,2*n+1);    
    }    
double update(double l,double r,double x,int flag,int n){
    if(anode[n].up <= l || anode[n].down >= r)
  return 0;
 if(anode[n].child == 0)
 {
  if(anode[n].cover > 1)
  {
   double ans = (x-anode[n].x)*(anode[n].up-anode[n].down);
   anode[n].cover += flag;
   anode[n].x = x;
   return ans;
  }
  else
  {
   anode[n].cover += flag;
   anode[n].x = x;
   return 0;
  }
 }
 return update(l,r,x,flag,2*n)+update(l,r,x,flag,2*n+1);
    }    
int    main(){
    int i,j;
    double x1,y1,x2,y2,aera;
    scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        m=0;
        for(i=1;i<=n;i++){
            scanf("%lf %lf %lf %lf",&x1,&y1,&x2,&y2);
            m++;
            a[m].x=x1,a[m].y1=y1,a[m].y2=y2,a[m].flag=1;
            y[m]=y1;
            m++;
            a[m].x=x2,a[m].y1=y1,a[m].y2=y2,a[m].flag=-1;
            y[m]=y2;
            }
            sort(y+1,y+1+m);
            sort(a+1,a+1+m,cmp);
            j=1;
            yd[1]=y[1];
            for(i=2;i<=m;i++){
                if(y[i]!=y[i-1])
                yd[++j]=y[i];
                }
            bulid(1,j,1);
            aera=0;
            for(i=1;i<=m;i++)
                aera+=update(a[i].y1,a[i].y2,a[i].x,a[i].flag,1);
                printf("%.2lf\n",aera);    
        }
      return 0;
    }


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值