Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ebf8119

Browse files
1
1 parent 03df2e6 commit ebf8119

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

‎src/main/kotlin/p34xx/Problem3495.kt

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,28 @@ import util.expect
55
fun main() {
66
class Solution {
77
fun minOperations(queries: Array<IntArray>): Long {
8-
class SegNode(val l4: Int, val r4: Int) {
9-
private val l = 1L shl (l4 * 2)
10-
private val r = (1L shl ((r4 + 1) * 2)) - 1
8+
return queries.sumOf {
9+
var result = 0L
10+
var left = it[0]
11+
val right = it[1] + 1
1112

12-
val children by lazy {
13-
arrayOf(SegNode(l4, (l4 + r4) / 2), SegNode((l4 + r4) / 2 + 1, r4))
14-
}
13+
for (op in 1..15) {
14+
val l = 1 shl ((op - 1) * 2)
15+
val r = 1 shl (op * 2)
16+
17+
when {
18+
l > right -> {
19+
break
20+
}
1521

16-
fun query(num1: Int, num2: Int): Long {
17-
return if (num1 > r || num2 < l) {
18-
0L
19-
} else if (l4 < r4) {
20-
children.sumOf { it.query(num1, num2) }
21-
} else {
22-
(minOf(num2.toLong(), r) - maxOf(num1.toLong(), l) + 1L) * (l4 + 1)
22+
left < r -> {
23+
result += (minOf(r, right) - left) * op.toLong()
24+
left = r
25+
}
2326
}
2427
}
25-
}
26-
27-
val root = SegNode(0, 15)
2828

29-
return queries.sumOf {
30-
(root.query(it[0], it[1]) + 1) / 2
29+
(result + 1) / 2
3130
}
3231
}
3332
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /