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 03df2e6

Browse files
1
1 parent 3568a0b commit 03df2e6

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package p34xx
2+
3+
import util.expect
4+
5+
fun main() {
6+
class Solution {
7+
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
11+
12+
val children by lazy {
13+
arrayOf(SegNode(l4, (l4 + r4) / 2), SegNode((l4 + r4) / 2 + 1, r4))
14+
}
15+
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)
23+
}
24+
}
25+
}
26+
27+
val root = SegNode(0, 15)
28+
29+
return queries.sumOf {
30+
(root.query(it[0], it[1]) + 1) / 2
31+
}
32+
}
33+
}
34+
35+
expect {
36+
Solution().minOperations(
37+
arrayOf(
38+
intArrayOf(1, 2)
39+
)
40+
)
41+
}
42+
}

0 commit comments

Comments
(0)

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