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. 【XSY2714】大佬的难题 数学 树状数组

    题目描述 给你三个排列\(A,B,C\),求 \[ \sum_{1\leq x,y\leq n}[a_x<a_y][b_x<b_y][c_x<c_y] \] \(n\leq 2\ti ...

  2. Ionic3在ts中获取html中值的方法

    我觉得有两种方法,都是Angular中的语法,一种是把值当做参数传递,另一种是使用ngModel实现双向绑定 还有一种很少用到的,Js的原生方法:document.getElementById('ch ...

  3. IDEA 简单的正则匹配

    IDEA在进行查看或替换的时候,勾选Regex 选项就可以进行正则匹配查找了 几个简单实用的正则: 以什么开头,以什么结尾的字符串 以aa开头,以bb结尾的字符串aa.*bb 从开头到某个字符串为止的 ...

  4. 24 Zabbix系统配置日志监控告警--关键字触发

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 24 Zabbix系统配置日志监控告警--关键字触发 trapper是被监控主机主动发送数据给za ...

  5. 「SCOI2014」方伯伯的 OJ 解题报告

    「SCOI2014」方伯伯的 OJ 和列队有点像,平衡树点分裂维护即可 但是需要额外用个set之类的对编号查找点的位置 插入完了后记得splay,删除时注意特判好多东西 Code: #include ...

  6. maven将项目及第三方jar打成一个jar包

    pom.xml中添加如下配置 把依赖包和自己项目的文件打包如同一个jar包(这种方式对spring的项目不支持) <build> <plugins> <plugin> ...

  7. 为什么每次app访问服务器都建立新连接 导致服务器大量连接疯涨

    运维发现服务器有大量连接不释放,而且每次app访问都会建立新连接. netstat -antlp |grep ESTAB|grep 8080|wc -l    (访问服务器8080端口的已建立的连接数 ...

  8. docker镜像操作

    1.获取镜像 docker pull NAME[:TAG] 如果不显式地指定TAG,则默认会选择latest标签,即下载仓库中最新版本的镜像.//获取最新镜像docker pull ubuntu // ...

  9. linux free命令

    Linux上的free命令详解 free命令的所有输出值都是从/proc/meminfo中读出的 total used free shared buffers cached Mem: -/+ buff ...

  10. 第五篇:数据备份、pymysql模块

    http://www.cnblogs.com/linhaifeng/articles/7525619.html#_label3 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们 ...