結果

問題 No.1790 Subtree Deletion
ユーザー yudedakoyudedako
提出日時 2022-01-31 18:06:03
言語 Scala(Beta)
(3.4.0)
結果
WA  
実行時間 -
コード長 2,745 bytes
コンパイル時間 11,312 ms
コンパイル使用メモリ 261,632 KB
実行使用メモリ 122,144 KB
最終ジャッジ日時 2023-09-02 02:22:00
合計ジャッジ時間 42,588 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 911 ms
63,116 KB
testcase_01 AC 919 ms
62,792 KB
testcase_02 AC 919 ms
62,688 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 1,211 ms
66,104 KB
testcase_09 AC 2,173 ms
121,596 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*

sealed trait State
case class In(value: Int) extends State
case class Out(value: Int) extends State

class XorBit(val size: Int, initial: Array[Long]):
  private val array = initial.clone()
  for i <- 1 until size do
    val j = i + (~i & (i + 1))
    if j < size then
      array(j) ^= array(i)
  def get(position: Int): Long =
    var result = 0L
    var pos = position
    while pos >= 0 do
      result ^= array(pos)
      pos -= ~pos & (pos + 1)
    result
  def add(position: Int, value: Long) =
    var pos = position
    while pos < size do
      array(pos) ^= value
      pos += ~pos & (pos + 1)
class SumBit(val size: Int):
  private val array = Array.fill(size){0}
  def get(position: Int): Int =
    var pos = position
    var result = 0
    while pos >= 0 do
      result += array(pos)
      pos -= ~pos & (pos + 1)
    result
  def add(position: Int, value: Int) =
    var pos = position
    while pos < size do
      array(pos) += value
      pos += ~pos & (pos + 1)

@main def main =
  val n = readLine().toInt
  val edges = Array.fill(n - 1){
    val Array(l, r, a) = readLine().split(' ')
    (l.toInt - 1, r.toInt - 1, a.toLong)
  }
  val q = readLine().toInt
  val queries = Array.fill(q){
    val Array(t, x) = readLine().split(' ').map(_.toInt)
    (t, x - 1)
  }
  val graph = Array.fill(n){ArrayBuffer[(Int, Long)]()}
  for (l, r, a) <- edges do
    graph(l).addOne(r -> a)
    graph(r).addOne(l -> a)
  val parentEdge = Array.fill(n){-1L}.tap(_(0) = 0)
  val stack = mutable.Stack[State](In(0))
  val inTime = Array.fill(n){0}
  val outTime = Array.fill(n){0}
  var time = 0
  while stack.nonEmpty do
    stack.pop match
      case In(current) =>
        inTime(current) = time
        stack.push(Out(current))
        time += 1
        for (to, a) <- graph(current) do
          if parentEdge(to) == -1L then
            parentEdge(to) = a
            stack.push(In(to))
      case Out(current) =>
        outTime(current) = time - 1
  val eulerTour = Array.fill(n){0L}
  for i <- 0 until n do
    eulerTour(inTime(i)) = parentEdge(i)
  val bit = XorBit(n, eulerTour)
  val removed = SumBit(n)
  val result = ArrayBuffer[Long]()
  for (t, x) <- queries do
    t match
      case 1 =>
        val xor = bit.get(outTime(x)) ^ bit.get(inTime(x) - 1)
        bit.add(inTime(x), xor)
        removed.add(inTime(x), 1)
        removed.add(outTime(x) + 1, -1)
      case 2 =>
        result.addOne(
          if removed.get(inTime(x)) > 0 then
            0L
          else
            bit.get(outTime(x)) ^ bit.get(inTime(x))
        )
  println(result.mkString("\n"))





0