HDU 1255 覆蓋的面積(矩形面積交)


覆蓋的面積

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2304    Accepted Submission(s): 1136


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
 

 

Author
Ignatius.L & weigang Lee
 

 

Recommend
Ignatius.L
 
 
 
 
矩形面積交。線段樹的經典題了,重新做了一遍。
/*
HDU 1255 覆蓋的面積
求矩形面積交(離散化+線段樹)
給定一些矩形
求被這些矩形覆蓋過至少兩次的區域的面積

Author:kuangbin
date:2012-8-15

*/
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
using namespace std;
const int MAXN=2020;
struct Node
{
    int l,r;
    int c;
    double lf,rf;
    double cnt;//覆蓋一次以上的長度
    double more;//覆蓋兩次以上的長度
}segTree[MAXN*3];
struct Line
{
    double x,y1,y2;
    double f;
}line[MAXN];

double y[MAXN];

bool cmp(Line a,Line b)
{
    return a.x<b.x;
}

void Build(int i,int l,int r)
{
    segTree[i].l=l;
    segTree[i].r=r;
    segTree[i].cnt=0;
    segTree[i].more=0;
    segTree[i].lf=y[l];
    segTree[i].rf=y[r];
    if(l+1==r)return;
    int mid=(l+r)>>1;
    Build(i<<1,l,mid);
    Build((i<<1)|1,mid,r);
}
void calen(int i)
{
    if(segTree[i].c>=2)
    {
        segTree[i].more=segTree[i].cnt=segTree[i].rf-segTree[i].lf;
        return;
    }
    else if(segTree[i].c==1)
    {
        segTree[i].cnt=segTree[i].rf-segTree[i].lf;
        if(segTree[i].l+1==segTree[i].r)segTree[i].more=0;
        else segTree[i].more=segTree[i<<1].cnt+segTree[(i<<1)|1].cnt;
    }
    else
    {
        if(segTree[i].l+1==segTree[i].r)
        {
            segTree[i].cnt=segTree[i].more=0;
        }
        else
        {
            segTree[i].cnt=segTree[i<<1].cnt+segTree[(i<<1)|1].cnt;
            segTree[i].more=segTree[i<<1].more+segTree[(i<<1)|1].more;
        }
    }
}
void update(int i,Line e)
{
    if(e.y1==segTree[i].lf&&segTree[i].rf==e.y2)
    {
        segTree[i].c+=e.f;
        calen(i);
        return;
    }
    if(e.y2<=segTree[i<<1].rf) update(i<<1,e);
    else if(e.y1>=segTree[(i<<1)|1].lf) update((i<<1)|1,e);
    else
    {
        Line temp=e;
        temp.y2=segTree[i<<1].rf;
        update(i<<1,temp);
        temp=e;
        temp.y1=segTree[(i<<1)|1].lf;
        update((i<<1)|1,temp);
    }
    calen(i);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int T;
    int n;
    double x1,y1,x2,y2;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int t=1;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
            line[t].x=x1;
            //這里題目描述有問題?左下角和右上角
            line[t].y1=y1;
            line[t].y2=y2;
            line[t].x=x1;
            line[t].f=1;
            y[t]=y1;
            t++;
            line[t].y1=y1;
            line[t].y2=y2;
            line[t].x=x2;
            line[t].f=-1;
            y[t]=y2;
            t++;
        }
        sort(line+1,line+t,cmp);
        sort(y+1,y+t);
        Build(1,1,t-1);
        update(1,line[1]);
        double ans=0;
        for(int i=2;i<t;i++)
        {
            ans+=segTree[1].more*(line[i].x-line[i-1].x);
            update(1,line[i]);
        }
        printf("%.2lf\n",ans);
    }
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM