scala solution. SortedSet
```scala []
object Solution {
  import scala.collection.immutable.SortedSet
  def maxRemoval(nums: Array[Int], queries: Array[Array[Int]]): Int = {
    val qs = queries.map(a => (a.head,a.last)).sortBy(x => (x._1,-x._2))
    def f(i:Int,j:Int,aSet:Set[(Int,Int)],bSet:Set[(Int,Int)],acc:Int): Int =
      if(i>=nums.length) (qs.length-j)+acc+bSet.size
      else if(j<qs.length && qs(j)._1<=i) f(i,j+1,aSet,bSet+((qs(j)._2,j)),acc)
      else if(aSet.headOption.exists(_._1 < i)) f(i,j,aSet.tail,bSet,acc)
      else if(aSet.size>=nums(i)) f(i+1,j,aSet,bSet,acc)
      else if(bSet.headOption.exists(_._1 < i)) f(i,j,aSet,bSet.tail,acc+1)
      else if(bSet.nonEmpty) f(i,j,aSet+bSet.last,bSet.init,acc)
      else if(j>=qs.length || qs(j)._1>i) -1
      else if(qs(j)._2<j) f(i+1,j,aSet,bSet,acc+1)
      else f(i,j+1,aSet,bSet+((qs(j)._2,j)),acc)
    f(0,0,SortedSet(),SortedSet(),0)
  }
}
```
['Greedy', 'Sorting', 'Heap (Priority Queue)', 'Ordered Set', 'Scala']