Submission #53621281


Source Code Expand

import java.io.*
import java.util.*
import kotlin.collections.*

val INPUT: InputStream = System.`in`
val OUTPUT: PrintStream = System.out

val _reader = INPUT.bufferedReader()

var _tokenizer: StringTokenizer = StringTokenizer("")
fun read(): String {
    while (!_tokenizer.hasMoreTokens()) {
        _tokenizer = StringTokenizer(_reader.readLine() ?: return "", " ")
    }
    return _tokenizer.nextToken()
}

fun readInt() = read().toInt()
fun readDouble() = read().toDouble()
fun readLong() = read().toLong()
fun readStrings(n: Int) = List(n) { read() }
fun readInts(n: Int) = List(n) { readInt() }
fun readIntArray(n: Int) = IntArray(n) { readInt() }
fun readDoubles(n: Int) = List(n) { readDouble() }
fun readDoubleArray(n: Int) = DoubleArray(n) { readDouble() }
fun readLongs(n: Int) = List(n) { readLong() }
fun readLongArray(n: Int) = LongArray(n) { readLong() }

val _writer = PrintWriter(OUTPUT, false)
inline fun output(block: PrintWriter.() -> Unit) {
    _writer.apply(block).flush()
}

class MutableIntMap<K> : LinkedHashMap<K, Int>() {
    override operator fun get(key: K): Int = super.get(key) ?: 0

    fun increment(key: K, value: Int = 1) {
        this[key] = this[key] + value
    }

    companion object {
        fun <K> mutableIntMapOf(vararg pairs: Pair<K, Int>) =
            MutableIntMap<K>().apply { putAll(pairs) }
    }
}

typealias Pii = Pair<Int, Int>

fun readPii() = readInt() to readInt()
fun readPiis(n: Int) = List(n) { readPii() }

infix fun Int.hasBit(i: Int): Boolean = this and (1 shl i) > 0
infix fun Int.xorBit(i: Int): Int = this xor (1 shl i)
infix fun Long.hasBit(i: Int): Boolean = this and (1L shl i) > 0
infix fun Long.xorBit(i: Int): Long = this xor (1L shl i)

fun largerStackSize(stackSizeMegaBytes: Int = 100, action: () -> Unit) {
    Thread(null, action, "", 1024L * 1024 * stackSizeMegaBytes).apply {
        start()
        join()
    }
}

// #################################################################################################

val M = listOf(
    listOf(2, 1, 2, 1),
    listOf(1, 2, 1, 2),
    listOf(0, 1, 0, 1),
    listOf(1, 0, 1, 0),
    listOf(2, 1, 2, 1),
    listOf(1, 2, 1, 2),
    listOf(0, 1, 0, 1),
    listOf(1, 0, 1, 0),
)

class RangeSum2D(values: List<List<Int>>) {
    private val n = values.size
    private val m = values[0].size
    private val sum = Array(n) { LongArray(m) }
    private fun sum(i: Int, j: Int) = sum.getOrNull(i)?.getOrNull(j) ?: 0

    fun rangeSum(bottom: Int, right: Int): Long {
        check(bottom in 0..n && right in 0..m)
        return sum(bottom - 1, right - 1)
    }

    /** Queries the sum of the range. */
    fun rangeSum(top: Int, left: Int, bottom: Int, right: Int): Long {
        check(top in 0..bottom && bottom <= n)
        check(left in 0..right && right <= m)
        return rangeSum(bottom, right) - rangeSum(top, right) - rangeSum(bottom, left) + rangeSum(top, left)
    }

    fun rangeSumTile(bottom: Int, right: Int): Long {
        check(bottom >= 0 && right >= 0)
        if (bottom <= n && right <= m) return rangeSum(bottom, right)
        val a = bottom / n
        val i = bottom % n
        val b = right / m
        val j = right % m
        return rangeSum(n, m) * a * b + rangeSum(i, m) * b + rangeSum(n, j) * a + rangeSum(i, j)
    }

    fun rangeSumTile(top: Int, left: Int, bottom: Int, right: Int): Long {
        check(top in 0..bottom && left in 0..right)
        return rangeSumTile(bottom, right) -
                rangeSumTile(top, right) -
                rangeSumTile(bottom, left) +
                rangeSumTile(top, left)
    }

    init {
        for (i in 0 until n) for (j in 0 until m) {
            sum[i][j] = sum(i - 1, j) + sum(i, j - 1) - sum(i - 1, j - 1) + values[i][j]
        }
    }
}

val Sum = RangeSum2D(M)

fun dfs(a: Long, b: Long): Long {
    if (a <= 4 && b <= 2) return Sum.rangeSum(a.toInt(), b.toInt())
    val a0 = a / 4
    val b0 = b / 2
    if (a0 > 0) {
        return if (b0 > 0) {
            dfs(a % 4, b) + (8 * b0 + dfs(4, b % 2)) * a0
        } else {
            dfs(a % 4, b) + Sum.rangeSum(4, b.toInt()) * a0
        }
    }
    return (dfs(a, b % 2) + Sum.rangeSum(a.toInt(), 2) * b0)
}

fun dfs(a: Long, b: Long, c: Long, d: Long): Long {
    return dfs(c, d) - dfs(c, b) - dfs(a, d) + dfs(a, b)
}

fun solve() {
    val a = readLong() + 1000000000
    val b = readLong() + 1000000000
    val c = readLong() + 1000000000
    val d = readLong() + 1000000000
    println(dfs(a, b, c, d))
}

fun main() {
//    repeat(readInt()) { solve() }
    solve()
}

Submission Info

Submission Time
Task D - AtCoder Wallpaper
User wangchaohui
Language Kotlin (Kotlin/JVM 1.8.20)
Score 450
Code Size 4711 Byte
Status AC
Exec Time 70 ms
Memory 42120 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 3
AC × 68
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_hand_00.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt, 02_random_25.txt, 02_random_26.txt, 02_random_27.txt, 02_random_28.txt, 02_random_29.txt, 02_random_30.txt, 02_random_31.txt, 02_random_32.txt, 02_random_33.txt, 02_random_34.txt, 02_random_35.txt, 02_random_36.txt, 02_random_37.txt, 02_random_38.txt, 02_random_39.txt, 02_random_40.txt, 02_random_41.txt, 02_random_42.txt, 02_random_43.txt, 02_random_44.txt, 02_random_45.txt, 02_random_46.txt, 02_random_47.txt, 02_random_48.txt, 02_random_49.txt, 02_random_50.txt, 02_random_51.txt, 02_random_52.txt, 02_random_53.txt, 02_random_54.txt, 02_random_55.txt, 02_random_56.txt, 02_random_57.txt, 02_random_58.txt, 02_random_59.txt, 02_random_60.txt, 02_random_61.txt, 02_random_62.txt, 02_random_63.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 67 ms 42076 KiB
00_sample_01.txt AC 63 ms 41900 KiB
00_sample_02.txt AC 65 ms 41892 KiB
01_hand_00.txt AC 68 ms 41904 KiB
02_random_00.txt AC 65 ms 42088 KiB
02_random_01.txt AC 66 ms 41896 KiB
02_random_02.txt AC 65 ms 41804 KiB
02_random_03.txt AC 62 ms 41908 KiB
02_random_04.txt AC 63 ms 41836 KiB
02_random_05.txt AC 65 ms 42036 KiB
02_random_06.txt AC 70 ms 41776 KiB
02_random_07.txt AC 65 ms 41944 KiB
02_random_08.txt AC 63 ms 42100 KiB
02_random_09.txt AC 64 ms 42028 KiB
02_random_10.txt AC 67 ms 41888 KiB
02_random_11.txt AC 64 ms 41788 KiB
02_random_12.txt AC 65 ms 42032 KiB
02_random_13.txt AC 64 ms 41848 KiB
02_random_14.txt AC 64 ms 41824 KiB
02_random_15.txt AC 62 ms 41820 KiB
02_random_16.txt AC 65 ms 42064 KiB
02_random_17.txt AC 64 ms 41988 KiB
02_random_18.txt AC 64 ms 42028 KiB
02_random_19.txt AC 63 ms 41880 KiB
02_random_20.txt AC 62 ms 41836 KiB
02_random_21.txt AC 63 ms 41908 KiB
02_random_22.txt AC 65 ms 41932 KiB
02_random_23.txt AC 66 ms 41788 KiB
02_random_24.txt AC 62 ms 42008 KiB
02_random_25.txt AC 64 ms 41832 KiB
02_random_26.txt AC 62 ms 41856 KiB
02_random_27.txt AC 62 ms 41912 KiB
02_random_28.txt AC 63 ms 42088 KiB
02_random_29.txt AC 64 ms 42032 KiB
02_random_30.txt AC 65 ms 41888 KiB
02_random_31.txt AC 62 ms 42024 KiB
02_random_32.txt AC 62 ms 41824 KiB
02_random_33.txt AC 67 ms 41784 KiB
02_random_34.txt AC 65 ms 42088 KiB
02_random_35.txt AC 62 ms 41980 KiB
02_random_36.txt AC 64 ms 42088 KiB
02_random_37.txt AC 62 ms 41908 KiB
02_random_38.txt AC 62 ms 41872 KiB
02_random_39.txt AC 63 ms 41848 KiB
02_random_40.txt AC 65 ms 41972 KiB
02_random_41.txt AC 66 ms 41872 KiB
02_random_42.txt AC 62 ms 41808 KiB
02_random_43.txt AC 62 ms 41920 KiB
02_random_44.txt AC 63 ms 41796 KiB
02_random_45.txt AC 63 ms 41756 KiB
02_random_46.txt AC 63 ms 41720 KiB
02_random_47.txt AC 64 ms 42080 KiB
02_random_48.txt AC 63 ms 41700 KiB
02_random_49.txt AC 63 ms 41948 KiB
02_random_50.txt AC 62 ms 41956 KiB
02_random_51.txt AC 63 ms 41924 KiB
02_random_52.txt AC 64 ms 42032 KiB
02_random_53.txt AC 63 ms 42108 KiB
02_random_54.txt AC 65 ms 42120 KiB
02_random_55.txt AC 63 ms 41876 KiB
02_random_56.txt AC 66 ms 41908 KiB
02_random_57.txt AC 64 ms 41824 KiB
02_random_58.txt AC 63 ms 41780 KiB
02_random_59.txt AC 64 ms 41944 KiB
02_random_60.txt AC 63 ms 41980 KiB
02_random_61.txt AC 64 ms 41936 KiB
02_random_62.txt AC 63 ms 41956 KiB
02_random_63.txt AC 67 ms 41944 KiB