题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255

题目大意:给你若干个矩形,让你求这些矩形重叠两次及以上的部分的面积。

解题思路:模板题,跟HDU 1542  Atlantis一样是求面积并,唯一的差别是这题要求的是重叠两次以上的面积,只要将cnt>0的条件改为cnt>1即可。

代码:

 #include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<string>
#define LC(a) (a<<1)
#define RC(a) (a<<1|1)
#define MID(a,b) ((a+b)>>1)
using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;
const int N=2e3+; struct node{
double x1,x2,h,flag;
node(){}
node(double a,double b,double c,double d){
x1=a;x2=b;h=c;flag=d;
}
}a[N]; struct Node{
int l,r,cnt;
double sum;
}tree[*N]; double X[N]; bool cmp(node a,node b){
return a.h<b.h;
} int bin_search(double num,int l,int r){
while(l<=r){
int mid=(l+r)/;
if(X[mid]==num)
return mid;
else if(X[mid]<num)
l=mid+;
else
r=mid-;
}
} void pushup(int p){
tree[p].cnt=(tree[LC(p)].cnt==tree[RC(p)].cnt?tree[LC(p)].cnt:-);
tree[p].sum=tree[LC(p)].sum+tree[RC(p)].sum;
} void pushdown(int p){
tree[LC(p)].cnt=tree[RC(p)].cnt=tree[p].cnt;
//由cnt>0改为cnt>1
if(tree[p].cnt<=)
tree[LC(p)].sum=tree[RC(p)].sum=;
else{
tree[LC(p)].sum=X[tree[LC(p)].r+]-X[tree[LC(p)].l];
tree[RC(p)].sum=X[tree[RC(p)].r+]-X[tree[RC(p)].l];
}
} void build(int p,int l,int r){
tree[p].l=l;
tree[p].r=r;
tree[p].cnt=;
if(l==r){
tree[p].sum=;
return;
}
build(LC(p),l,MID(l,r));
build(RC(p),MID(l,r)+,r);
pushup(p);
} void update(int p,int l,int r,int cnt){
if(l>tree[p].r||r<tree[p].l)
return;
if(l<=tree[p].l&&r>=tree[p].r&&tree[p].cnt!=-){
tree[p].cnt+=cnt;
if(tree[p].cnt>=)
tree[p].sum=X[tree[p].r+]-X[tree[p].l];
else
tree[p].sum=;
return;
}
if(tree[p].cnt!=-)
pushdown(p);
update(LC(p),l,r,cnt);
update(RC(p),l,r,cnt);
pushup(p);
} int main(){
int T;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
int m1=,m2=;
for(int i=;i<=n;i++){
double x1,y1,x2,y2;
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
X[++m1]=x1;
a[m1]=node(x1,x2,y1,);
X[++m1]=x2;
a[m1]=node(x1,x2,y2,-);
}
//横坐标离散化
sort(a+,a++m1,cmp);
sort(X+,X++m1);
for(int i=;i<=m1;i++){
if(X[i]!=X[i-])
X[++m2]=X[i];
}
build(,,m2-);
double ans=;
//依次读入扫描线求重叠两次及以上的面积并
for(int i=;i<=m1-;i++){
int l=bin_search(a[i].x1,,m2);
int r=bin_search(a[i].x2,,m2)-;
update(,l,r,a[i].flag);
ans+=tree[].sum*(a[i+].h-a[i].h);
}
printf("%.2lf\n",ans);
}
return ;
}

HDU 1255 覆盖的面积(线段树:扫描线求面积并)的更多相关文章

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

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

  2. hdu1542 Atlantis 线段树--扫描线求面积并

    There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...

  3. hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]

    Atlantis Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total S ...

  4. HDU 4419 Colourful Rectangle --离散化+线段树扫描线

    题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...

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

    矩形面积并 线段树-扫描线裸题 #include<stdio.h> #include<string.h> #include<algorithm> #include& ...

  6. HDU 1828“Picture”(线段树+扫描线求矩形周长并)

    传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...

  7. hdu1542 线段树扫描线求矩形面积的并

    题意:       给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次. 思路:       自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方 ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题意很清楚,就是让你求矩阵之间叠加层数大于1的矩形块的面积和. 因为n只有1000,所以我离散化 ...

  9. UVA-11983-Weird Advertisement(线段树+扫描线)[求矩形覆盖K次以上的面积]

    题意: 求矩形覆盖K次以上的面积 分析: k很小,可以开K颗线段树,用sum[rt][i]来保存覆盖i次的区间和,K次以上全算K次 // File Name: 11983.cpp // Author: ...

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

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

随机推荐

  1. 【CF601C】Kleofáš and the n-thlon

    Portal -->CF601C Description 大概是说\(m\)个人参加\(n\)场比赛,每场一人有一个排名,每场没有两个人排名相同,一个人最后的得分是\(n\)场比赛的排名相加,现 ...

  2. 2018-2019 ACM-ICPC 徐州区域赛 部分题解

    题目链接:2018-2019 ACM-ICPC, Asia Xuzhou Regional Contest A. Rikka with Minimum Spanning Trees 题意: 给出一个随 ...

  3. 回顾static与final的作用

    static是java中非常重要的一个关键字,而且它的用法也很丰富,主要有四种用法: 用来修饰成员变量,将其变为类的成员,从而实现所有对象对于该成员的共享: 用来修饰成员方法,将其变为类方法,可以直接 ...

  4. 9.Android UiAutomator正则表达式的使用

    一.正则表达式元字符: 1.一些常用元字符: 元字符 描述 . 表示任意一个字符 \s 空格字符(空格键.tab.换行.换页.回车) \S 非空字符串([^\s]) \d 一个数字(相当于[0-9]中 ...

  5. 手脱JDPack

    1.PEID查壳 JDPack 2.载入OD,入口是一个pushad入栈,可以使用ESP,下硬件访问断点,shift+F9 0040E000 > pushad ; //入口 0040E001 E ...

  6. array_intersect

    <?php date_default_timezone_set('Asia/Shanghai'); $a1=array("a"=>"red",&qu ...

  7. 「Python」python日期与字符串互转

    1str -> date import datetime detester = ‘2017-01-01' date = datetime.datetime.strptime(detester,’ ...

  8. [DeeplearningAI笔记]卷积神经网络1.2-1.3边缘检测

    4.1卷积神经网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 1.2边缘检测示例 边缘检测可以视为横向边缘检测和纵向边缘检测如下图所示: 边缘检测的原理是通过一个特定构造的卷积核对原始图 ...

  9. Arrays.asList方法遇到的问题

    在使用Arrays.asList(T...a)方法时,遇到了 java.lang.UnsupportedOperationException  异常. 后来发现,该方法返回的类型是Arrays$Arr ...

  10. HDU 6205 尺取

    容易看出来,扩增一倍,找最长的区间就行了 /** @Date : 2017-09-11 12:43:11 * @FileName: 1012.cpp * @Platform: Windows * @A ...