module mod_fenwick_tree
  implicit none
  integer, private, parameter :: intkind = 4

  type fenwick_tree
    private
    integer :: n, p
    integer(intkind), allocatable :: body(:)
  contains
    procedure :: init => init
    procedure :: add => add
    procedure :: sum0 => sum0
    procedure :: sum1 => sum1
    generic :: sum => sum0, sum1
    procedure :: get => get
    procedure :: lower_bound => lower_bound
    procedure :: update => update
    procedure :: max => max0
  end type

  interface fenwick_tree
    module procedure :: newft0
  end interface fenwick_tree

contains

  type(fenwick_tree) function newft0(n) result(res)
    integer, intent(in) :: n
    integer :: p
    p = 1
    do while (lshift(p, 1) <= n)
      p = lshift(p, 1)
    end do
    res%n = n
    res%p = p
    allocate(res%body(n))
    res%body = 0
  end

  subroutine init(this, n)
    class(fenwick_tree), intent(out) :: this
    integer, intent(in) :: n
    integer :: p
    p = 1
    do while (lshift(p, 1) <= n)
      p = lshift(p, 1)
    end do
    this%n = n
    this%p = p
    allocate(this%body(n))
    this%body = 0
  end

  subroutine add(this, i, v)
    class(fenwick_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer(intkind), intent(in) :: v
    integer :: x
    x = i
    do while (x <= this%n)
      this%body(x) = this%body(x) + v
      x = x + and(x, -x)
    end do
  end

  integer(intkind) function sum0(this, i) result(res)
    class(fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    res = 0
    x = i
    do while (x > 0)
      res = res + this%body(x)
      x = x - and(x, -x)
    end do
  end

  integer(intkind) function sum1(this, l, r) result(res)
    class(fenwick_tree), intent(in) :: this
    integer, intent(in) :: l, r
    integer :: i, j
    res = 0
    i = l - 1
    j = r
    do while (j > i)
      res = res + this%body(j)
      j = j - and(j, -j)
    end do
    do while (i > j)
      res = res - this%body(i)
      i = i - and(i, -i)
    end do
  end

  integer(intkind) function get(this, i) result(res)
    class(fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    res = sum1(this, i, i)
  end

  integer function lower_bound(this, v) result(res)
    class(fenwick_tree), intent(in) :: this
    integer, intent(in) :: v
    integer :: k
    integer :: w
    res = 0
    if (v <= 0) return
    k = this%p
    w = v
    do while (k > 0)
      if (res + k <= this%n .and. this%body(res + k) < w) then
        w = w - this%body(res + k)
        res = res + k
      end if
      k = rshift(k, 1)
    end do
    res = res + 1
  end

  subroutine update(this, i, v)
    class(fenwick_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer(intkind), intent(in) :: v
    integer :: x
    x = i
    do while (x <= this%n)
      if (this%body(x) < v) this%body(x) = v
      x = x + and(x, -x)
    end do
  end

  integer(intkind) function max0(this, i) result(res)
    class(fenwick_tree), intent(in) :: this
    integer, intent(in) :: i
    integer :: x
    x = i
    res = 0
    do while (x > 0)
      res = max(res, this%body(x))
      x = x - and(x, -x)
    end do
  end

  integer(intkind) function lis_length(a) result(res)
    integer, intent(in) :: a(:)
    type(fenwick_tree) :: bit
    integer(intkind) :: tmp
    integer :: n, i
    n = size(a)
    call init(bit, n)
    res = 0
    do i = 1, n
      tmp = max0(bit, a(i) - 1) + 1
      res = max(res, tmp)
      call update(bit, a(i), tmp)
    end do
  end

  integer(intkind) function inversion_num(a) result(res)
    integer, intent(in) :: a(:)
    type(fenwick_tree) :: bit
    integer :: n, i
    n = size(a)
    call init(bit, n)
    res = 0
    do i = 1, n
      call add(bit, a(i), 1_intkind)
      res = res + i - sum0(bit, a(i))
    end do
  end

end module mod_fenwick_tree
module mod_two_dim_merge_sort
  implicit none
  integer, private, parameter :: intkind = 4
contains
  logical function less(a, b, k) result(res)
    integer(intkind), intent(in) :: a(:), b(:)
    integer :: k
    res = a(k) < b(k)
  end
  subroutine merge_sort(a, k)
    integer(intkind), intent(inout) :: a(:, :)
    integer, intent(in) :: k
    integer :: n, m, l, i, u
    n = size(a, 1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m / 2
        u = min(2 * i * l, n)
        call merger(a(2 * (i - 1) * l + 1:(2 * i - 1) * l, :), &
        a((2 * i - 1) * l + 1:u, :), k)
      end do
      l = 2 * l
      m = (m + 1) / 2
    end do
  end
  subroutine merger(a1, a2, k)
    integer(intkind), intent(inout) :: a1(:, :), a2(:, :)
    integer, intent(in) :: k
    integer(intkind) :: a(size(a1, 1) + size(a2, 1), size(a1, 2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1, 1)
    n2 = size(a2, 1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (less(a1(i1, :), a2(i2, :), k)) then
        a(i1 + i2 - 1, :) = a1(i1, :)
        i1 = i1 + 1
      else
        a(i1 + i2 - 1, :) = a2(i2, :)
        i2 = i2 + 1
       end if
    end do
    if (i1 <= n1) then
      a(i1 + i2 - 1:n1 + n2, :) = a1(i1:n1, :)
    else
      a(i1 + i2 - 1:n1 + n2, :) = a2(i2:n2, :)
    end if
    a1 = a(1:n1, :)
    a2 = a(n1 + 1:n1 + n2, :)
  end
end module mod_two_dim_merge_sort
program range_set_query
  use mod_fenwick_tree
  use mod_two_dim_merge_sort
  implicit none
  integer :: n, q, r(500000, 3) = 0, i, l = 1
  integer, dimension(500000) :: c = 0, x = 0, g = 0
  type(fenwick_tree) :: bit
  read(*,*) n, q
  read(*,*) c(1:n)
  bit = fenwick_tree(n)
  do i = 1, q
    read(*,*) r(i, 1:2)
    r(i, 3) = i
  end do
  call merge_sort(r(1:q, 1:3), 2)
  do i = 1, q
    do while (l <= r(i, 2))
      if (g(c(l)) > 0) call bit%add(g(c(l)), -1)
      call bit%add(l, 1)
      g(c(l)) = l
      l = l + 1
    end do
    x(r(i, 3)) = bit%sum(r(i, 1), r(i, 2))
  end do
  do i = 1, q
    write(*,'(i0)') x(i)
  end do
end program range_set_query

Measured memory_bytes: 20972