Submission #24810503


Source Code Expand

@file:Suppress("DuplicatedCode")

import java.io.IOException
import java.io.InputStream
import java.io.PrintWriter
import java.util.NoSuchElementException

fun main(args: Array<String>) {
    PrintWriter(System.out).use { out ->
        P012.solve(P012.FastScanner(), out)
        out.flush()
    }
}

private fun P012.solve(sc: P012.FastScanner, out: PrintWriter) {
    val H = sc.nextInt()
    val W = sc.nextInt()
    val Q = sc.nextInt()

    val li = BooleanArray(H * W)
    val uf = P012.UnionFind(H * W)

    repeat(Q) { _ ->
        val type = sc.nextInt()
        if (type == 1) {
            val r = sc.nextInt() - 1
            val c = sc.nextInt() - 1
            val index = r * W + c
            li[index] = true

            if (r != 0) {
                val pos = (r - 1) * W + c
                if (li[pos]) uf.unite(index, pos)
            }

            if (r != H - 1) {
                val pos = (r + 1) * W + c
                if (li[pos]) uf.unite(index, pos)
            }

            if (c != 0) {
                val pos = r * W + c - 1
                if (li[pos]) uf.unite(index, pos)
            }

            if (c != W - 1) {
                val pos = r * W + c + 1
                if (li[pos]) uf.unite(index, pos)
            }
        } else {
            val r1 = sc.nextInt() - 1
            val c1 = sc.nextInt() - 1
            val fp = r1 * W + c1
            val r2 = sc.nextInt() - 1
            val c2 = sc.nextInt() - 1
            val sp = r2 * W + c2

            val msg = if (li[fp] && li[sp] && uf.same(fp, sp)) "Yes" else "No"
            out.println(msg)
        }
    }
}

@Suppress("MemberVisibilityCanBePrivate")
private object P012 {
    private const val zeroCode = '0'.toInt()
    private const val nineCode = '9'.toInt()

    class FastScanner {
        private val `in`: InputStream = System.`in`
        private val buffer = ByteArray(2048)
        private var ptr = 0
        private var buflen = 0

        private fun hasNextByte(): Boolean {
            if (ptr < buflen) {
                return true
            } else {
                ptr = 0
                try {
                    buflen = `in`.read(buffer)
                } catch (e: IOException) {
                    e.printStackTrace()
                }
                if (buflen <= 0) {
                    return false
                }
            }
            return true
        }

        private fun readByte(): Byte = if (hasNextByte()) buffer[ptr++] else -1

        private fun readByteAsInt(): Int = if (hasNextByte()) buffer[ptr++].toInt() else -1

        operator fun hasNext(): Boolean {
            while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++
            return hasNextByte()
        }

        operator fun next(): String {
            if (!hasNext()) throw NoSuchElementException()

            return buildString {
                var b = readByteAsInt()

                while (isPrintableChar(b)) {
                    appendCodePoint(b)
                    b = readByteAsInt()
                }
            }
        }

        fun toCharIterator(): CharIterator = object : CharIterator() {
            private var nextByte: Byte = readByte()

            override fun hasNext(): Boolean = isPrintableChar(nextByte)

            override fun nextChar(): Char {
                val res = nextByte
                nextByte = readByte()
                return res.toChar()
            }

        }

        fun nextLong(): Long {
            if (!hasNext()) throw NoSuchElementException()
            var n: Long = 0
            var minus = false
            var b = readByteAsInt()

            if (b == '-'.toInt()) {
                minus = true
                b = readByteAsInt()
            }

            if (b !in zeroCode..nineCode) {
                throw NumberFormatException()
            }

            while (true) {
                if (b in zeroCode..nineCode) {
                    n *= 10
                    n += b - zeroCode.toLong()
                } else if (b == -1 || !isPrintableChar(b)) {
                    return if (minus) -n else n
                } else {
                    throw NumberFormatException()
                }

                b = readByteAsInt()
            }
        }

        fun nextInt(): Int {
            val nl = nextLong()
            if (nl < Int.MIN_VALUE || nl > Int.MAX_VALUE) throw NumberFormatException()
            return nl.toInt()
        }

        fun nextDouble(): Double {
            return next().toDouble()
        }

        fun readIntArray(n: Int): IntArray {
            val arr = IntArray(n)

            for (i in 0 until n) {
                arr[i] = nextInt()
            }

            return arr
        }

        fun readBigInt(capacity: Int): IntArray {
            if (!hasNext()) throw NoSuchElementException()

            val arr = IntArray(capacity)
            var len = 0
            arr[0] = readByteAsInt() - zeroCode
            while (arr[len] in 0..9) {
                len++
                arr[len] = readByteAsInt() - zeroCode
            }

            return arr.sliceArray(0 until len)
        }

        companion object {
            private inline fun isPrintableChar(c: Int): Boolean {
                return c in 33..126
            }

            private inline fun isPrintableChar(c: Byte): Boolean {
                return c in 33..126
            }
        }
    }

    class UnionFind(val size: Int) {
        val par = IntArray(size) { it }
        val rank = IntArray(size)

        fun root(pos: Int): Int = if (par[pos] == pos) pos else {
            par[pos] = root(par[pos])
            par[pos]
        }

        fun same(x: Int, y: Int) = root(x) == root(y)

        fun unite(x: Int, y: Int) {
            val x = root(x)
            val y = root(y)

            if (x == y) return

            if (rank[x] < rank[y]) {
                par[x] = y
            } else {
                par[y] = x
                if (rank[x] == rank[y]) rank[x]++
            }
        }
    }
}

Submission Info

Submission Time
Task 012 - Red Painting(★4)
User Bookends
Language Kotlin (1.3.71)
Score 4
Code Size 6293 Byte
Status AC
Exec Time 212 ms
Memory 72224 KiB

Compile Error

warning: ATTENTION!
This build uses unsafe internal compiler arguments:

-XXLanguage:+InlineClasses

This mode is not recommended for production use,
as no stability/compatibility guarantees are given on
compiler or generated code. Use it at your own risk!

Main.kt:191:21: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
            private inline fun isPrintableChar(c: Int): Boolean {
                    ^
Main.kt:195:21: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
            private inline fun isPrintableChar(c: Byte): Boolean {
                    ^
Main.kt:213:17: warning: name shadowed: x
            val x = root(x)
                ^
Main.kt:214:17: warning: name shadowed: y
            val y = root(y)
                ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 4 / 4
Status
AC × 3
AC × 43
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_01.txt, subtask_1_02.txt, subtask_1_03.txt, subtask_1_04.txt, subtask_1_05.txt, subtask_1_06.txt, subtask_1_07.txt, subtask_1_08.txt, subtask_1_09.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_18.txt, subtask_1_19.txt, subtask_1_20.txt, subtask_1_21.txt, subtask_1_22.txt, subtask_1_23.txt, subtask_1_24.txt, subtask_1_25.txt, subtask_1_26.txt, subtask_1_27.txt, subtask_1_28.txt, subtask_1_29.txt, subtask_1_30.txt, subtask_1_31.txt, subtask_1_32.txt, subtask_1_33.txt, subtask_1_34.txt, subtask_1_35.txt, subtask_1_36.txt, subtask_1_37.txt, subtask_1_38.txt, subtask_1_39.txt, subtask_1_40.txt
Case Name Status Exec Time Memory
sample_01.txt AC 88 ms 34176 KiB
sample_02.txt AC 77 ms 34172 KiB
sample_03.txt AC 81 ms 34016 KiB
subtask_1_01.txt AC 153 ms 35972 KiB
subtask_1_02.txt AC 130 ms 42896 KiB
subtask_1_03.txt AC 171 ms 42760 KiB
subtask_1_04.txt AC 175 ms 60260 KiB
subtask_1_05.txt AC 162 ms 39892 KiB
subtask_1_06.txt AC 134 ms 35784 KiB
subtask_1_07.txt AC 142 ms 37512 KiB
subtask_1_08.txt AC 186 ms 46528 KiB
subtask_1_09.txt AC 133 ms 39108 KiB
subtask_1_10.txt AC 151 ms 37420 KiB
subtask_1_11.txt AC 131 ms 46012 KiB
subtask_1_12.txt AC 102 ms 37272 KiB
subtask_1_13.txt AC 127 ms 39992 KiB
subtask_1_14.txt AC 132 ms 37312 KiB
subtask_1_15.txt AC 179 ms 49400 KiB
subtask_1_16.txt AC 163 ms 63192 KiB
subtask_1_17.txt AC 144 ms 48592 KiB
subtask_1_18.txt AC 108 ms 35832 KiB
subtask_1_19.txt AC 134 ms 37864 KiB
subtask_1_20.txt AC 161 ms 42212 KiB
subtask_1_21.txt AC 179 ms 64024 KiB
subtask_1_22.txt AC 133 ms 54536 KiB
subtask_1_23.txt AC 138 ms 47784 KiB
subtask_1_24.txt AC 169 ms 47604 KiB
subtask_1_25.txt AC 164 ms 41456 KiB
subtask_1_26.txt AC 108 ms 39364 KiB
subtask_1_27.txt AC 166 ms 36508 KiB
subtask_1_28.txt AC 164 ms 36824 KiB
subtask_1_29.txt AC 176 ms 36672 KiB
subtask_1_30.txt AC 167 ms 36616 KiB
subtask_1_31.txt AC 175 ms 36868 KiB
subtask_1_32.txt AC 162 ms 36600 KiB
subtask_1_33.txt AC 149 ms 36292 KiB
subtask_1_34.txt AC 156 ms 71096 KiB
subtask_1_35.txt AC 198 ms 72224 KiB
subtask_1_36.txt AC 190 ms 71348 KiB
subtask_1_37.txt AC 198 ms 72020 KiB
subtask_1_38.txt AC 206 ms 72128 KiB
subtask_1_39.txt AC 191 ms 71332 KiB
subtask_1_40.txt AC 212 ms 71624 KiB