zoukankan
html css js c++ java
PKU1151线段树解法
模仿别人的代码写的
这题不用线段树也可以过
#include
<
stdio.h
>
#include
<
string
.h
>
#include
<
stdlib.h
>
struct
line
{
double
x, y1, y2;
int
in
;
}
;
struct
node
{
node
*
pl,
*
pr;
int
iL, iR;
double
left, right;
double
y_len;
int
in
;
}
mem[
404
];
line l[
202
];
double
y[
202
];
int
n;
double
sum;
int
nodeCnt;
node
*
newNode()
{
node
*
pt
=&
mem[nodeCnt
++
];
memset(pt,
0
,
sizeof
(pt));
return
pt;
}
node
*
buildTree(
int
il,
int
ir)
{
node
*
root
=
newNode();
root
->
iL
=
il;
root
->
iR
=
ir;
root
->
left
=
y[il];
root
->
right
=
y[ir];
if
(ir
-
il
>
1
)
{
int
mid
=
(il
+
ir)
/
2
;
root
->
pl
=
buildTree(il,mid);
root
->
pr
=
buildTree(mid,ir);
}
return
root;
}
void
length(node
*
root)
{
root
->
y_len
=
0
;
if
(root
->
in
)
{
root
->
y_len
=
root
->
right
-
root
->
left;
}
else
{
if
(root
->
pl)
root
->
y_len
+=
root
->
pl
->
y_len;
if
(root
->
pr)
root
->
y_len
+=
root
->
pr
->
y_len;
}
}
void
update(node
*
root, line ll)
{
if
(root
->
left
==
ll.y1
&&
root
->
right
==
ll.y2)
{
root
->
in
+=
ll.
in
;
length(root);
return
;
}
if
(root
->
iR
-
root
->
iL
<=
1
)
return
;
if
(root
->
pl
->
right
>
ll.y1)
{
if
(root
->
pl
->
right
>=
ll.y2)
update(root
->
pl, ll);
else
{
line temp
=
ll;
temp.y2
=
root
->
pl
->
right;
update(root
->
pl, temp);
temp
=
ll;
temp.y1
=
root
->
pr
->
left;
update(root
->
pr,temp);
}
}
else
{
update(root
->
pr, ll);
}
length(root);
}
int
cmp1(
const
void
*
a ,
const
void
*
b)
{
return
(
*
(line
*
)a).x
>
(
*
(line
*
)b).x
?
1
:
-
1
;
}
int
cmp2(
const
void
*
a,
const
void
*
b)
{
return
(
*
(
double
*
)a)
>
(
*
(
double
*
)b)
?
1
:
-
1
;
}
int
main()
{
int
T
=
1
;
while
(scanf(
"
%d
"
,
&
n)
==
1
&&
n)
{
nodeCnt
=
0
;
memset(l,
0
,
sizeof
(l));
memset(y,
0
,
sizeof
(y));
memset(mem,
0
,
sizeof
(mem));
int
i;
for
(i
=
0
; i
<
n; i
++
)
{
scanf(
"
%lf%lf%lf%lf
"
,
&
l[
2
*
i].x,
&
l[
2
*
i].y1,
&
l[
2
*
i
+
1
].x,
&
l[
2
*
i
+
1
].y2);
l[
2
*
i].y2
=
l[
2
*
i
+
1
].y2, l[
2
*
i
+
1
].y1
=
l[
2
*
i].y1;
l[
2
*
i].
in
=
1
, l[
2
*
i
+
1
].
in
=-
1
;
y[
2
*
i]
=
l[
2
*
i].y1, y[
2
*
i
+
1
]
=
l[
2
*
i].y2;
}
qsort(l,
2
*
n,
sizeof
(l[
0
]), cmp1);
qsort(y,
2
*
n,
sizeof
(y[
0
]), cmp2);
node
*
root
=
buildTree(
0
,
2
*
n
-
1
);
sum
=
0
;
for
(i
=
0
; i
<
2
*
n
-
1
; i
++
)
{
update(root, l[i]);
sum
+=
(root
->
y_len)
*
(l[i
+
1
].x
-
l[i].x);
}
printf(
"
Test case #%d\nTotal explored area: %.2f\n\n
"
,T
++
,sum);
}
return
0
;
}
查看全文
相关阅读:
SpringCloud Alibaba开篇:SpringCloud这么火,为何还要学习SpringCloud Alibaba?
SpringBoot整合原生OpenFegin的坑(非SpringCloud)
Git入门教程,详解Git文件的四大状态
全世界最强的算法平台codeforces究竟有什么魅力?
设计模式第二篇,链式方法模式
matplotlib设置颜色、标记、线条,让你的图像更加丰富
20行代码实现,使用Tarjan算法求解强连通分量
深入理解SVM,详解SMO算法
手把手教你配置git和git仓库
设计模式 | Catalog设计模式,抵御业务方需求变动
原文地址:https://www.cnblogs.com/SQL/p/934721.html
最新文章
JDK8中的新时间API:Duration Period和ChronoUnit介绍
java安全编码指南之:文件和共享目录的安全性
java安全编码指南之:序列化Serialization
java安全编码指南之:文件IO操作
万字长文深入理解java中的集合-附PDF下载
java安全编码指南之:线程安全规则
聊聊Java中的异常及处理
drf JWT认证模块与自定制
了解JWT认证
drf 自动生成接口文档
热门文章
drf 分页组件
drf ModelSerializer高级使用
【性能优化】纳尼?内存又溢出了?!是时候总结一波了!!
【面经】面试官:如何以最高的效率从MySQL中随机查询一条记录?
【面经】面试官:做过性能优化的工作吗?你会从哪些方面入手做性能优化呢?
【面经】面试官:讲讲类的加载、链接和初始化?
【面经】面试官:如果让你设计一个高并发的消息中间件,你会怎么做?
## 【分布式事务】面试官问我:MySQL中的XA事务崩溃了如何恢复??
面试官问我:看过sharding-jdbc的源码吗?我吧啦吧啦说了一通!!
系统从初期到支撑亿级流量,都经历了哪些架构的变迁?
Copyright © 2011-2022 走看看