hdu 1255 覆盖的面积 求矩形面积交

本文探讨了深度学习在图像处理领域的应用,包括AR特效、图像处理、音视频直播等场景,展示了深度学习如何通过复杂的神经网络模型解决图像识别、编辑、合成等问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积.



 

Input
输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000.

注意:本题的输入数据较多,推荐使用scanf读入数据.
 

Output
对于每组测试数据,请计算出被这些矩形覆盖过至少两次的区域的面积.结果保留两位小数.
 

Sample Input
  
  
2 5 1 1 4 2 1 3 3 7 2 1.5 5 4.5 3.5 1.25 7.5 4 6 3 10 7 3 0 0 1 1 1 0 2 1 2 0 3 1
 

Sample Output
  
  
7.63 0.00

//


#include<stdio.h>
#include<algorithm>
#define MAXN 2015
using namespace std;
//坐标是浮点数
double y[MAXN*2];
struct lovekid{
 double left,right,len,crossLen;
 lovekid *leftSon,*rightSon;
 int cover;
}tree[MAXN*2],*Root;
struct line{
    double x,y1,y2;
    int cover;
    bool operator<(const line&t)const{
        return x<t.x;
    }
}point[MAXN*2];
int idx;
lovekid *CreatKid(){
 lovekid *p;
 p=&tree[idx++];
 p->cover=0;
 p->len=p->crossLen=0;
 p->leftSon=NULL;
 p->rightSon=NULL;
 return p;
}
lovekid *CreatLoveKid(int left,int right){
 lovekid *root=CreatKid();
 root->left=y[left];
 root->right=y[right];
 if(right-left>1){
  int mid=(left+right)>>1;
  root->leftSon=CreatLoveKid(left,mid);
  root->rightSon=CreatLoveKid(mid,right);
 }
 return root;
}
void GetLen(lovekid*root){
 root->len=0;
 if(root->cover>0)
  root->len=root->right-root->left;
 else if(root->leftSon!=NULL)
  root->len=root->leftSon->len+root->rightSon->len;
}
void GetCrossLen(lovekid*root){
 root->crossLen=0;
 if(root->cover>1)
  root->crossLen=root->right-root->left;
 else if(root->leftSon!=NULL){
  if(root->cover>0)
   root->crossLen=root->leftSon->len+root->rightSon->len;
  else root->crossLen=root->leftSon->crossLen+root->rightSon->crossLen;
 }
}
void UpdateLen(lovekid*root){
 GetLen(root);
 GetCrossLen(root);
}
void Update(lovekid*root,line t){
    if(root->left==t.y1&&root->right==t.y2){
        root->cover+=t.cover;
        UpdateLen(root);
        return;
    }
    if(t.y1>=root->leftSon->right)
        Update(root->rightSon,t);
    else if(t.y2<=root->rightSon->left)
        Update(root->leftSon,t);
    else{
        line tmp=t;
        tmp.y2=root->leftSon->right;
        Update(root->leftSon,tmp);
        tmp=t;
        tmp.y1=root->rightSon->left;
        Update(root->rightSon,tmp);
    }
    UpdateLen(root);
}
int main(){
 //freopen("C:\\Users\\loveKid\\Desktop\\in.txt","r",stdin);
 int N,cas,i,j;
 double x1,x2,y1,y2,ans;
 scanf("%d",&cas);
 while(cas--){
  scanf("%d",&N);
     idx=j=0;
  ans=0;
     for(i=0;i<N;i++,j+=2){
         scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
         point[j].x=x1;
         point[j].y1=y1;
         point[j].y2=y2;
         point[j].cover=1;
         y[j]=y1;
         point[j+1].x=x2;
         point[j+1].y1=y1;
         point[j+1].y2=y2;
         point[j+1].cover=-1;
         y[j+1]=y2;
     }
     sort(y,y+j);
     sort(point,point+j);
     Root=CreatLoveKid(0,j-1);
     Update(Root,point[0]);
     for(i=1;i<j;i++){
         ans+=Root->crossLen*(point[i].x-point[i-1].x);
          Update(Root,point[i]);
     }
     printf("%.2lf\n",ans);
 }
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值