Advertisement
vkazar

Untitled

Nov 30th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.48 KB | None | 0 0
  1. import java.io.BufferedReader
  2. import java.io.File
  3.  
  4. // <template>
  5. class Node(var left: Node?, var right: Node?, var value: Int)
  6. // <template>
  7.  
  8. private val counter = mutableMapOf<Int, Int>()
  9.  
  10. fun main() {
  11.     val reader = File("input.txt").inputStream().bufferedReader()
  12.     val writer = File("output.txt").outputStream().bufferedWriter()
  13.     val treeReader = TreeReader<Node> { v, l, r -> Node(l, r, v) }
  14.     val root = treeReader.readTree(reader)
  15.  
  16.     val res = printRange(root, 1, 1000)
  17.  
  18.     res.sorted().forEach { writer.appendLine(it.toString()) }
  19.  
  20.     val diffs = treeReader.counter.filter { counter[it.key] != it.value }.toMap()
  21.     println(diffs)
  22. }
  23.  
  24. fun printRange(root: Node?, L: Int, R: Int):List<Int> {
  25.     if (root == null) return listOf()
  26.     val res = mutableListOf<Int>()
  27.     fun getValuesInRange(node: Node?, L: Int, R: Int) {
  28.         if (node == null) return
  29.  
  30.         if (L <= node.value) {
  31.             getValuesInRange(node.left, L, R)
  32.         }
  33.  
  34.         if (node.value in L..R) {
  35.             counter[node.value] = counter.getOrDefault(node.value, 0) + 1
  36.             res.add(node.value)
  37.         }
  38.  
  39.         if (R >= node.value) {
  40.             getValuesInRange(node.right, L, R)
  41.         }
  42.     }
  43.  
  44.     getValuesInRange(root, L, R)
  45.     return res
  46. }
  47.  
  48. class TreeReader<T>(private val nodeCreator: (Int, T?, T?) -> T?) {
  49.  
  50.     private val _counter = mutableMapOf<Int, Int>()
  51.     val counter: Map<Int, Int>
  52.         get() = _counter.toMap()
  53.  
  54.     fun readTree(reader: BufferedReader): T? {
  55.         return reader.use { buffer ->
  56.             val count = buffer.readLine().toInt()
  57.  
  58.             buildMap {
  59.                 repeat(count) {
  60.                     val line = buffer.readLine().split(" ")
  61.                     val id = line[0].toInt()
  62.                     val value = line[1].toInt()
  63.                     _counter[value] = _counter.getOrDefault(value, 0) + 1
  64.                     put(id, value to (line[2] to line[3]))
  65.                 }
  66.             }.createTree()
  67.         }
  68.     }
  69.  
  70.     private fun Map<Int, Pair<Int, Pair<String, String>>>.createTree(): T? = createNode(this.keys.min())
  71.  
  72.     private fun Map<Int, Pair<Int, Pair<String, String>>>.createNode(id: Int): T? {
  73.         val value = this[id] ?: return null
  74.         val childrenIds = value.second
  75.         return nodeCreator(
  76.             value.first,
  77.             childrenIds.first.toIntOrNull()?.let { createNode(it) },
  78.             childrenIds.second.toIntOrNull()?.let { createNode(it) })
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement