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: Time Limit Exceeded)

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 '^'::(right Xor rest) ⇒ Some(Xor(left, right), rest)
        case left Number '^'::(right Number rest) ⇒ Some(Xor(left, right), rest)
        case _ ⇒ None
      }
    }
    def apply(left: HighPriority, right: HighPriority): XorNode = {
      if (left.length * 2 < right.length){
        right match {
          case XorNode(_, l, r) ⇒ XorNode(left.length + right.length + 1, Xor(left, l), r)
          case _ ⇒ XorNode(left.length + right.length + 1, left, right)
        }
      }else if (right.length * 2 < left.length){
        left match {
          case XorNode(_, l, r) ⇒ XorNode(left.length + right.length + 1, l, Xor(r, right))
          case _ ⇒ XorNode(left.length + right.length + 1, left, right)
        }
      }else XorNode(left.length + right.length + 1, left, right)
    }
  }
  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 left HighPriority '&'::(right And rest) ⇒ Some(And(left, right), rest)
        case left HighPriority '&'::(right HighPriority rest) ⇒ Some(And(left, right), rest)
        case _ ⇒ None
      }
    }
    def apply(left: NormalPriority, right: NormalPriority): AndNode = {
      if (left.length * 2 < right.length){
        right match {
          case AndNode(_, l, r) ⇒ AndNode(left.length + right.length + 1, And(left, l), r)
          case _ ⇒ AndNode(left.length + right.length + 1, left, right)
        }
      }else if (right.length * 2 < left.length){
        left match {
          case AndNode(_, l, r) ⇒ AndNode(left.length + right.length + 1, l, And(r, right))
          case _ ⇒ AndNode(left.length + right.length + 1, left, right)
        }
      }else AndNode(left.length + right.length + 1, left, right)
    }
  }
  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 left NormalPriority '|'::(right Or rest) ⇒ Some(Or(left, right), rest)
        case left NormalPriority '|'::(right NormalPriority rest) ⇒ Some(Or(left, right), rest)
        case _ ⇒ None
      }
    }
    def apply(left: ExpressionNode, right: ExpressionNode): OrNode = {
      if (left.length * 2 < right.length){
        right match {
          case OrNode(_, l, r) ⇒ OrNode(left.length + right.length + 1, Or(left, l), r)
          case _ ⇒ OrNode(left.length + right.length + 1, left, right)
        }
      }else if (right.length * 2 < left.length){
        left match {
          case OrNode(_, l, r) ⇒ OrNode(left.length + right.length + 1, l, Or(r, right))
          case _ ⇒ OrNode(left.length + right.length + 1, left, right)
        }
      }else OrNode(left.length + right.length + 1, left, right)
    }
  }
  object Expression{
    def unapply(arg: List[Char]): Option[ExpressionNode] = {
      arg match {
        case node Or Expression(e) ⇒ Some(OrNode(node.length + e.length + 1, node, e))
        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 {
    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 {
    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 {
    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)
  }
}