Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlin.math.abs
- private data class Rectangle(val x1: Int, val y1: Int, val x2: Int, val y2: Int) {
- companion object {
- @JvmStatic
- internal inline val input
- get() = readLine()!!
- .trim()
- .split(' ')
- .map(String::toInt)
- .let { (x1, y1, x2, y2) -> Rectangle(x1, y1, x2, y2) }
- @JvmStatic
- internal fun getCommonArea(vararg rectangles: Rectangle): Long {
- for (i in 0 until rectangles.size - 1)
- for (q in i + 1 until rectangles.size)
- if (!(rectangles[i] isHaveCommonAreaWith rectangles[q]))
- return 0
- val xs = rectangles.flatMap { (x1, _, x2) -> listOf(x1, x2) }.sorted()
- val ys = rectangles.flatMap { (_, y1, _, y2) -> listOf(y1, y2) }.sorted()
- return (xs[rectangles.size] - xs[rectangles.size - 1]).toLong() *
- (ys[rectangles.size] - ys[rectangles.size - 1]).toLong()
- }
- }
- inline val area
- get() = abs(x1 - x2 + 0L) * abs(y1 - y2 + 0L)
- infix fun isHaveCommonAreaWith(other: Rectangle): Boolean {
- val (x1F, x2F) = listOf(x1, x2).sorted()
- val (x1S, x2S) = listOf(other.x1, other.x2).sorted()
- if (x2F < x1S || x2S < x1F) return false
- val (y1F, y2F) = listOf(y1, y2).sorted()
- val (y1S, y2S) = listOf(other.y1, other.y2).sorted()
- if (y2F < y1S || y2S < y1F) return false
- return true
- }
- infix fun `&`(other: Rectangle): Long = getCommonArea(this, other)
- }
- fun main() {
- val first = Rectangle.input
- val second = Rectangle.input
- val third = Rectangle.input
- val fs = first `&` second
- val ft = first `&` third
- val st = second `&` third
- print(first.area + second.area + third.area - fs - ft - st + Rectangle.getCommonArea(first, second, third))
- }
Add Comment
Please, Sign In to add comment