HDU 1255 覆盖的面积(线段树扫描线)

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<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int maxn=2000+100;
struct node{
    int l,r;
    int c;//记录重叠次数
    double lf,rf;
    double len1,len2;//len1,len2分别为覆盖一次,两次的长度
}t[maxn<<2];
struct Line{
    double x,y1,y2;
    int f;
}line[maxn];
double y[maxn];
int cas,n;
int cmp(Line l1,Line l2)
{
    return l1.x<l2.x;
}
void build(int l,int r,int rs)
{
    t[rs].l=l;t[rs].r=r;
    t[rs].c=0;
    t[rs].len1=t[rs].len2=0;
    t[rs].lf=y[l];
    t[rs].rf=y[r];
    if(l+1==r)  return ;
    int mid=(l+r)>>1;
    build(l,mid,rs<<1);
    build(mid,r,rs<<1|1);
}
void pushup(int rs)
{
    if(t[rs].c>=2)
    {
        t[rs].len1=t[rs].len2=t[rs].rf-t[rs].lf;
        return ;
    }
    else if(t[rs].c==1)
    {
        t[rs].len1=t[rs].rf-t[rs].lf;
        if(t[rs].l+1==t[rs].r)  t[rs].len2=0;
        else   t[rs].len2=t[rs<<1].len1+t[rs<<1|1].len1;
    }
    else
    {
        if(t[rs].l+1==t[rs].r)  t[rs].len1=t[rs].len2=0;
        else
        {
            t[rs].len1=t[rs<<1].len1+t[rs<<1|1].len1;
            t[rs].len2=t[rs<<1].len2+t[rs<<1|1].len2;
        }
    }
}
void update(int rs,Line e)
{
    if(e.y1==t[rs].lf&&e.y2==t[rs].rf)
    {
        t[rs].c+=e.f;
        pushup(rs);
        return ;
    }
    if(e.y2<=t[rs<<1].rf) update(rs<<1,e);
    else if(e.y1>=t[rs<<1|1].lf)  update(rs<<1|1,e);
    else
    {
        Line temp=e;
        temp.y2=t[rs<<1].rf;
        update(rs<<1,temp);
        temp=e;
        temp.y1=t[rs<<1|1].lf;
        update(rs<<1|1,temp);
    }
    pushup(rs);
}
int main()
{
   double x1,y1,x2,y2;
   scanf("%d",&cas);
   while(cas--)
   {
       scanf("%d",&n);
       int tt=1;
       REP(i,n)
       {
           scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
           line[tt].x=x1;
           line[tt].y1=y1;
           line[tt].y2=y2;
           line[tt].f=1;
           y[tt]=y1;
           tt++;
           line[tt].x=x2;
           line[tt].y1=y1;
           line[tt].y2=y2;
           line[tt].f=-1;
           y[tt]=y2;
           tt++;
       }
       sort(line+1,line+tt,cmp);
       sort(y+1,y+tt);
       build(1,tt-1,1);
       update(1,line[1]);
       double ans=0;
       for(int i=2;i<tt;i++)
       {
           ans+=t[1].len2*(line[i].x-line[i-1].x);
           update(1,line[i]);
       }
       printf("%.2f\n",ans);
   }
   return 0;
}
时间: 2024-08-22 23:19:25

HDU 1255 覆盖的面积(线段树扫描线)的相关文章

hdu 1255 覆盖的面积(线段树&amp;扫描线&amp;重复面积)

覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3375    Accepted Submission(s): 1645 Problem Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数

hdu 1255 覆盖的面积 线段树扫描线求重叠面积

稀里糊涂打完了没想到1A了,心情还是很舒畅的,c和c++的四舍五入还是四舍六入遇积进位遇偶舍,我感觉很混乱啊,这道题我输出的答案是7.62,也就是遇偶舍了,可是我就手动处理一下进位问题,发现0.005 系统自动进位0.01了,尼玛啊,我一下子就混乱了,不是遇偶舍么,0也是偶数啊,怎么就进位了呢.最后我就不手动处理进位问题了,直接0.2lf%,虽然我输出的结果是7.62,但是提交也过了 这道题和poj1151,hdu1542差不多,扫描线详细讲解http://blog.csdn.net/young

HDU 1255 覆盖的面积 (线段树+扫描线+离散化)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题意很清楚,就是让你求矩阵之间叠加层数大于1的矩形块的面积和. 因为n只有1000,所以我离散化一下,数据大小就缩小了,那么之后只需要线段树单点更新就好了. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <map> 5 #include <algor

HDU 1255 覆盖的面积 线段树+扫描线

同 POJ1151 这次是两次 #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <string> #include <stack> #include <queue> #include <cmath> #include <vector

HDU 1255 覆盖的面积 (线段树 + 离散化 + 扫描线)

覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4304    Accepted Submission(s): 2139 Problem Description 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数

HDU 1255 覆盖的面积(线段树面积并)

描述 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input 输入数据的第一行是一个正整数T(1<=T<=100),代表测试数据的数量.每个测试数据的第一行是一个正整数N(1<=N<=1000),代表矩形的数量,然后是N行数据,每一行包含四个浮点数,代表平面上的一个矩形的左上角坐标和右下角坐标,矩形的上下边和X轴平行,左右边和Y轴平行.坐标的范围从0到100000. 注意:本题的输入数据较多,推荐使用scanf读入数据. Output 对于每组测试数据,请计算出

hdu1255 覆盖的面积 线段树-扫描线

矩形面积并 线段树-扫描线裸题 1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 #include<math.h> 5 using namespace std; 6 const int maxm=2e3+5; 7 const double eps=1e-5; 8 9 struct seg{ 10 double x,y1,y2; 11 int c; 12 bool operator

HDU 1255 覆盖的面积 (扫描线 线段树 离散化)

题目链接 题意:中文题意. 分析:纯手敲,与上一道题目很相似,但是刚开始我以为只是把cnt>=0改成cnt>=2就行了,. 但是后来发现当当前加入的线段的范围之前 还有线段的时候就不行了,因为虽然现在都不等于 2,但是之前的那个线段加上现在的已经覆盖2次了. 1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 #include &

hdu 1255 覆盖的面积(扫描线)

http://acm.hdu.edu.cn/showproblem.php?pid=1255 一道挺简单的题,让我折腾了许久.主要卡在了更新节点后维护父亲节点上.后来思路明确了就很容易了. 节点信息: l,r:区间端点 cnt:区间被覆盖的次数,cnt = 0说明没有被完全覆盖. len1:区间被覆盖的长度 len2:区间至少被两条线段覆盖的长度. 只要找到父亲节点与子节点在len1,len2,cnt的关系就简单了. #include <stdio.h> #include <iostre

HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)

Problem A : Counting Squares From:HDU, 1264 Problem Description Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in t