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
;
}
查看全文
相关阅读:
Swift高级语法学习总结(转)
Swift基础语法学习总结(转)
Go语言语法汇总(转)
IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0)(转)
修正iOS从照相机和相册中获取的图片方向(转)
解决小米、红米及其他 Android 手机无法在 Mac 下进行真机调试的问题(转)
mac os 禁止apache httpd自动启动(转)
rethinkdb的dataexplorer查询使用
中文 iOS/Mac 开发博客列表(转)
mac上eclipse用gdb调试(转)
原文地址:https://www.cnblogs.com/SQL/p/934721.html
最新文章
mysql 触发器
mysql 存储过程
mysql视图
tornado结合前端进行文件上传
CSRF
Tornadao Cookie
windows搭建安装react-native环境
升级CocoaPod遇到ERROR: While executing gem ... (TypeError) no implicit conversion of nil into String问题的解决方法
PHP的一些开源项目网站
MySQL中int类型的字段使用like查询方法
热门文章
(转)sqlplus中文显示乱码的问题
( [原创] 4s摄像头出现的问题及解决办法集锦。
apache开启.htaccess及.htaccess的使用方法(转)
cocoapods使用一直Updating local specs repositories的解决方案
iOS UIButton 设置图片不变型setImage
docker私有库UI和添加私有库到本机能够push和pull
建立docker私有库(docker registry)(转)
在MAC服务器上搭建docker-registry(转)
docker设置并运行部分命令及原文
查看linux系统版本命令 (转)
Copyright © 2011-2022 走看看