Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
CSDN 技术社区
contest_exercises_template
提交
a9e0489c
C
contest_exercises_template
项目概览
CSDN 技术社区
/
contest_exercises_template
通知
79
Star
2
Fork
18
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
4
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
contest_exercises_template
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
a9e0489c
编写于
2月 24, 2023
作者:
F
feilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
format
上级
af970f08
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
81 addition
and
81 deletion
+81
-81
exercises/huanhuilong/中等/查找点在自然区间的坐标/solution.cpp
exercises/huanhuilong/中等/查找点在自然区间的坐标/solution.cpp
+81
-81
未找到文件。
exercises/huanhuilong/中等/查找点在自然区间的坐标/solution.cpp
浏览文件 @
a9e0489c
...
...
@@ -8,113 +8,113 @@
class
Range
{
public:
Range
()
:
m_pos
(
0
),
m_length
(
0
){};
Range
(
uint64_t
pos
,
uint64_t
length
)
:
m_pos
(
pos
),
m_length
(
length
){}
Range
(
Range
const
&
rhs
){
if
(
this
==
&
rhs
)
return
;
m_pos
=
rhs
.
m_pos
;
m_length
=
rhs
.
m_length
;
}
Range
&
operator
=
(
Range
const
&
rhs
){
if
(
this
==
&
rhs
)
return
*
this
;
m_pos
=
rhs
.
m_pos
;
m_length
=
rhs
.
m_length
;
return
*
this
;
}
~
Range
(){}
bool
operator
<
(
Range
const
&
rhs
)
const
{
Range
()
:
m_pos
(
0
),
m_length
(
0
){};
Range
(
uint64_t
pos
,
uint64_t
length
)
:
m_pos
(
pos
),
m_length
(
length
){}
Range
(
Range
const
&
rhs
){
if
(
this
==
&
rhs
)
return
;
m_pos
=
rhs
.
m_pos
;
m_length
=
rhs
.
m_length
;
}
Range
&
operator
=
(
Range
const
&
rhs
){
if
(
this
==
&
rhs
)
return
*
this
;
m_pos
=
rhs
.
m_pos
;
m_length
=
rhs
.
m_length
;
return
*
this
;
}
~
Range
(){}
bool
operator
<
(
Range
const
&
rhs
)
const
{
return
m_pos
<
rhs
.
m_pos
||
(
m_pos
==
rhs
.
m_pos
&&
m_length
<
rhs
.
m_length
);
}
uint64_t
GetPos
()
const
{
uint64_t
GetPos
()
const
{
return
m_pos
;
}
uint64_t
GetLength
()
const
{
uint64_t
GetLength
()
const
{
return
m_length
;
}
uint64_t
GetRight
()
const
{
uint64_t
GetRight
()
const
{
return
m_pos
+
m_length
;
}
public:
uint64_t
m_pos
;
uint64_t
m_length
;
uint64_t
m_pos
;
uint64_t
m_length
;
};
size_t
findNatureRangeIndex
(
std
::
vector
<
Range
>&
rangeList
,
uint64_t
point
){
size_t
n
=
rangeList
.
size
();
size_t
first
=
0
;
size_t
last
=
2
*
n
+
1
;
size_t
max
=
last
;
uint64_t
testBeg
=
0
;
uint64_t
testEnd
=
0
;
size_t
idx
=
SIZE_MAX
;
while
(
first
<=
last
)
{
size_t
mid
=
(
first
+
last
)
/
2
;
// compute mid point.
size_t
n
=
rangeList
.
size
();
size_t
first
=
0
;
size_t
last
=
2
*
n
+
1
;
size_t
max
=
last
;
uint64_t
testBeg
=
0
;
uint64_t
testEnd
=
0
;
size_t
idx
=
SIZE_MAX
;
while
(
first
<=
last
)
{
size_t
mid
=
(
first
+
last
)
/
2
;
// compute mid point.
std
::
cout
<<
"xx:"
<<
mid
;
// 处理右边界
if
(
mid
>=
max
-
1
)
{
uint64_t
lastEnd
=
rangeList
[
n
-
1
].
m_pos
+
rangeList
[
n
-
1
].
m_length
;
// 处理右边界
if
(
mid
>=
max
-
1
)
{
uint64_t
lastEnd
=
rangeList
[
n
-
1
].
m_pos
+
rangeList
[
n
-
1
].
m_length
;
std
::
cout
<<
"point:"
<<
point
<<
"lastEnd:"
<<
lastEnd
<<
"max:"
<<
max
;
if
(
point
>=
lastEnd
)
{
idx
=
max
-
1
;
}
else
{
idx
=
max
-
2
;
}
break
;
}
// 处理左边界
if
(
mid
==
0
)
{
if
(
point
>=
rangeList
[
0
].
m_pos
)
{
idx
=
1
;
}
else
{
idx
=
0
;
}
break
;
}
// 处理中间
bool
even
=
mid
%
2
==
0
;
if
(
even
)
{
size_t
left
=
(
mid
-
1
)
/
2
;
size_t
right
=
left
+
1
;
testBeg
=
rangeList
[
left
].
m_pos
+
rangeList
[
left
].
m_length
;
testEnd
=
rangeList
[
right
].
m_pos
;
}
else
{
size_t
current
=
mid
/
2
;
testBeg
=
rangeList
[
current
].
m_pos
;
testEnd
=
rangeList
[
current
].
m_pos
+
rangeList
[
current
].
m_length
;
}
// 二分查找
if
(
point
>=
lastEnd
)
{
idx
=
max
-
1
;
}
else
{
idx
=
max
-
2
;
}
break
;
}
// 处理左边界
if
(
mid
==
0
)
{
if
(
point
>=
rangeList
[
0
].
m_pos
)
{
idx
=
1
;
}
else
{
idx
=
0
;
}
break
;
}
// 处理中间
bool
even
=
mid
%
2
==
0
;
if
(
even
)
{
size_t
left
=
(
mid
-
1
)
/
2
;
size_t
right
=
left
+
1
;
testBeg
=
rangeList
[
left
].
m_pos
+
rangeList
[
left
].
m_length
;
testEnd
=
rangeList
[
right
].
m_pos
;
}
else
{
size_t
current
=
mid
/
2
;
testBeg
=
rangeList
[
current
].
m_pos
;
testEnd
=
rangeList
[
current
].
m_pos
+
rangeList
[
current
].
m_length
;
}
// 二分查找
if
(
point
>=
testEnd
)
{
if
(
point
>=
testEnd
)
{
std
::
cout
<<
"->"
<<
point
<<
","
<<
testBeg
<<
","
<<
testEnd
<<
std
::
endl
;
first
=
mid
+
1
;
// repeat search in top half.
}
else
if
(
point
<
testBeg
)
{
first
=
mid
+
1
;
// repeat search in top half.
}
else
if
(
point
<
testBeg
)
{
std
::
cout
<<
"<-"
<<
point
<<
","
<<
testBeg
<<
","
<<
testEnd
<<
std
::
endl
;
last
=
mid
-
1
;
// repeat search in bottom half.
}
else
{
last
=
mid
-
1
;
// repeat search in bottom half.
}
else
{
std
::
cout
<<
point
<<
","
<<
testBeg
<<
","
<<
testEnd
<<
std
::
endl
;
idx
=
mid
;
break
;
}
}
idx
=
mid
;
break
;
}
}
std
::
cout
<<
std
::
endl
;
return
idx
;
return
idx
;
}
int
main
(
int
argc
,
char
**
argv
){
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录