覆盖的面积

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

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
 

题意:

给出矩形左下和右上角的坐标,求矩形交的面积。

代码:

//cnt[rt]>=2的一定是覆盖过两次以上的,直接计算。cnt[rt]==1时,如果rt的左右儿子
//被覆盖过大于等于1次,那么再被他们父亲覆盖上就是覆盖过两次了,值是左右儿子被
//覆盖过一次的长度的和。cnt[rt]==0照常更新。sum2记录覆盖过两次以上的边的长度。
//sum1记录覆盖过一次的边的长度。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
double sum1[maxn*],sum2[maxn*],mp[maxn];
int cnt[maxn*];
struct node{
double l,r,h;
int d;
node(){}
node(double a,double b,double c,int d):l(a),r(b),h(c),d(d){}
bool operator < (const node &p)const{
if(h==p.h) return d>p.d;
return h<p.h;
}
}nodes[maxn*];
int Bsearch(double a,int b,double *c)
{
int l=,r=b-,mid;
while(l<=r){
mid=(l+r)>>;
if(c[mid]==a) return mid;
else if(c[mid]>a) r=mid-;
else l=mid+;
}
return -;
}
void Pushup(int l,int r,int rt)
{
if(cnt[rt]){
sum1[rt]=mp[r+]-mp[l];
if(cnt[rt]==)
sum2[rt]=sum1[rt<<]+sum1[rt<<|];
else sum2[rt]=mp[r+]-mp[l];
}
else if(l==r) sum1[rt]=sum2[rt]=;
else{
sum1[rt]=sum1[rt<<]+sum1[rt<<|];
sum2[rt]=sum2[rt<<]+sum2[rt<<|];
}
}
void Update(int ql,int qr,int v,int l,int r,int rt)
{
if(ql<=l&&qr>=r){
cnt[rt]+=v;
Pushup(l,r,rt);
return;
}
//if(l==r) return;
int m=(l+r)>>;
if(ql<=m) Update(ql,qr,v,l,m,rt<<);
if(qr>m) Update(ql,qr,v,m+,r,rt<<|);
Pushup(l,r,rt);
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
int m=,nu=;
double x1,y1,x2,y2;
memset(sum1,,sizeof(sum1));
memset(sum2,,sizeof(sum2));
memset(cnt,,sizeof(cnt));
for(int i=;i<n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
nodes[m++]=node(x1,x2,y1,);
nodes[m++]=node(x1,x2,y2,-);
mp[nu++]=x1;mp[nu++]=x2;
}
sort(mp,mp+nu);
nu=unique(mp,mp+nu)-mp;
sort(nodes,nodes+m);
double ans=;
for(int i=;i<m-;i++){
int lef=Bsearch(nodes[i].l,nu,mp);
int rig=Bsearch(nodes[i].r,nu,mp)-;
if(lef<=rig)
Update(lef,rig,nodes[i].d,,nu-,);
ans+=sum2[]*(nodes[i+].h-nodes[i].h);
}
printf("%.2lf\n",ans);
}
return ;
}

HDU1255 扫描线 矩形交面积 离散化的更多相关文章

  1. HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化

    题目链接:https://vjudge.net/problem/HDU-1255 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<= ...

  2. hdu 1255 覆盖的面积 (扫描线求矩形交)

    覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

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

    版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...

  4. poj 3277 City Horizon (线段树 扫描线 矩形面积并)

    题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...

  5. CCF 201312-3 最大的矩形 (暴力,离散化)

    问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方图.例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3 ...

  6. HDU 5130 Signal Interference --计算几何,多边形与圆的交面积

    题意: 求所有满足PB <= k*PA 的P所在区域与多边形的交面积. 解法: 2014广州赛区的银牌题,当时竟然没发现是圆,然后就没做出来,然后就gg了. 圆的一般式方程: 设A(x1,y1) ...

  7. Open-air shopping malls(二分半径,两元交面积)

    http://acm.hdu.edu.cn/showproblem.php?pid=3264 Open-air shopping malls Time Limit: 2000/1000 MS (Jav ...

  8. TZOJ 2392 Bounding box(正n边形三点求最小矩形覆盖面积)

    描述 The Archeologists of the Current Millenium (ACM) now and then discover ancient artifacts located ...

  9. hdu 1828 Picture(线段树扫描线矩形周长并)

    线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...

随机推荐

  1. CsvHelper文档-2读

    CsvHelper文档-2读 这个库默认不需要做任何设置就可以很容易的使用它.如果你的类属性名称直接匹配csv的标题名称,那么可以按照下面的实例来用: (以下所有的代码都需要引用using csvhe ...

  2. 安装Tensorflow过程pip安装报错:is not a supported wheel on this platform

    安装Tensorflow过程pip安装报错:is not a supported wheel on this platform 通过pip安装wheel镜像时,安装指令为: pip install - ...

  3. http和https的异同

    转自:http://blog.csdn.net/whatday/article/details/38147103 什么是 HTTPS? HTTPS (基于安全套接字层的超文本传输协议 或者是 HTTP ...

  4. ACM 第五天

    匈牙利算法(二分图匹配) C - Courses Consider a group of N students and P courses. Each student visits zero, one ...

  5. TCP系列09—连接管理—8、TCP Reset

    我们在介绍TCP头的时候,提到过其中有个RST标志位.当一个TCP报文中这个标志位打开的时候,我们叫做reset包(严格的说应该叫做reset段,但是很多时候段包帧并不加以区分)或者简单称呼为rese ...

  6. GPS定位,根据经纬度查询附近地点的经纬度-sql方法实现

    根据当前所在的坐标点也即经纬度,查找数据库中附近5公里或10公里附近的所有信息的实现,经过查找资料,原来是我高二学的,就是求弦长,数学忘完了,没想到数学还这么有用,数学啊 真是用途太大了. 用到的什么 ...

  7. 【ADO.NET】SqlBulkCopy批量添加DataTable

    使用事务和SqlBulkCopy批量插入数据 SqlBulkCopy是.NET Framework 2.0新增的类,位于命名空间System.Data.SqlClient下,主要提供把其他数据源的数据 ...

  8. openstack之keystone部署

    前言 openstack更新频率是挺快的,每六个月更新一次(命名是是以A-Z的方式,Austin,Bexar...Newton).博主建议大家先可一种版本研究,等某一版本研究透彻了,在去研究新的版本. ...

  9. BZOJ5466 NOIP2018保卫王国(倍增+树形dp)

    暴力dp非常显然,设f[i][0/1]表示i号点不选/选时i子树内的答案,则f[i][0]=Σf[son][1],f[i][1]=a[i]+Σmin(f[son][0],f[son][1]). 注意到 ...

  10. BZOJ4867 Ynoi2017舌尖上的由乃(dfs序+分块)

    容易想到用dfs序转化为序列上的问题.考虑分块,对每块排序,修改时对于整块打上标记,边界暴力重构排序数组,询问时二分答案,这样k=sqrt(nlogn)时取最优复杂度nsqrt(nlogn)logn, ...