結果

問題 No.1625 三角形の質問
ユーザー yakamotoyakamoto
提出日時 2021-07-23 23:08:58
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 3,141 ms / 6,000 ms
コード長 6,409 bytes
コンパイル時間 20,952 ms
コンパイル使用メモリ 477,652 KB
実行使用メモリ 100,736 KB
最終ジャッジ日時 2024-07-18 20:51:22
合計ジャッジ時間 62,397 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 299 ms
54,824 KB
testcase_01 AC 623 ms
67,964 KB
testcase_02 AC 1,574 ms
89,456 KB
testcase_03 AC 903 ms
88,660 KB
testcase_04 AC 1,162 ms
89,048 KB
testcase_05 AC 1,667 ms
97,032 KB
testcase_06 AC 2,893 ms
100,528 KB
testcase_07 AC 2,845 ms
100,612 KB
testcase_08 AC 3,141 ms
100,588 KB
testcase_09 AC 2,845 ms
100,736 KB
testcase_10 AC 2,943 ms
100,608 KB
testcase_11 AC 2,895 ms
100,624 KB
testcase_12 AC 2,913 ms
100,464 KB
testcase_13 AC 3,004 ms
100,700 KB
testcase_14 AC 2,922 ms
100,424 KB
testcase_15 AC 2,880 ms
100,656 KB
testcase_16 AC 1,001 ms
96,512 KB
testcase_17 AC 701 ms
96,496 KB
testcase_18 AC 1,312 ms
100,160 KB
testcase_19 AC 744 ms
100,620 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Main.kt:13:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun f(x1: Long, x2: Long) = max(x1, x2)
          ^
Main.kt:61:30: warning: unnecessary non-null assertion (!!) on a non-null receiver of type Query2
  val maxL = Q.maxBy { it.l }!!.l
                             ^
Main.kt:256:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: LongArray) {
          ^
Main.kt:260:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: IntArray) {
          ^
Main.kt:264:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debug(a: BooleanArray) {
          ^
Main.kt:268:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}
          ^
Main.kt:270:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<LongArray>) {
          ^
Main.kt:277:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<IntArray>) {
          ^
Main.kt:284:11: warning: expected performance impact from inlining is insignificant. Inlining works best for functions with parameters of functional types
  private inline fun debugDim(A: Array<BooleanArray>) {
          ^
Main.kt:301:11: warning: expected performance impact fr

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.lang.AssertionError
import java.util.*
import kotlin.math.*

val MOD = 1_000_000_007

class RangeUpdateTree(n: Int) {

  private val zero = 0L
  private inline fun f(x1: Long, x2: Long) = max(x1, x2)

  private val N =
    if (Integer.highestOneBit(n) == n) n
    else Integer.highestOneBit(n) shl 1

  private val dat = LongArray(2 * N){zero}

//  private inline fun f(a: Int, b: Int) = a + b

  /**
   * [l, r)
   */
  fun add(l: Int, r: Int, a: Long) {
    var left = l + N
    var right = r - 1 + N

    while(left <= right) {
      if ((left and 1) == 1) dat[left] = f(dat[left], a)
      if ((right and 1) == 0) dat[right] = f(dat[right], a)
      left = (left + 1) shr  1 // 右の子供なら右の親に移動
      right = (right - 1) shr 1 // 左の子供なら左の親に移動
    }
  }

  fun query(i: Int): Long {
    var ix = N + i
    var res: Long = zero

    while(ix >= 1) {
      res = f(res, dat[ix])
      ix = ix shr 1
    }

    return res
  }
}



data class Tri(val x1: Int, val y1: Int, val x2: Int, val y2: Int, val x3: Int, val y3: Int) {
  fun l() = min(x1, min(x2, x3))
  fun r() = max(x1, max(x2, x3))
}
interface Query
data class Query1(val i: Int, val t: Tri) : Query
data class Query2(val i: Int, val l: Int, val r: Int) : Query
class Group(val Q: Array<Query2>) {
  val maxL = Q.maxBy { it.l }!!.l
  val R: IntArray
  init {
    Q.sortBy { it.r }
    R = Q.map { it.r }.toIntArray()
  }
  val tree = RangeUpdateTree(Q.size)

  override fun toString(): String {
    return "[${Q.joinToString(" ")}]"
  }
}
fun lb(A: IntArray, x: Int): Int {
  var l = - 1
  var h = A.size
  while(h - l > 1) {
    val m = (h + l) / 2
    if (A[m] >= x) h = m
    else l = m
  }
  return h
}
class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  private val reader = BufferedReader(InputStreamReader(stream), 32768)

  fun solve() {
    val (N, Q) = na(2)
    fun readTri() = Tri(ni(), ni(), ni(), ni(), ni(), ni())
    val T = Array(N) { readTri() }

    val queries = Array(Q) {
        if (ni() == 1)
          Query1(it, readTri())
        else
          Query2(it, ni(), ni())
    }
    debug{"${queries.joinToString(" ")}"}

    val Q2 = queries.filterIsInstance<Query2>().toTypedArray()

    if (Q2.isEmpty()) return

    Q2.sortBy { it.l }
    debug{"${Q2.joinToString(" ")}"}

    val n = sqrt(Q2.size.toDouble()).toInt() // 1グループあたりのquery数の最大
    val m = (Q2.size + n - 1)/n // group数

    debug{"n:$n m:$m"}

    val G = Array(m) {
      val q2 = Q2.slice(n*it until min(Q2.size, n*(it + 1)))
      Group(q2.toTypedArray())
    }
    debug{"${G.joinToString(" ")}"}
    
    val qid2Pos = Array(Q){ Pair(-1, -1) }
    for (g in G.indices) {
      for (i in G[g].Q.indices) {
        qid2Pos[G[g].Q[i].i] = Pair(g, i)
      }
    }

    fun calc(t: Tri): Long = run {
      abs((t.x1-t.x3).toLong()*(t.y2-t.y3) - (t.x2 - t.x3).toLong()*(t.y1-t.y3))
    }
    fun add(t: Tri) {
      val v = calc(t)
      val l = t.l()
      val r = t.r()
      debug{"l:$l r:$r"}
      for (g in G) {
        if (l >= g.maxL) {
          val j = lb(g.R, r)
          g.tree.add(j, g.Q.size, v)
        } 
        else {
          for (i in 0 until g.Q.size) {
            if (g.Q[i].l <= l && r <= g.Q[i].r) {
              g.tree.add(i, i + 1, v)
            }
          }

          break
        }
      }
    }

    for (t in T) {
      add(t)
    }

    for (qid in queries.indices) {
      val q = queries[qid]
      when(q) {
        is Query1 -> add(q.t)
        is Query2 -> {
          val (g, i) = qid2Pos[qid]
          val ans = G[g].tree.query(i)
          if (ans == 0L) out.println(-1) else out.println(ans)
        }
        else -> {}
      }
    }
  }












































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return IntArray(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  /**
   * コーナーケースでエラー出たりするので、debug(dp[1])のように添え字付きの場合はdebug{}をつかうこと
   */
  private inline fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private inline fun debug(a: BooleanArray) {
    debug { toString(a) }
  }

  private inline fun toString(a: BooleanArray) = run{a.map { if (it) 1 else 0 }.joinToString("")}

  private inline fun debugDim(A: Array<LongArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }
  private inline fun debugDim(A: Array<BooleanArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }

  private inline fun assert(b: Boolean) = run{if (!b) throw AssertionError()}
  private inline fun assert(b: Boolean, f: () -> String) = run{if (!b) throw AssertionError(f())}

  companion object {
    // TestRunnerから呼びたいので単純なmainじゃだめ
    fun main() {
      val out = java.io.PrintWriter(System.out)
      Solver(System.`in`, out).solve()
      out.flush()
    }
  }
}

/**
 * judgeから呼ばれる
 */
fun main() = Solver.main()
0