- @file:Suppress("NOTHING_TO_INLINE", "EXPERIMENTAL_FEATURE_WARNING", "OVERRIDE_BY_INLINE")
- @file:OptIn(ExperimentalStdlibApi::class)
- import java.io.PrintWriter
- import java.util.TreeMap
- import kotlin.math.*
- import kotlin.random.Random
- import kotlin.collections.sort as _sort
- import kotlin.collections.sortDescending as _sortDescending
- import kotlin.io.println as iprintln
- /** @author Spheniscine */
- fun main() { _writer.solve(); _writer.flush() }
- fun PrintWriter.solve() {
- // val startTime = System.nanoTime()
- val numCases = 1//readInt()
- case@ for(case in 1..numCases) {
- // print("Case #$case: ")
- object {
- val n = readInt()
- val M = TreeMap<Int, Int>()
- var lenB = 0
- fun add(l: Int, r: Int) {
- M[r] = l
- lenB += r - l
- }
- fun rem(l: Int, r: Int) {
- M.remove(r)
- lenB -= r - l
- }
- init {
- repeat(n) {
- val b = readChar() == 'B'
- var l = readInt()
- var r = l + readInt()
- if(b) {
- while(true) {
- val (ri: Int, li: Int) = M.ceilingEntry(l) ?: break
- if(li > r) break
- if(li < l) l = li
- if(ri > r) r = ri
- rem(li, ri)
- }
- add(l, r)
- } else {
- while(true) {
- val (ri: Int, li: Int) = M.higherEntry(l) ?: break
- if(li >= r) break
- rem(li, ri)
- if(li < l) add(li, l)
- if(ri > r) add(r, ri)
- }
- }
- println("${M.size} $lenB")
- }
- }
- }
- }
- // iprintln("Time: ${(System.nanoTime() - startTime) / 1000000} ms")
- }
- /** IO */
- //const val PATH = "src/main/resources/"
- //@JvmField val INPUT = File(PATH + "input.txt").inputStream()
- //@JvmField val OUTPUT = File(PATH + "output.txt").outputStream()
- @JvmField val INPUT = System.`in`
- @JvmField val OUTPUT = System.out
- const val _BUFFER_SIZE = 1 shl 16
- @JvmField val _buffer = ByteArray(_BUFFER_SIZE)
- @JvmField var _bufferPt = 0
- @JvmField var _bytesRead = 0
- tailrec fun readChar(): Char {
- if(_bufferPt == _bytesRead) {
- _bufferPt = 0
- _bytesRead = INPUT.read(_buffer, 0, _BUFFER_SIZE)
- }
- return if(_bytesRead < 0) Char.MIN_VALUE
- else {
- val c = _buffer[_bufferPt++].toChar()
- if (c == '\r') readChar()
- else c
- }
- }
- fun readLine(): String? {
- var c = readChar()
- return if(c == Char.MIN_VALUE) null
- else buildString {
- while(c != '\n' && c != Char.MIN_VALUE) {
- append(c)
- c = readChar()
- }
- }
- }
- fun readLn() = readLine()!!
- fun read() = buildString {
- var c = readChar()
- while(c <= ' ') {
- if(c == Char.MIN_VALUE) return@buildString
- c = readChar()
- }
- do {
- append(c)
- c = readChar()
- } while(c > ' ')
- }
- fun readInt() = read().toInt()
- fun readDouble() = read().toDouble()
- fun readLong() = read().toLong()
- fun readStrings(n: Int) = List(n) { read() }
- fun readLines(n: Int) = List(n) { readLn() }
- fun readInts(n: Int) = List(n) { read().toInt() }
- fun readIntArray(n: Int) = IntArray(n) { read().toInt() }
- fun readDoubles(n: Int) = List(n) { read().toDouble() }
- fun readDoubleArray(n: Int) = DoubleArray(n) { read().toDouble() }
- fun readLongs(n: Int) = List(n) { read().toLong() }
- fun readLongArray(n: Int) = LongArray(n) { read().toLong() }
- @JvmField val _writer = PrintWriter(OUTPUT, false)
- /** sort overrides to avoid quicksort attacks */
- @JvmField var _random: Random? = null
- val random get() = _random ?: Random(0x594E215C123 * System.nanoTime()).also { _random = it }
- fun IntArray.sort() { shuffle(random); _sort() }
- fun IntArray.sortDescending() { shuffle(random); _sortDescending() }
- fun LongArray.sort() { shuffle(random); _sort() }
- fun LongArray.sortDescending() { shuffle(random); _sortDescending() }
- fun DoubleArray.sort() { shuffle(random); _sort() }
- fun DoubleArray.sortDescending() { shuffle(random); _sortDescending() }
- inline fun CharArray.sort() { _sort() }
- inline fun CharArray.sortDescending() { _sortDescending() }
- inline fun <T : Comparable<T>> Array<out T>.sort() = _sort()
- inline fun <T : Comparable<T>> Array<out T>.sortDescending() = _sortDescending()
- inline fun <T : Comparable<T>> MutableList<out T>.sort() = _sort()
- inline fun <T : Comparable<T>> MutableList<out T>.sortDescending() = _sortDescending()
- // import preserving junk function
- @Suppress("NonAsciiCharacters") fun 雪花飄飄北風嘯嘯天地一片蒼茫() { iprintln(max(1, 2)) }