@@ -28,7 +28,8 @@ LC70 爬楼梯 https://leetcode.cn/problems/climbing-stairs/
28
28
- 有障碍物 https://atcoder.jp/contests/abc129/tasks/abc129_c
29
29
LC198 打家劫舍 https://leetcode.cn/problems/house-robber/
30
30
- 值域打家劫舍 LC740 https://leetcode.cn/problems/delete-and-earn/
31
- - 分组+值域打家劫舍 https://atcoder.jp/contests/abc403/tasks/abc403_d
31
+ - 分组+值域打家劫舍 https://atcoder.jp/contests/abc403/tasks/abc403_d
32
+ - 类似思路的题目 https://leetcode.cn/problems/the-number-of-beautiful-subsets/
32
33
- 恰好选 floor(n/2) 个 https://atcoder.jp/contests/abc162/tasks/abc162_f
33
34
- 矩阵打家劫舍 https://codeforces.com/problemset/problem/1195/C
34
35
- 环形 LC213 https://leetcode.cn/problems/house-robber-ii/
@@ -3244,13 +3245,13 @@ func _(abs func(int) int) {
3244
3245
https://www.luogu.com.cn/problem/P1613
3245
3246
https://www.acwing.com/problem/content/296/ 计算重复
3246
3247
*/
3247
- binaryLifting := func (left []int , qs [] struct { l , r int }) [] int {
3248
+ binaryLifting := func (left []int ) {
3248
3249
// 以 https://atcoder.jp/contests/arc060/tasks/arc060_c + https://leetcode.cn/problems/path-existence-queries-in-a-graph-ii/ 为例
3249
3250
n := len (left )
3250
3251
const mx = 17 // bits.Len(uint(n))
3251
3252
pa := make ([][mx ]int , n )
3252
3253
for i , l := range left {
3253
- pa [i ][0 ] = l
3254
+ pa [i ][0 ] = l // 从 i 往左,最远可以跳到 l
3254
3255
}
3255
3256
for i := range mx - 1 {
3256
3257
for x := range pa {
@@ -3259,33 +3260,38 @@ func _(abs func(int) int) {
3259
3260
}
3260
3261
}
3261
3262
3262
- ans := make ([]int , len (qs ))
3263
- for qi , q := range qs {
3264
- l , r := q .l , q .r
3263
+ // 从 r 跳到 <= l 的位置,最少要跳多少步
3264
+ minJumps := func (l , r int ) (res int ) {
3265
3265
if l == r {
3266
- // ans[qi] = 0
3267
- continue
3266
+ return
3268
3267
}
3269
3268
if l > r {
3270
3269
l , r = r , l
3271
3270
}
3272
- res := 0
3273
3271
for k := mx - 1 ; k >= 0 ; k -- {
3274
3272
p := pa [r ][k ]
3275
3273
if p > l {
3276
- res |= 1 << k
3277
3274
r = p
3275
+ res |= 1 << k
3278
3276
}
3279
3277
}
3280
- res ++
3281
3278
r = pa [r ][0 ]
3282
- if r <= l {
3283
- ans [qi ] = res
3284
- } else {
3285
- ans [qi ] = - 1
3279
+ res ++
3280
+ if r > l {
3281
+ return - 1
3286
3282
}
3283
+ return
3287
3284
}
3288
- return ans
3285
+
3286
+ // 从 i 开始,往左跳 k 步的位置
3287
+ jumpK := func (i , k int ) int {
3288
+ for ; k > 0 ; k &= k - 1 {
3289
+ i = pa [i ][bits .TrailingZeros (uint (k ))]
3290
+ }
3291
+ return i
3292
+ }
3293
+
3294
+ _ = []any {minJumps , jumpK }
3289
3295
}
3290
3296
3291
3297
/* 数据结构优化 DP
0 commit comments