paranid5

D

Sep 19th, 2021 (edited)
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.91 KB | None | 0 0
  1. import kotlin.math.abs
  2.  
  3. private data class Rectangle(val x1: Int, val y1: Int, val x2: Int, val y2: Int) {
  4.     companion object {
  5.         @JvmStatic
  6.         internal inline val input
  7.             get() = readLine()!!
  8.                 .trim()
  9.                 .split(' ')
  10.                 .map(String::toInt)
  11.                 .let { (x1, y1, x2, y2) -> Rectangle(x1, y1, x2, y2) }
  12.  
  13.         @JvmStatic
  14.         internal fun getCommonArea(vararg rectangles: Rectangle): Long {
  15.             for (i in 0 until rectangles.size - 1)
  16.                 for (q in i + 1 until rectangles.size)
  17.                     if (!(rectangles[i] isHaveCommonAreaWith rectangles[q]))
  18.                         return 0
  19.  
  20.             val xs = rectangles.flatMap { (x1, _, x2) -> listOf(x1, x2) }.sorted()
  21.             val ys = rectangles.flatMap { (_, y1, _, y2) -> listOf(y1, y2) }.sorted()
  22.             return (xs[rectangles.size] - xs[rectangles.size - 1]).toLong() *
  23.                     (ys[rectangles.size] - ys[rectangles.size - 1]).toLong()
  24.         }
  25.     }
  26.  
  27.     inline val area
  28.         get() = abs(x1 - x2 + 0L) * abs(y1 - y2 + 0L)
  29.  
  30.     infix fun isHaveCommonAreaWith(other: Rectangle): Boolean {
  31.         val (x1F, x2F) = listOf(x1, x2).sorted()
  32.         val (x1S, x2S) = listOf(other.x1, other.x2).sorted()
  33.         if (x2F < x1S || x2S < x1F) return false
  34.  
  35.         val (y1F, y2F) = listOf(y1, y2).sorted()
  36.         val (y1S, y2S) = listOf(other.y1, other.y2).sorted()
  37.         if (y2F < y1S || y2S < y1F) return false
  38.  
  39.         return true
  40.     }
  41.  
  42.     infix fun `&`(other: Rectangle): Long = getCommonArea(this, other)
  43. }
  44.  
  45. fun main() {
  46.     val first = Rectangle.input
  47.     val second = Rectangle.input
  48.     val third = Rectangle.input
  49.  
  50.     val fs = first `&` second
  51.     val ft = first `&` third
  52.     val st = second `&` third
  53.  
  54.     print(first.area + second.area + third.area - fs - ft - st + Rectangle.getCommonArea(first, second, third))
  55. }
Add Comment
Please, Sign In to add comment