https://cn.vjudge.net/problem/HDU-1255

题意

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

分析

求面积并的题:https://www.cnblogs.com/fht-litost/p/9580330.html

这题求面积交,也就是cover>=2才计算,采用第一种方法就只用小小改动。

以下用了第二种方法。这里得维护覆盖一次以上的长度,和覆盖两次以上的长度。重点在cal()函数。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <set>
#include <bitset>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define ms(a, b) memset(a, b, sizeof(a))
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define eps 0.0000000001
#define IOS ios::sync_with_stdio(0);cin.tie(0);
#define random(a, b) rand()*rand()%(b-a+1)+a
#define pi acos(-1)
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
const int maxn = + ;
const int maxm = + ;
const int mod = ; int n;
double y[maxn];
struct LINE{
double x;
double y1,y2;
int flag;
bool operator <(const LINE &a)const{
return x<a.x;
}
}line[maxn];
struct ND{
double l,r;
int cover;
bool f;
double one;//覆盖一次以上的长度
double more;//覆盖两次以上的长度
}tree[maxn<<];
void build(int rt,int l,int r){
tree[rt].l=y[l];
tree[rt].r=y[r];
tree[rt].cover=;
tree[rt].one=tree[rt].more=;
tree[rt].f=false;
if(l+==r){
tree[rt].f=true;
return;
}
int mid = (l+r)>>;
build(rt<<,l,mid);
build(rt<<|,mid,r);
} void cal(int rt){
if(tree[rt].cover>=){
tree[rt].more=tree[rt].one=tree[rt].r-tree[rt].l;
}else if(tree[rt].cover==){
tree[rt].one=tree[rt].r-tree[rt].l;
if(tree[rt].f) tree[rt].more=;
else tree[rt].more=tree[rt<<].one+tree[rt<<|].one;
}else{
if(tree[rt].f){
tree[rt].more=tree[rt].one=;
}else{
tree[rt].one=tree[rt<<].one+tree[rt<<|].one;
tree[rt].more=tree[rt<<].more+tree[rt<<|].more;
}
}
}
void update(int rt,double l,double r,int flag){
if(l==tree[rt].l&&r==tree[rt].r){
tree[rt].cover+=flag;
cal(rt);
return;
}
if(tree[rt<<].r>=r) update(rt<<,l,r,flag);
else if(l>=tree[rt<<|].l) update(rt<<|,l,r,flag);
else{
update(rt<<,l,tree[rt<<].r,flag);
update(rt<<|,tree[rt<<|].l,r,flag);
}
cal(rt);
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int T,cas=;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
int cnt=-;
double x1,x2,y1,y2;
for(int i=;i<n;i++){
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
y[++cnt]=y1;
line[cnt].x=x1;
line[cnt].y1=y1;
line[cnt].y2=y2;
line[cnt].flag=;
y[++cnt]=y2;
line[cnt].x=x2;
line[cnt].y1=y1;
line[cnt].y2=y2;
line[cnt].flag=-;
}
sort(y,y++cnt);
sort(line,line++cnt);
build(,,cnt);
update(,line[].y1,line[].y2,line[].flag);
double area=;
for(int i=;i<=cnt;i++){
area+=tree[].more*(line[i].x-line[i-].x);
update(,line[i].y1,line[i].y2,line[i].flag);
}
printf("%.2f\n",area);
}
return ;
}

HDU - 1255 覆盖的面积 (线段树求面积交)的更多相关文章

  1. HDU 1264 Counting Squares(线段树求面积的并)

    Counting Squares Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. hdu 1754 I Hate It (线段树求区间最值)

    HDU1754 I Hate It Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

  3. hdu 1255 覆盖的面积 (线段树处理面积覆盖问题(模板))

    http://acm.hdu.edu.cn/showproblem.php?pid=1255 覆盖的面积 Time Limit: 10000/5000 MS (Java/Others)    Memo ...

  4. HDU 1828 / POJ 1177 Picture --线段树求矩形周长并

    题意:给n个矩形,求矩形周长并 解法:跟求矩形面积并差不多,不过线段树节点记录的为: len: 此区间线段长度 cover: 此区间是否被整个覆盖 lmark,rmark: 此区间左右端点是否被覆盖 ...

  5. hdu-1255(线段树求面积并)模板

    题目链接:传送门 思路: (1)建立线段的信息,每个线段存储l到r的线段的x位置和y的起始点与终点. 建立线段树的节点信息,每个节点代表一个区间的信息,x表示区间的横坐标的位置,l,r表示纵坐标的范围 ...

  6. HDU 3642 Get The Treasury ( 线段树 求长方体体积并 )

    求覆盖三次及其以上的长方体体积并. 这题跟 http://wenku.baidu.com/view/d6f309eb81c758f5f61f6722.html 这里讲的长方体体积并并不一样. 因为本题 ...

  7. HDU - 1542 Atlantis(线段树求面积并)

    https://cn.vjudge.net/problem/HDU-1542 题意 求矩形的面积并 分析 点为浮点数,需要离散化处理. 给定一个矩形的左下角坐标和右上角坐标分别为:(x1,y1).(x ...

  8. [HDU] 1394 Minimum Inversion Number [线段树求逆序数]

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

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

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

随机推荐

  1. 「2017 Multi-University Training Contest 2」2017多校训练2

    1001 Is Derek lying 题目链接 HDU6045 Is Derek lying? 给出两个人选择题的回答,问得分分别为x和y是否可能.(\(1\le N \le 80000,0\le ...

  2. Hdoj 2717.Catch That Cow 题解

    Problem Description Farmer John has been informed of the location of a fugitive cow and wants to cat ...

  3. stm32 HAL库编程驱动控制文件<stm32f1xx_hal_conf.h>的使能方法

    @2019-01-25 [小记] 工具化.易控制.易配置.效率高: 驱动控制文件<stm32f1xx_hal_conf.h>中驱动模块使能由STM32CubeMX工具配置使能

  4. js中获取css样式属性值

    关于js中style,currentStyle和getComputedStyle几个注意的地方 (1)用js的style只能获取元素的内联样式,内部样式和外部样式使用style是获取不到的.针对css ...

  5. 「2017 山东三轮集训 Day7 解题报告

    「2017 山东三轮集训 Day7」Easy 练习一下动态点分 每个点开一个线段树维护子树到它的距离 然后随便查询一下就可以了 注意线段树开大点... Code: #include <cstdi ...

  6. luogu3811 乘法逆元

    逆元定义:若a*x=1(mod p),(a,p互质),则x为a mod p意义下的逆元 做法见https://www.luogu.org/blog/zjp-shadow/cheng-fa-ni-yua ...

  7. DBC格式解析(以文本形式打开)

    我们先来看一段数据 BO_ 1024 VOLTAGE01: 8 BMS2 SG_ V01 : 7|16@0+ (0.001,0) [0|0] "" Vector__XXX SG_ ...

  8. 2019西北工业大学程序设计创新实践基地春季选拔赛(重现赛) Chino with Equation(组合公式)

    链接:https://ac.nowcoder.com/acm/contest/553/D来源:牛客网 题目描述 Chino的数学很差,因此Cocoa非常担心.今天,Cocoa要教Chino解不定方程. ...

  9. jackson json转对象 对象转json

    一,Jackson使用示例 第1步:创建ObjectMapper对象. 创建ObjectMapper对象.它是一个可重复使用的对象. ObjectMapper mapper = new ObjectM ...

  10. 点击a标签不跳转的办法

    方法1: <a href="http://www.baidu.com" onclick="return false"></a> 方法2: ...