题意:

      给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次。

思路:

      自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方法,我是离散化横坐标,然后去扫描纵坐标(反过来也行),把每一个长方形的两个宽边拿出来,按照高度排序,然后开始扫描,对于么一个区间的更新,我用的是暴力点更新,因为如果写段更新的话第二个权值没有想到什么好的一起更新的方法。

然后在做另一道题目的时候发现这个方法超时了,没办法又去硬着头皮学了段更新的,段更新的扫描线应该大体有两种,我学了其中一个,然后又用段更新的做了下这个题目。


一开始的暴力区间扫描


#include<stdio.h>

#include<string.h>
#include<algorithm> #define lson l ,mid ,t << 1
#define rson mid + 1 ,r ,t <<1 | 1

using namespace
std; typedef struct
{
double
l ,r ,h;
int
mk;
}
EDGE; EDGE edge[100*2+10];
double
sum[100*2*4+10];
double
tmp[100*2+10];
double
num[100*2+10];
int
now[100*2+10]; bool camp(EDGE a ,EDGE b)
{
return
a.h < b.h;
} void
Pushup(int t)
{

sum[t] = sum[t<<1] + sum[t<<1|1];
} void
Update(int l ,int r ,int t ,int a ,int b)
{
if(
l == r)
{

now[a] += b;
if(
now[a]) sum[t] = num[a+1] - num[a];
else
sum[t] = 0;
return;
}
int
mid = (l + r) >> 1;
if(
a <= mid) Update(lson ,a ,b);
else
Update(rson ,a ,b);
Pushup(t);
} int
search_2(int n ,double now)
{
int
low ,up ,mid ,ans;
low = 0 ,up = n;
while(
low <= up)
{

mid = (low + up) >> 1;
if(
now <= num[mid])
{

ans = mid;
up = mid - 1;
}
else
low = mid + 1;
}
return
ans;
} int main ()
{
int
n ,i ,j ,cas = 1;
double
x1 ,x2 ,y1 ,y2;
while(~
scanf("%d" ,&n) && n)
{
int
id = 0;
for(
i = 1 ;i <= n ;i ++)
{

scanf("%lf %lf %lf %lf" ,&x1 ,&y1 ,&x2 ,&y2);
edge[++id].l = x1;
edge[id].r = x2 ,edge[id].h = y1 ,edge[id].mk = 1;
tmp[id] = x1;
edge[++id].l = x1;
edge[id].r = x2 ,edge[id].h = y2 ,edge[id].mk = -1;
tmp[id] = x2;
}

sort(tmp + 1 ,tmp + id + 1);
sort(edge + 1 ,edge + id + 1 ,camp);
tmp[0] = -1;
for(
id = 0 ,i = 1 ;i <= n * 2 ;i ++)
if(
tmp[i] != tmp[i-1]) num[++id] = tmp[i];
memset(now ,0 ,sizeof(now));
memset(sum ,0 ,sizeof(sum));
double
ans = 0;
edge[0].h = edge[1].h;
for(
i = 1 ;i <= n * 2 ;i ++)
{

ans += sum[1] * (edge[i].h - edge[i-1].h);
int
l = search_2(id ,edge[i].l);
int
r = search_2(id ,edge[i].r) - 1;
for(
j = l ;j <= r ;j ++)
Update(1 ,id - 1 ,1 ,j ,edge[i].mk);
}

printf("Test case #%d\n" ,cas ++);
printf("Total explored area: %.2lf\n\n" ,ans);
}
return
0;
}

后来的段更新扫描

#include<stdio.h>
#include<string.h>
#include<algorithm> #define lson l ,mid ,t << 1
#define rson mid , r ,t << 1 | 1

using namespace
std; typedef struct
{
double
l ,r ,h;
int
mk;
}
EDGE; EDGE edge[1000]; double len[1000];
double
tmp[1000] ,num[1000];
int
cnt[1000]; bool camp(EDGE a ,EDGE b)
{
return
a.h < b.h;
} void
Pushup(int l ,int r ,int t)
{
if(
cnt[t]) len[t] = num[r] - num[l];
else if(
l + 1 == r) len[t] = 0;
else
len[t] = len[t<<1] + len[t<<1|1];
}
void
Update(int l ,int r ,int t ,int a ,int b ,int c)
{
if(
l == a && r == b)
{

cnt[t] += c;
Pushup(l ,r ,t);
return;
}
int
mid = (l + r) >> 1;
if(
b <= mid) Update(lson ,a ,b ,c);
else if(
a >= mid) Update(rson ,a ,b ,c);
else
{

Update(lson ,a ,mid ,c);
Update(rson ,mid,b ,c);
}

Pushup(l ,r ,t);
} int
search_2(int id ,double now)
{
int
low ,up ,mid ,ans;
low = 1 ,up = id;
while(
low <= up)
{

mid = (low + up) >> 1;
if(
now <= num[mid])
{

ans = mid;
up = mid - 1;
}
else
low = mid + 1;
}
return
ans;
} int main ()
{
int
id ,n ,i ,cas = 1;
double
x1 ,x2 ,y1 ,y2;
while(~
scanf("%d" ,&n) && n)
{
for(
id = 0 ,i = 1 ;i <= n ;i ++)
{

scanf("%lf %lf %lf %lf" ,&x1 ,&y1 ,&x2 ,&y2);
edge[++id].l = x1;
edge[id].r = x2 ,edge[id].h = y1 ,edge[id].mk = 1;
tmp[id] = x1;
edge[++id].l = x1;
edge[id].r = x2 ,edge[id].h = y2 ,edge[id].mk = -1;
tmp[id] = x2;
}

sort(tmp + 1 ,tmp + id + 1);
sort(edge + 1 ,edge + id + 1 ,camp);
memset(len ,0 ,sizeof(len));
memset(cnt ,0 ,sizeof(cnt));
tmp[0] = -1;
for(
id = 0 ,i = 1 ;i <= n * 2 ;i ++)
if(
tmp[i] != tmp[i-1]) num[++id] = tmp[i];
double
ans = 0;
edge[0].h = edge[1].h;
for(
i = 1 ;i <= n * 2 ;i ++)
{

ans += len[1] * (edge[i].h - edge[i-1].h);
int
l = search_2(id ,edge[i].l);
int
r = search_2(id ,edge[i].r);
Update(1 ,id ,1 ,l ,r ,edge[i].mk);
}

printf("Test case #%d\n" ,cas ++);
printf("Total explored area: %.2lf\n\n" ,ans);
}
return
0;
}



hdu1542 线段树扫描线求矩形面积的并的更多相关文章

  1. 2015 UESTC 数据结构专题E题 秋实大哥与家 线段树扫描线求矩形面积交

    E - 秋实大哥与家 Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/59 De ...

  2. HDU 1542"Atlantis"(线段树+扫描线求矩形面积并)

    传送门 •题意 给你 n 矩形,每个矩形给出你 $(x_1,y_1),(x_2,y_2)$ 分别表示这个矩形的左下角和右上角坐标: 让你求这 n 个矩形并的面积: 其中 $x \leq 10^{5} ...

  3. hdu1828 线段树扫描线求矩形面积的周长

    题意:       给你n个矩形,问你这n个矩形所围成的图形的周长是多少. 思路:       线段树的扫描线简单应用,这个题目我用的方法比较笨,就是扫描两次,上下扫描,求出多边形的上下边长和,然后同 ...

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

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

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

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

  6. POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]

    题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...

  7. 扫描线 + 线段树 : 求矩形面积的并 ---- hnu : 12884 Area Coverage

    Area Coverage Time Limit: 10000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit user ...

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

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

  9. hdu 4419 线段树 扫描线 离散化 矩形面积

    //离散化 + 扫描线 + 线段树 //这个线段树跟平常不太一样的地方在于记录了区间两个信息,len[i]表示颜色为i的被覆盖的长度为len[i], num[i]表示颜色i 『完全』覆盖了该区间几层. ...

随机推荐

  1. 因MemoryCache闹了个笑话

    前言 是这么一回事: 我正在苦思一个业务逻辑,捋着我还剩不多的秀发,一时陷入冥想中...... 突然聊天图标一顿猛闪,打开一看,有同事语音: 大概意思是:同事把项目中Redis部分缓存换成Memory ...

  2. EurekaServer源码分析

    Eureka Server功能 接受服务注册 接受服务心跳 服务剔除 服务下线 集群同步 获取注册表中服务实例信息 需要注意的是,Eureka Server同时也是一个Eureka Client,在不 ...

  3. C# 应用 - 使用 HttpClient 发起上传文件、下载文件请求

    1. 示例代码 using System; using System.IO; using System.Net.Http; /// <summary> /// 下载文件 /// </ ...

  4. [NOIP 2020] 微信步数

    一.题目 点此看题 二.题目 首先感谢一下这位大佬的博客,虽然我看不懂您的讲解,但是还是读得懂代码的 思路是 \(\tt jys\) 给我讲明白的,首先我们可以感觉到快速计算它肯定和矩形有关系,也就是 ...

  5. Codeforces 1015E1 Stars Drawing (Easy Edition)

    题面: 传送门 题目描述: 要求用十字星星来画题目给出的"星"图.如果不能用十字星星来画"星"图,输出-1:如果能,则输出要在图的哪个位置画相应大小的十字星图. ...

  6. 最权威最简明的maven 使用教程

    Maven是一个项目管理工具,它包含了一个项目对象模型 (Project Object Model),一组标准集合,一个项目生命周期(Project Lifecycle),一个依赖管理系统(Depen ...

  7. Python中类的特殊属性和魔术方法

    1.属性 属性 含义 __name__ 类.函数.方法等的名字   __dir__ __module__ 类定义所在的模块名 __class__ 对象或类所属的类   只是返回基类 __bases__ ...

  8. 使用 DD 命令制作 USB 启动盘

    Windows 下有很多很好用的 USB 启动盘制作工具,比如 Rufus,但是 MacOS 下这个类型的工具就少了很多,这里记录下在 MacOS 中用 DD 命令制作 Linux USB 启动盘的操 ...

  9. 攻防世界 reverse Replace

    Replace 湖湘杯2018 查壳upx,手动脱壳,修复IAT,去掉重定向便可以运行. ida查看,流程清晰.关键函数check_E51090. int __cdecl main(int argc, ...

  10. 攻防世界 reverse reverse-for-the-holy-grail-350

    reverse-for-the-holy-grail-350   tu-ctf-2016 程序流程很简单,就一个检验函数: 1 __int64 __fastcall stringMod(__int64 ...