Problem:
G: 式の切り取り

問題

長さ $N$ の数式 $S$ が与えられる。
数式は、以下の BNF で示される形式になっている。

<expr> ::= <number> | <expr> <op> <expr>

<op> ::= ‘^’ | ‘&’ | ‘|’

<number> は、 $0$ 以上 $2^{31}-1$ 以下の整数を表す。

演算子、’^’ ‘&’ ‘|’ は、それぞれ排他的論理和、論理積、論理和を表す。
演算子の優先順位は以下の通りである。

高　’^’ > ‘&’ > ‘|’　低

区間 $[i, j]$ が $Q$ 個与えられる。
$S_i, \dots , S_j$ の計算結果を出力せよ。

なお、数式 $S_i, \dots , S_j$ が上記のBNFで示される形式であることは保証される。また、数式にゼロ詰めされた値が含まれることはない。

制約

$1 \leq N, Q \leq 10^5$

$N = |S| \ \ \ \ \ |S|$ は文字列の長さを表す。

$0 \leq i \leq j < N$

入力形式

入力は以下の形式で与えられる。

$N$

$S$

$Q$

$i_1\ j_1$

$\vdots$

$i_Q\ j_Q$

出力

$S_i, \dots , S_j$ の計算結果を出力せよ。 また、末尾に改行も出力せよ。

サンプル

サンプル入力 1

7
9^2&1|2
4
0 6
0 2
2 6
4 4

サンプル出力 1

3
11
2
1

サンプル入力 2

13
3423&423^1234
3
2 12
5 9
9 12

サンプル出力 2

21
422
1234

サンプル入力 3

7
1234567
3
0 0
0 1
4 6

サンプル出力 3

1
12
567

Solution in Scala (judge verdict: Accepted)

import scala.annotation.tailrec

object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  val n = readLine.trim.toInt
  val expression = readLine.trim.toList match {
    case Expression(e) ⇒ e
    case _ ⇒ ???
  }
  for (_ ← 0 until readLine.trim.toInt){
    val Array(i, j) = readLine.trim.split(' ').map(_.toInt)
    println(
      expression.calculate(i, j).get
    )
  }
  type Length = Int
  sealed trait ExpressionNode {
    def whole: Int
    def length: Length
    def calculate(from: Int, to: Int): Option[Int]
  }
  trait LowPriority extends ExpressionNode
  trait NormalPriority extends LowPriority
  trait HighPriority extends NormalPriority

  object Number {
    def unapply(arg: List[Char]): Option[(NumberNode, List[Char])] = {
      arg match {
        case h::t if h.isDigit ⇒
          val (left, right) = arg.span(_.isDigit)
          Some(NumberNode(left.length, left.map(_.asDigit)), right)
        case _ ⇒ None
      }
    }
  }
  object Xor {
    def unapply(arg: List[Char]): Option[(XorNode, List[Char])] = {
      arg match {
        case left Number '^'::t ⇒ Some(parseXor(t, List(left)))
        case _ ⇒ None
      }
    }
    private def makeXor(array: Array[NumberNode], from: Int, until: Int): HighPriority = {
      if (from + 1 == until) array(from)
      else {
        val mid = (from + until) / 2
        new XorNode(makeXor(array, from, mid), makeXor(array, mid, until))
      }
    }
    private def makeAnd(result: List[NumberNode]): XorNode = {
      val array = result.reverse.toArray
      val mid = array.length / 2
      new XorNode(makeXor(array, 0, mid), makeXor(array, mid, array.length))
    }
    @tailrec
    private def parseXor(arg: List[Char], result: List[NumberNode]): (XorNode, List[Char]) = {
      arg match {
        case number Number '^'::rest ⇒ parseXor(rest, number::result)
        case number Number rest ⇒ (makeAnd(number::result), rest)
        case _ ⇒ (makeAnd(result), arg)
      }
    }
  }
  object HighPriority {
    def unapply(arg: List[Char]): Option[(HighPriority, List[Char])] = {
      arg match {
        case xor Xor rest ⇒ Some(xor, rest)
        case number Number rest ⇒ Some(number, rest)
        case _ ⇒ None
      }
    }
  }
  object And {
    def unapply(arg: List[Char]): Option[(AndNode, List[Char])] = {
      arg match {
        case high HighPriority '&'::t ⇒ Some(parseAnd(t, List(high)))
        case _ ⇒ None
      }
    }
    private def makeAnd(array: Array[HighPriority], from: Int, until: Int): NormalPriority = {
      if (from + 1 == until) array(from)
      else {
        val mid = (from + until) / 2
        new AndNode(makeAnd(array, from, mid), makeAnd(array, mid, until))
      }
    }
    private def makeAnd(result: List[HighPriority]): AndNode = {
      val array = result.reverse.toArray
      val mid = array.length / 2
      new AndNode(makeAnd(array, 0, mid), makeAnd(array, mid, array.length))
    }
    @tailrec
    private def parseAnd(arg: List[Char], result: List[HighPriority]): (AndNode, List[Char]) = {
      arg match {
        case high HighPriority '&'::t ⇒ parseAnd(t, high::result)
        case high HighPriority t ⇒ (makeAnd(high::result), t)
        case _ ⇒ (makeAnd(result), arg)
      }
    }
  }
  object NormalPriority {
    def unapply(arg: List[Char]): Option[(NormalPriority, List[Char])] = {
      arg match {
        case and And rest ⇒ Some(and, rest)
        case high HighPriority rest ⇒ Some(high, rest)
        case _ ⇒ None
      }
    }
  }
  object Or {
    def unapply(arg: List[Char]): Option[(OrNode, List[Char])] = {
      arg match {
        case normal NormalPriority '|'::t ⇒ Some(parseOr(t, List(normal)))
        case _ ⇒ None
      }
    }
    private def makeOr(array: Array[NormalPriority], from: Int, until: Int): ExpressionNode = {
      if (from + 1 == until) array(from)
      else {
        val mid = (from + until) / 2
        new OrNode(makeOr(array, from, mid), makeOr(array, mid, until))
      }
    }
    private def makeOr(result: List[NormalPriority]): OrNode = {
      val array = result.reverse.toArray
      val mid = array.length / 2
      new OrNode(makeOr(array, 0, mid), makeOr(array, mid, array.length))
    }
    private def parseOr(arg: List[Char], result: List[NormalPriority]): (OrNode, List[Char]) = {
      arg match {
        case normal NormalPriority '|'::t ⇒ parseOr(t, normal::result)
        case normal NormalPriority t ⇒ (makeOr(normal::result), t)
        case _ ⇒ (makeOr(result), arg)
      }
    }
  }
  object Expression{
    def unapply(arg: List[Char]): Option[ExpressionNode] = {
      arg match {
        case node Or Nil ⇒ Some(node)
        case normal NormalPriority Nil ⇒ Some(normal)
        case _ ⇒ None
      }
    }
  }
  case class XorNode(length: Length, left: HighPriority, right: HighPriority) extends ExpressionNode with HighPriority {
    def this(left: HighPriority, right: HighPriority){
      this(left.length + right.length + 1, left, right)
    }
    override val whole: Int = left.whole ^ right.whole
    override def calculate(from: Int, to: Int): Option[Int] = {
      if (from <= 0 && length < to) Some(whole)
      else if (to < 0 || length <= from) None
      else Some(left.calculate(from, to).getOrElse(0) ^ right.calculate(from - left.length - 1, to - left.length - 1).getOrElse(0))
    }
  }
  case class AndNode(length: Length, left: NormalPriority, right: NormalPriority) extends ExpressionNode with NormalPriority {
    def this(left: NormalPriority, right: NormalPriority){
      this(left.length + right.length + 1, left, right)
    }
    override val whole: Int = left.whole & right.whole
    override def calculate(from: Int, to: Int): Option[Int] = {
      if (from <= 0 && length < to) Some(whole)
      else if (to < 0 || length <= from) None
      else Some(left.calculate(from, to).getOrElse(-1) & right.calculate(from - left.length - 1, to - left.length - 1).getOrElse(-1))
    }
  }
  case class OrNode(length: Length, left: ExpressionNode, right: ExpressionNode) extends ExpressionNode with LowPriority {
    def this(left: ExpressionNode, right: ExpressionNode){
      this(left.length + right.length + 1, left, right)
    }
    override val whole: Int = left.whole | right.whole
    override def calculate(from: Int, to: Int): Option[Int] = {
      if (from <= 0 && length < to) Some(whole)
      else if (to < 0 || length <= from) None
      else Some(left.calculate(from, to).getOrElse(0) | right.calculate(from - left.length - 1, to - left.length - 1).getOrElse(0))
    }
  }
  case class NumberNode(length: Length, value: List[Int]) extends ExpressionNode with HighPriority {
    override val whole: Int = value.foldLeft(0){case (l, r) ⇒ l * 10 + r}
    override def calculate(from: Int, to: Int): Option[Int] = if (length <= from || to < 0) None else if (from <= 0 && length < to) Some(whole) else Some(read(value, from, to))
    private def read(arg: List[Int], from: Int, to: Int, result: Int = 0): Int = {
      arg match {
        case h::t if from <= 0 && 0 <= to ⇒ read(t, from - 1, to - 1, result * 10 + h)
        case _::t if 0 < from ⇒ read(t, from - 1, to - 1, 0)
        case _ if to < 0 ⇒ result
        case Nil ⇒ result
      }
    }
  }

  implicit class Extension[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}