output_description: For each query with T_i=2, print the answer.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N Q
      A_1 A_2 \cdots A_N
      T_1 L_1 R_1
      T_2 L_2 R_2
      \vdots
      T_Q L_Q R_Q
attempt: # self.data: 1-indexed
  #    __1__
  #  _2_   _3_
  # 4   5 6   7
  
  # f(f(a, b), c) == f(a, f(b, c))
  
  
  class LazySegmentTree:
      # a = [default] * n
      # O(n)
      def __init__(self, n, f, default=(0, 0, 1)):
          self.num_leaf = 2 ** (n - 1).bit_length()
          self.data = [default] * (2 * self.num_leaf)
          self.lazy = [True] * (2 * self.num_leaf)
          self.f = f
  
      # You can use first_update before you use update.
      # a[i] = x
      # O(log(n))
      def first_update(self, i, x):
          i += self.num_leaf
          self.data[i] = x
          i >>= 1
          while i > 0:
              self.data[i] = self.f(self.data[2 * i], self.data[2 * i + 1])
              i >>= 1
  
      def gindex(self, l, r):
          lis = []
          l += self.num_leaf
          r += self.num_leaf
          lm = (l // (l & -l)) >> 1
          rm = (r // (r & -r)) >> 1
          while l < r:
              if l <= lm:
                  lis.append(l)
              if r <= rm:
                  lis.append(r)
              l >>= 1
              r >>= 1
          while l:
              lis.append(l)
              l >>= 1
          lis.reverse()
          return lis
  
      def lazy_processing(self, i):
          self.data[i] = (
              self.data[i][1] * self.data[i][2] - self.data[i][0],
              self.data[i][2],
              self.data[i][1],
          )
          self.lazy[i] = not self.lazy[i]
  
      # from parent to children
      def propagate(self, lis):
          for i in lis:
              if self.lazy[i]:
                  continue
              self.lazy_processing(2 * i)
              self.lazy_processing(2 * i + 1)
              self.lazy[i] = True
  
      # update a[l:r]
      def update(self, l, r):
          lis = self.gindex(l, r)
  
          # top-down propagation
          self.propagate(lis)
  
          l += self.num_leaf
          r += self.num_leaf - 1
          while l < r:
              if l & 1:
                  self.lazy_processing(l)
                  l += 1
              if not r & 1:
                  self.lazy_processing(r)
                  r -= 1
              l >>= 1
              r >>= 1
          if l == r:
              self.lazy_processing(l)
  
          # bottom-up propagation
          lis.reverse()
          for i in lis:
              self.data[i] = self.f(self.data[2 * i], self.data[2 * i + 1])
  
      # return f(a[l:r])
      def query(self, l, r):
          # top-down propagation
          self.propagate(self.gindex(l, r))
  
          l += self.num_leaf
          r += self.num_leaf - 1
          lres, rres = (0, 1, 0), self.data[0]  # self.data[0] == default
          while l < r:
              if l & 1:
                  lres = self.f(lres, self.data[l])
                  l += 1
              if not r & 1:
                  rres = self.f(self.data[r], rres)
                  r -= 1
              l >>= 1
              r >>= 1
          if l == r:
              res = self.f(self.f(lres, self.data[l]), rres)
          else:
              res = self.f(lres, rres)
          return res
  
  
  from sys import stdin
  
  input = stdin.buffer.readline
  
  
  def main():
      n, q = map(int, input().split())
      a = list(map(int, input().split()))
  
      def f(tup1, tup2):
          return (
              tup1[0] + tup2[0] + tup1[2] * tup2[1],
              tup1[1] + tup2[1],
              tup1[2] + tup2[2],
          )
  
      lst = LazySegmentTree(n, f=f)
  
      for i, x in enumerate(a):
          lst.first_update(i, (0, 1 - x, x))
  
      ans = []
      for _ in range(q):
          t, l, r = list(map(int, input().split()))
          if t == 1:
              lst.update(l - 1, r)
          else:
              ans.append(lst.query(l - 1, r)[0])
  
      for i in ans:
          print(i)
  
  
  main()
  
problem_description: Statement
  
  You are given a binary array A=(A_1,A_2,\cdots,A_N) of length N.
  
  Process Q queries of the following types. The i-th query is represented by
  three integers T_i,L_i,R_i.
  
    * T_i=1: Replace the value of A_j with 1-A_j for each L_i \leq j \leq R_i.
    * T_i=2: Calculate the inversion(*) of the array A_{L_i},A_{L_i+1},\cdots,A_{R_i}．
  
  Note：The inversion of the array x_1,x_2,\cdots,x_k is the number of the pair
  of integers i,j with 1 \leq i < j \leq k, x_i > x_j.
samples: [{"input": "5 5\n    0 1 0 0 1\n    2 1 5\n    1 3 4\n    2 2 5\n    1 1 3\n    2 1 2", "output": "2\n    0\n    1\n    \n\n  * First query: Print 2, which is the inversion of (A_1,A_2,A_3,A_4,A_5)=(0,1,0,0,1).\n  * Second query: Replace the value of A_3 and A_4 with 1 and 1, respectively.\n  * Third query: Print 0, which is the inversion of (A_2,A_3,A_4,A_5)=(1,1,1,1).\n  * Fourth query: Replace the value of A_1, A_2 and A_4 with 1, 0 and 0, respectively.\n  * Fifth query: Print 1, which is the inversion of (A_1,A_2)=(1,0)."}]