Submission #53362331


Source Code Expand

import java.io.*
import java.util.*
import java.util.function.IntPredicate
import kotlin.collections.*
import kotlin.math.max

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

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

class SimpleSegmentTree<T>(
    private val n: Int,
    private val identity: T,
    private val combine: (T, T) -> T,
) {
    private val tree = MutableList(2 * n) { identity }

    constructor(values: List<T>, identity: T, combine: (T, T) -> T) :
            this(values.size, identity, combine) {
        values.forEachIndexed { i, v -> tree[i + n] = v }
        for (i in n - 1 downTo 1) tree[i] = combine(tree[i * 2], tree[i * 2 + 1])
    }

    /** Queries for the range `[l, r)`. */
    fun query(l: Int, r: Int): T {
        var resL = identity
        var resR = identity
        var i = l + n
        var j = r + n
        while (i < j) {
            if (i and 1 > 0) resL = combine(resL, tree[i++])
            if (j and 1 > 0) resR = combine(tree[--j], resR)
            i /= 2
            j /= 2
        }
        return combine(resL, resR)
    }

    fun update(p: Int, transform: (T) -> T) {
        var i = p + n
        tree[i] = transform(tree[i])
        while (i > 1) {
            i /= 2
            tree[i] = combine(tree[i * 2], tree[i * 2 + 1])
        }
    }
}

fun solve() {
    val n = readInt()
    val c = readLong()
    val maxL = SimpleSegmentTree(n, identity = Long.MIN_VALUE / 2, combine = ::max)
    val maxR = SimpleSegmentTree(n, identity = Long.MIN_VALUE / 2, combine = ::max)
    maxL.update(0) { 0 }
    maxR.update(0) { 0 }

    repeat(readInt()) {
        val t = readInt() - 1
        val p = readLong()
        val m = p + max(maxL.query(0, t + 1) - t * c, maxR.query(t, n) + t * c)
//        println("! $t $m")
        if (m + t * c > maxL.query(t, t + 1)) {
            maxL.update(t) { m + t * c }
        }
        if (m - t * c > maxR.query(t, t + 1)) {
            maxR.update(t) { m - t * c }
        }
    }
    val ans = (0 until n).maxOf { maxL.query(it, it + 1) - it * c }
    println(ans)
}

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

Submission Info

Submission Time
Task G - Merchant Takahashi
User wangchaohui
Language Kotlin (Kotlin/JVM 1.8.20)
Score 550
Code Size 4109 Byte
Status AC
Exec Time 911 ms
Memory 101516 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 4
AC × 69
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_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, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt, 01_random_49.txt, 01_random_50.txt, 01_random_51.txt, 01_random_52.txt, 01_random_53.txt, 01_random_54.txt, 01_random_55.txt, 01_random_56.txt, 01_random_57.txt, 01_random_58.txt, 01_random_59.txt, 01_random_60.txt, 01_random_61.txt, 01_random_62.txt, 01_random_63.txt, 01_random_64.txt, 01_random_65.txt, 01_random_66.txt, 01_random_67.txt, 01_random_68.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 62 ms 38576 KiB
00_sample_01.txt AC 62 ms 38644 KiB
00_sample_02.txt AC 63 ms 38588 KiB
00_sample_03.txt AC 59 ms 38636 KiB
01_random_04.txt AC 800 ms 97836 KiB
01_random_05.txt AC 764 ms 100864 KiB
01_random_06.txt AC 891 ms 100876 KiB
01_random_07.txt AC 831 ms 100884 KiB
01_random_08.txt AC 894 ms 101036 KiB
01_random_09.txt AC 822 ms 101008 KiB
01_random_10.txt AC 815 ms 97500 KiB
01_random_11.txt AC 799 ms 100736 KiB
01_random_12.txt AC 856 ms 101140 KiB
01_random_13.txt AC 863 ms 101096 KiB
01_random_14.txt AC 851 ms 101036 KiB
01_random_15.txt AC 883 ms 101256 KiB
01_random_16.txt AC 841 ms 97604 KiB
01_random_17.txt AC 794 ms 100692 KiB
01_random_18.txt AC 872 ms 100864 KiB
01_random_19.txt AC 884 ms 100976 KiB
01_random_20.txt AC 911 ms 101204 KiB
01_random_21.txt AC 893 ms 101080 KiB
01_random_22.txt AC 772 ms 97844 KiB
01_random_23.txt AC 779 ms 100852 KiB
01_random_24.txt AC 846 ms 101132 KiB
01_random_25.txt AC 849 ms 101020 KiB
01_random_26.txt AC 869 ms 101100 KiB
01_random_27.txt AC 860 ms 100988 KiB
01_random_28.txt AC 796 ms 97644 KiB
01_random_29.txt AC 795 ms 100736 KiB
01_random_30.txt AC 838 ms 100992 KiB
01_random_31.txt AC 845 ms 101052 KiB
01_random_32.txt AC 864 ms 101068 KiB
01_random_33.txt AC 839 ms 101160 KiB
01_random_34.txt AC 799 ms 97420 KiB
01_random_35.txt AC 846 ms 100804 KiB
01_random_36.txt AC 806 ms 100448 KiB
01_random_37.txt AC 856 ms 101048 KiB
01_random_38.txt AC 854 ms 100940 KiB
01_random_39.txt AC 888 ms 101176 KiB
01_random_40.txt AC 796 ms 97472 KiB
01_random_41.txt AC 780 ms 100896 KiB
01_random_42.txt AC 821 ms 101516 KiB
01_random_43.txt AC 863 ms 101140 KiB
01_random_44.txt AC 861 ms 101008 KiB
01_random_45.txt AC 823 ms 100780 KiB
01_random_46.txt AC 807 ms 97284 KiB
01_random_47.txt AC 772 ms 100836 KiB
01_random_48.txt AC 791 ms 101320 KiB
01_random_49.txt AC 855 ms 101048 KiB
01_random_50.txt AC 873 ms 101056 KiB
01_random_51.txt AC 839 ms 101104 KiB
01_random_52.txt AC 288 ms 66200 KiB
01_random_53.txt AC 573 ms 81344 KiB
01_random_54.txt AC 741 ms 89792 KiB
01_random_55.txt AC 226 ms 60748 KiB
01_random_56.txt AC 493 ms 77552 KiB
01_random_57.txt AC 280 ms 66044 KiB
01_random_58.txt AC 429 ms 74568 KiB
01_random_59.txt AC 498 ms 77112 KiB
01_random_60.txt AC 839 ms 99404 KiB
01_random_61.txt AC 425 ms 64500 KiB
01_random_62.txt AC 361 ms 61140 KiB
01_random_63.txt AC 388 ms 64300 KiB
01_random_64.txt AC 772 ms 93928 KiB
01_random_65.txt AC 257 ms 66460 KiB
01_random_66.txt AC 439 ms 64148 KiB
01_random_67.txt AC 304 ms 59400 KiB
01_random_68.txt AC 305 ms 59680 KiB