Submission #53852008


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()
    }
}

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

fun solve() {
    val n = readInt()
    val intervals = readPiis(n)
    val p = PriorityQueue<Pair<Int, Boolean>>(compareBy({ it.first }, { it.second }))
    for ((s, t) in intervals) {
        p += s to false
        p += t to true
    }
    var ans = 0L
    var count = 0
    while (p.isNotEmpty()) {
        val (i, b) = p.poll()
//        println("$i $b")
        if (!b) {
            ans += count
            count++
        } else {
            count--
        }
    }
    println(ans)
}

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

Submission Info

Submission Time
Task D - Intersecting Intervals
User wangchaohui
Language Kotlin (Kotlin/JVM 1.8.20)
Score 400
Code Size 2654 Byte
Status AC
Exec Time 1626 ms
Memory 138272 KiB

Compile Error

Main.kt:77:14: warning: variable 'i' is never used
        val (i, b) = p.poll()
             ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 22
Set Name Test Cases
Sample 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 02_handmade_01.txt, 02_handmade_02.txt, 02_handmade_03.txt, 02_handmade_04.txt
Case Name Status Exec Time Memory
00_sample_01.txt AC 56 ms 38708 KiB
00_sample_02.txt AC 56 ms 38592 KiB
00_sample_03.txt AC 51 ms 38540 KiB
01_random_01.txt AC 1605 ms 121932 KiB
01_random_02.txt AC 941 ms 96724 KiB
01_random_03.txt AC 1613 ms 122008 KiB
01_random_04.txt AC 1068 ms 96932 KiB
01_random_05.txt AC 1613 ms 121884 KiB
01_random_06.txt AC 1626 ms 116692 KiB
01_random_07.txt AC 1555 ms 122104 KiB
01_random_08.txt AC 1065 ms 100072 KiB
01_random_09.txt AC 1606 ms 122116 KiB
01_random_10.txt AC 1564 ms 116808 KiB
01_random_11.txt AC 1612 ms 121928 KiB
01_random_12.txt AC 256 ms 56936 KiB
01_random_13.txt AC 1610 ms 123052 KiB
01_random_14.txt AC 1116 ms 99536 KiB
01_random_15.txt AC 1586 ms 122016 KiB
02_handmade_01.txt AC 783 ms 138272 KiB
02_handmade_02.txt AC 812 ms 120192 KiB
02_handmade_03.txt AC 913 ms 137052 KiB
02_handmade_04.txt AC 688 ms 112648 KiB