HDU 1255 覆盖的面积(线段树:扫描线求面积并)
题目链接: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 覆盖的面积(线段树:扫描线求面积并)的更多相关文章
- HDU 1264 Counting Squares (线段树-扫描线-矩形面积并)
版权声明:欢迎关注我的博客.本文为博主[炒饭君]原创文章,未经博主同意不得转载 https://blog.csdn.net/a1061747415/article/details/25471349 P ...
- hdu1542 Atlantis 线段树--扫描线求面积并
There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some ...
- hdu 1542&&poj 1151 Atlantis[线段树+扫描线求矩形面积的并]
Atlantis Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 4419 Colourful Rectangle --离散化+线段树扫描线
题意: 有三种颜色的矩形n个,不同颜色的矩形重叠会生成不同的颜色,总共有R,G,B,RG,RB,GB,RGB 7种颜色,问7种颜色每种颜色的面积. 解法: 很容易想到线段树扫描线求矩形面积并,但是如何 ...
- hdu1255 覆盖的面积 线段树-扫描线
矩形面积并 线段树-扫描线裸题 #include<stdio.h> #include<string.h> #include<algorithm> #include& ...
- HDU 1828“Picture”(线段树+扫描线求矩形周长并)
传送门 •参考资料 [1]:算法总结:[线段树+扫描线]&矩形覆盖求面积/周长问题(HDU 1542/HDU 1828) •题意 给你 n 个矩形,求矩形并的周长: •题解1(两次扫描线) 周 ...
- hdu1542 线段树扫描线求矩形面积的并
题意: 给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次. 思路: 自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方 ...
- HDU 1255 覆盖的面积 (线段树+扫描线+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1255 题意很清楚,就是让你求矩阵之间叠加层数大于1的矩形块的面积和. 因为n只有1000,所以我离散化 ...
- UVA-11983-Weird Advertisement(线段树+扫描线)[求矩形覆盖K次以上的面积]
题意: 求矩形覆盖K次以上的面积 分析: k很小,可以开K颗线段树,用sum[rt][i]来保存覆盖i次的区间和,K次以上全算K次 // File Name: 11983.cpp // Author: ...
- HDU1255 覆盖的面积 —— 求矩形交面积 线段树 + 扫描线 + 离散化
题目链接:https://vjudge.net/problem/HDU-1255 给定平面上若干矩形,求出被这些矩形覆盖过至少两次的区域的面积. Input输入数据的第一行是一个正整数T(1<= ...
随机推荐
- 表格隔行变色_jQuery控制实现鼠标悬停高亮
<!doctype html> <html> <head> <meta http-equiv="content-type" content ...
- Codeforces 585D. Lizard Era: Beginning(meet in the middle)
一眼题...这个数据范围也太明显了吧... suma1==suma2 && sumb1==sumb2 && sumc1==sumc2 相当于suma1-sumb1==s ...
- 【agc003E】Sequential operations on Sequence
Portal -->agc003E Description 给你一个数串\(S\),一开始的时候\(S=\{1,2,3,...,n\}\),现在要对其进行\(m\)次操作,每次操作给定一个\(a ...
- syntax error: non-declaration statement outside function body
在函数外部使用形如:name:="mark"这样语句会出现 syntax error: non-declaration statement outside function bod ...
- Codeforces Round #412 (rated, Div. 2, base on VK Cup 2017 Round 3) A B C D 水 模拟 二分 贪心
A. Is it rated? time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- bzoj 2795 [Poi2012]A Horrible Poem hash+数论
2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec Memory Limit: 128 MBSubmit: 640 Solved: 322[Subm ...
- C/C++ Volatile关键词深度剖析
文章来源:http://hedengcheng.com/?p=725 背景 此微博,引发了朋友们的大量讨论:赞同者有之:批评者有之:当然,更多的朋友,是希望我能更详细的解读C/C++ Volatile ...
- Linux下如何卸载软件(Debian系)
说明:此方法适用于Debian.Ubuntu等带apt工具的操作系统. 步骤: 1.首先我们需要知道将要卸载的软件名称,比如我现在打算卸载tightvncserver,但是如果你不确定名称,没关系,可 ...
- 2-sat HDU 1814
题解来自于:http://www.cnblogs.com/kuangbin/archive/2012/10/05/2712622.html 和平委员会 根据宪法,Byteland民主共和国的公众和平委 ...
- 安装HDP时的报错信息
1,安装ambari时报错:Bootstrap process timed out. It will be destroyed. 报错原因:/etc/sudoers文件中未设置免密权限 解决办法:ha ...