HDU1255 扫描线 矩形交面积 离散化
覆盖的面积
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5595 Accepted Submission(s): 2810

注意:本题的输入数据较多,推荐使用scanf读入数据.
题意:
给出矩形左下和右上角的坐标,求矩形交的面积。
代码:
//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 扫描线 矩形交面积 离散化的更多相关文章
- HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1255 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<= ...
- hdu 1255 覆盖的面积 (扫描线求矩形交)
覆盖的面积 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)
版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...
- poj 3277 City Horizon (线段树 扫描线 矩形面积并)
题目链接 题意: 给一些矩形,给出长和高,其中长是用区间的形式给出的,有些区间有重叠,最后求所有矩形的面积. 分析: 给的区间的范围很大,所以需要离散化,还需要把y坐标去重,不过我试了一下不去重 也不 ...
- CCF 201312-3 最大的矩形 (暴力,离散化)
问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方图.例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3 ...
- HDU 5130 Signal Interference --计算几何,多边形与圆的交面积
题意: 求所有满足PB <= k*PA 的P所在区域与多边形的交面积. 解法: 2014广州赛区的银牌题,当时竟然没发现是圆,然后就没做出来,然后就gg了. 圆的一般式方程: 设A(x1,y1) ...
- Open-air shopping malls(二分半径,两元交面积)
http://acm.hdu.edu.cn/showproblem.php?pid=3264 Open-air shopping malls Time Limit: 2000/1000 MS (Jav ...
- TZOJ 2392 Bounding box(正n边形三点求最小矩形覆盖面积)
描述 The Archeologists of the Current Millenium (ACM) now and then discover ancient artifacts located ...
- hdu 1828 Picture(线段树扫描线矩形周长并)
线段树扫描线矩形周长并 #include <iostream> #include <cstdio> #include <algorithm> #include &l ...
随机推荐
- DP动态规划练习
先来看一下经典的背包问题吧 http://www.cnblogs.com/Kalix/p/7617856.html 01背包问题 https://www.cnblogs.com/Kalix/p/76 ...
- 已有海外版Office365,如何开通相同Tenant的Azure
下面这个步骤是开通海外版Azure的测试账号,请了解! 翻到如图位置 点击免费开始 下一步 输入验证代码,此页没截图 使用信用卡,需要visa或master 下一步认证完就可以使用,没有继续截图
- 机器人控制tcp通信参数调优
机器人使用WiFi通信,实现指令下传,状态上传.而WiFi信道平时带宽较稳定,但会在某些时候突然中断,造成ping的延时较高,但可以马上恢复.如果一直ping,则一般情况下ping值很小,但长时间(数 ...
- struts2之form标签theme属性详解
struts2中theme属性包括xhtml,html,simple,ajax .默认是xhtml theme:设置struts2标签的主题,默认为xhtml. theme=xhtml时:会默认额外生 ...
- 《javascript模式--by Stoyan Stefanov》书摘--汇总
<javascript模式--by Stoyan Stefanov>书摘--基本技巧 http://www.cnblogs.com/liubei/p/JavascriptModeLog1. ...
- Nodejs第一天-{Nodejs基础 深刻理解浏览器 环境变量 基础语法}
Nodejs第一天 1.什么是Nodejs Nodejs是一个可以运行(解析)ECMAScript的环境; ECMAScript是规定了一些列的语法 ,这些语法想要解析的执行就需要放在某个环境 ...
- Python 再谈字符串
字符串除了要用引号来创建之外,其他和元组一样,不能修改,如果要修改只能用切片或者拼接的方式. 其他的什么乱七八糟的运算符都一样 一些不同 capitalize()-将字符串的第一个字母大写 str1. ...
- 2d命令行小游戏源码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- Java容器之Iterator接口
Iterator 接口: 1. 所有实现了Collection接口的容器类都有一个iterator方法用以返回一个实现了Iterator接口的对象. 2. Iterator 对象称作迭代器,用以方便的 ...
- 在mysql启用远程连接
1.在ubuntu下面安装mysql. apt-get install mysql-server mysql-client -y 2.修改/etc/mysql/my.cnf文件. #bind-addr ...