hdu 1255(线段树 扫描线) 覆盖的面积
http://acm.hdu.edu.cn/showproblem.php?pid=1255
典型线段树辅助扫描线,顾名思义扫描线就是相当于yy出一条直线从左到右(也可以从上到下)扫描过去,此时先将所有的横坐标和纵坐标排序
因为是从左到右扫描,那么横坐标应该离散化一下
当扫描线依次扫描的时候,依次扫描到的纵区间在线段树中查找,依据是上边还是下边记录,上边就是-1,下边就是+1,
如果某区间记录值为0的时候,代表没有被覆盖,为1的时候代表覆盖一次,为2代表覆盖两次(不会出现为负数的情况)
最后将依次扫描的覆盖两次的纵区间长度乘以以此的横坐标的和就行
所以http://acm.hdu.edu.cn/search.php?action=listproblem hdu1542 就在稍微改改这题的代码就行了
code
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
struct node{
double x,y1,y2;
double flag;
};
node num[];
double a[];
bool cmp(node q,node w){return q.x<w.x;}
struct point {
int l,r,mark;
double lf,rf;
double once; //覆盖一次
double twice;//覆盖两次
};
point tree[*];
void build(int i,int left,int right)
{
tree[i].l=left,tree[i].r=right;
tree[i].once=tree[i].twice=;
tree[i].lf=a[left];
tree[i].rf=a[right];
if (left+==right) return ;
int mid=(left+right)/;
build(i*,left,mid);
build(i*+,mid,right);
}
void find(int i)
{
if (tree[i].mark>=)
{
tree[i].twice=tree[i].once=tree[i].rf-tree[i].lf;
return;
}
else if (tree[i].mark==)
{
tree[i].once=tree[i].rf-tree[i].lf;
if (tree[i].l+==tree[i].r) tree[i].twice=;
else tree[i].twice=tree[i*].once+tree[i*+].once;
}
else
{
if(tree[i].l+==tree[i].r)
tree[i].once=tree[i].twice=;
else
{
tree[i].once=tree[i*].once+tree[i*+].once;
tree[i].twice=tree[i*].twice+tree[i*+].twice;
}
}
}
void update(int i,node g)
{
//printf("***%d***\n",i);
if (g.y1==tree[i].lf&&g.y2==tree[i].rf)
{
tree[i].mark+=g.flag;
find(i); //更新区间
return ;
}
if (g.y2<=tree[i*].rf) update(i*,g);
else if (g.y1>=tree[i*+].lf) update(i*+,g);
else
{
node temp=g;
temp.y2=tree[i*].rf;
update(i*,temp);
temp=g;
temp.y1=tree[i*+].lf;
update(i*+,temp);
}
find(i);
}
int main()
{
int t,n,i,k;
double x1,x2,y1,y2,ans;
while (~scanf("%d",&k))
{
while (k--)
{
scanf("%d",&n);
t=;
while (n--)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
num[t].x=x1,num[t].y1=y1,num[t].y2=y2;
num[t].flag=;a[t]=y1;
t++;
num[t].x=x2,num[t].y1=y1,num[t].y2=y2;
num[t].flag=-;a[t]=y2;
t++;
}
sort(num+,num+t,cmp);
sort(a+,a+t);
ans=;
build(,,t-);
//printf("-1\n");
update(,num[]);
for (i=;i<t;i++)
{
ans+=tree[].twice*(num[i].x-num[i-].x);
update(,num[i]);
}
printf("%.2lf\n",ans);
}
}
return ;
}
hdu 1255(线段树 扫描线) 覆盖的面积的更多相关文章
- hdu 4419 线段树 扫描线 离散化 矩形面积
//离散化 + 扫描线 + 线段树 //这个线段树跟平常不太一样的地方在于记录了区间两个信息,len[i]表示颜色为i的被覆盖的长度为len[i], num[i]表示颜色i 『完全』覆盖了该区间几层. ...
- 覆盖的面积 HDU - 1255 线段树+扫描线+离散化 求特定交叉面积
#include<cstdio> #include<map> #include<algorithm> using namespace std; ; struct N ...
- hdu 1828 线段树扫描线(周长)
Picture Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- hdu 4052 线段树扫描线、奇特处理
Adding New Machine Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- POJ-1151-Atlantis(线段树+扫描线+离散化)[矩形面积并]
题意:求矩形面积并 分析:使用线段树+扫描线...因为坐标是浮点数的,因此还需要离散化! 把矩形分成两条边,上边和下边,对横轴建树,然后从下到上扫描上去,用col表示该区间有多少个下边,sum代表该区 ...
- hdu1542 线段树扫描线求矩形面积的并
题意: 给你n个正方形,求出他们的所占面积有多大,重叠的部分只能算一次. 思路: 自己的第一道线段树扫描线题目,至于扫描线,最近会写一个总结,现在就不直接在这里写了,说下我的方 ...
- hdu 5091(线段树+扫描线)
上海邀请赛的一道题目,看比赛时很多队伍水过去了,当时还想了好久却没有发现这题有什么水题的性质,原来是道成题. 最近学习了下线段树扫描线才发现确实是挺水的一道题. hdu5091 #include &l ...
- HDU 5107 线段树扫描线
给出N个点(x,y).每一个点有一个高度h 给出M次询问.问在(x,y)范围内第k小的高度是多少,没有输出-1 (k<=10) 线段树扫描线 首先离散化Y坐标,以Y坐标建立线段树 对全部的点和询 ...
- 覆盖的面积(HDU 1255 线段树)
覆盖的面积 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem D ...
随机推荐
- export default 与 export
export default 只能导出一个 可以用任意的变量来接收 export 可以暴露多个成员,需要用 import {} 接受成员 需要用名字接受 名字必须跟导出名字一致 //或者as作为别名 ...
- .html() 与.text() 获取值、取值 区别
1.html代码<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- python+爬虫+签名
在公众号,看到一个比较好玩的程序.它使用post的来传送请求,以前没有遇到过.可能是自己,写的程序太少了.查了一下post的用法: 通常,你想要发送一些编码为表单形式的数据——非常像一个 HTML 表 ...
- Recycleview 横竖屏
看到了一篇贴子:https://blog.csdn.net/yaosongqwe/article/details/48710375 //竖屏线性展示 mLlayoutmanager = new Lin ...
- 外购半成品报SHORT问题(非验货客户)
外购半成品报SHORT问题(验货客户)https://www.cnblogs.com/Snowfun/p/8660646.html 下面看非验货客户: 1.检查采购类型是否为F(SAP_MARC),为 ...
- 材料订单不在IN_MO或者IN_SCFHEADER中
select * from in_sfcheader where MO_ID IN('001600044481'); SELECT * FROM in_sfcheader_temp where MO_ ...
- metasploit framework(八):snmp扫描,暴力破解
snmp扫描linux 设置相关参数,这里使用的默认字典,你可以自己制定字典. run 然后使用枚举模块 设置相关参数 run,详细的信息就枚举出来了 snmp扫描windows 先枚举用户 设置目标 ...
- 665. Non-decreasing Array
Given an array with n integers, your task is to check if it could become non-decreasing by modifying ...
- dbcp第一次获取连接的时间问题
最近优化代码,发现第一次调用数据库连接时非常慢,往后便不再发生.经了解,数据库连接是用dbcp管理的,想在网上查找答案,但没有找到.在某人的提醒下决定研究源代码: 部分源代码如下(BasicDataS ...
- 【Django】关于前端配置
今天在网上课程了学了一下前端配置,感觉搭这个环境安装了不少东西,自己都有点混乱,现在整理一下思路: 1.nvm 即Note Version Manager用来管理node版本的工具: windows版 ...