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<= ...
随机推荐
- SCWS中文分词,功能函数实例应用
结合前文的demo演示,现写一个实用的功能函数,使用方法:header('Content-Type:text/html;charset=UTF-8');$text = '我是一个中国人, ...
- 【DP】【CF855C】 Helga Hufflepuff's Cup
Description 给你一个树,可以染 \(m\) 个颜色,定义一个特殊颜色 \(k\) , 要求保证整棵树上特殊颜色的个数不超过 \(x\) 个.同时,如果一个节点是特殊颜色,那么它的相邻节点的 ...
- Flash平台的分析与RIA的趋势
10月3号,Flash Player 11 和 AIR 3.0正式提供下载,一片安静.最近这两年来,关于Flash的新闻一向是以负面为主,先是 Silverlight 的挑战,然后是 iphone和i ...
- laravel调试神器tinker
一直以来,想调试框架中的某些东西,如想知道 Elpquent 的 create 方法返回值是个什么东西, 以前的话,应该就是在 create 方法调用之后,使用 dd 或者 var_dump 之类的函 ...
- 「Python」pandas入门教程
pandas适合于许多不同类型的数据,包括: 具有异构类型列的表格数据,例如SQL表格或Excel数据 有序和无序(不一定是固定频率)时间序列数据. 具有行列标签的任意矩阵数据(均匀类型或不同类型) ...
- HDU 1812 polya 大数
由于反射的存在,分奇偶讨论其置换的循环节数量,大数用JAVA就好了. import java.math.*; import java.util.*; public class Main{ public ...
- 为eclipse配置javap命令
javap命令经常使用来对java类文件来进行反编译,主要用来对java进行分析的工具,在学习Thinking in Java时,因为须要对类文件反编译.以查看jvm究竟对我们写的代码做了哪些优化和处 ...
- XML & JSON---iOS-Apple苹果官方文档翻译
技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong //转载请注明出处--本文永久链接 ...
- laravel artisan 工具心得
介绍一些非常好用的命令: 1.创建一个Eloquent模型:顺便创建一个对应的数据库表 php artisan make:model --migration Models/Admin/test 2.将 ...
- maven使用过程中遇到的问题总汇
1:web.xml is missing and <failOnMissingWebXml> is set to true 造成原因: 使用maven创建项目时有时在pom.xml的war ...