-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.kt
39 lines (33 loc) · 1.11 KB
/
solution.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class ShiftArray(val size: Int) {
private var shift = 0
private val real = IntArray(size)
fun evaluateIndex(index: Int): Int = (shift + index) % size
fun shiftLeft(v: Int) {
shift = (shift + size - v) % size
}
fun shiftRight(v: Int) {
shift = (shift + v) % size
}
operator fun get(index: Int): Int = real[evaluateIndex(index)]
operator fun set(index: Int, value: Int) {
real[evaluateIndex(index)] = value
}
fun toIntArray(): IntArray =
(real.drop(evaluateIndex(0)) + real.take(evaluateIndex(0))).toIntArray()
}
fun main(args: Array<out String>) {
val (n, q) = readLine()!!.split(" ").map { it.toInt() }
val arr = ShiftArray(n)
readLine()!!.split(" ").asSequence().map { it.toInt() }.withIndex().forEach { (i, v) ->
arr[i] = v
}
repeat(q) {
val query = readLine()!!.split(" ").map { it.toInt() }
when (query[0]) {
1 -> arr[query[1] - 1] += query[2]
2 -> arr.shiftLeft(query[1])
3 -> arr.shiftRight(query[1])
}
}
arr.toIntArray().joinToString(" ").let(::println)
}