Problem:
Score : 100 points

Problem 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.

Constraints

1 \leq N \leq 2 \times 10^5

0 \leq A_i \leq 1

1 \leq Q \leq 2 \times 10^5

1 \leq T_i \leq 2

1 \leq L_i \leq R_i \leq N

All values in Input are integer.

Input

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

Output

For each query with T_i=2, print the answer.

Sample Input 1

5 5
0 1 0 0 1
2 1 5
1 3 4
2 2 5
1 1 3
2 1 2

Sample Output 1

2
0
1

First query: Print 2, which is the inversion of (A_1,A_2,A_3,A_4,A_5)=(0,1,0,0,1).

Second query: Replace the value of A_3 and A_4 with 1 and 1, respectively.

Third query: Print 0, which is the inversion of (A_2,A_3,A_4,A_5)=(1,1,1,1).

Fourth query: Replace the value of A_1, A_2 and A_4 with 1, 0 and 0, respectively.

Fifth query: Print 1, which is the inversion of (A_1,A_2)=(1,0).

Solution in Lua (LuaJIT 2.1.0) (judge verdict: Wrong Answer)

local mfl, mce = math.floor, math.ceil
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local LazyRangeSeg = {}
LazyRangeSeg.create = function(self, n)
  local stagenum, mul = 1, 1
  self.n0, self.n1 = {{0}}, {{0}}
  self.c01, self.c10 = {{0}}, {{0}}
  self.lazy = {{false}}
  while mul < n do
    mul, stagenum = mul * 2, stagenum + 1
    self.n0[stagenum], self.n1[stagenum] = {}, {}
    self.c01[stagenum], self.c10[stagenum] = {}, {}
    self.lazy[stagenum] = {}
    for i = 1, mul do
      self.n0[stagenum][i], self.n1[stagenum][i] = 0, 0
      self.c01[stagenum][i], self.c10[stagenum][i] = 0, 0
      self.lazy[stagenum][i] = false
    end
  end
  self.left_stage = {}
  for i = 1, n do
    local sp, sz = 1, bls(1, stagenum - 1)
    while(i - 1) % sz ~= 0 do
      sp, sz = sp + 1, brs(sz, 1)
    end
    self.left_stage[i] = sp
  end
  self.sz_stage = {}
  local tmp, sp = 1, stagenum
  for i = 1, n do
    if tmp * 2 == i then tmp, sp = tmp * 2, sp - 1 end
    self.sz_stage[i] = sp
  end
  self.stagenum = stagenum
end
LazyRangeSeg.resolve = function(self, right)
  local stagenum = self.stagenum
  local offset = 0
  for i = 1, stagenum - 1 do
    local p = offset + bls(1, stagenum - i)
    if p < right then
      offset = p
      p = p + bls(1, stagenum - i)
    end
    if right < p then
      local curidx = brs(p, stagenum - i)
      if self.lazy[i][curidx] then
        self:resolveRange(i + 1, curidx * 2 - 1, true)
        self:resolveRange(i + 1, curidx * 2, incval, true)
        self.lazy[i + 1][curidx * 2 - 1] = not self.lazy[i + 1][curidx * 2 - 1]
        self.lazy[i + 1][curidx * 2] = not self.lazy[i + 1][curidx * 2]
        self.lazy[i][curidx] = false
      end
    elseif p == right then
      break
    else
      assert(false)
    end
  end
end
local function merge(ln0, ln1, lc01, lc10, rn0, rn1, rc01, rc10)
  return ln0 + rn0, ln1 + rn1, lc01 + rc01 + ln0 * rn1, lc10 + rc10 + ln1 * rn0
end
LazyRangeSeg.resolveRange = function(self, stagepos, idx, shallow)
  self.n0[stagepos][idx], self.n1[stagepos][idx] = self.n1[stagepos][idx], self.n0[stagepos][idx]
  self.c01[stagepos][idx], self.c10[stagepos][idx] = self.c10[stagepos][idx], self.c01[stagepos][idx]
  if shallow then return end
  for i = stagepos - 1, 1, -1 do
    local dst = brs(idx + 1, 1)
    local rem = dst * 4 - 1 - idx
    self.n0[i][dst], self.n1[i][dst], self.c01[i][dst], self.c10[i][dst]
    = merge(self.n0[i + 1][idx], self.n1[i + 1][idx],
            self.c01[i + 1][idx], self.c10[i + 1][idx],
            self.n0[i + 1][rem], self.n1[i + 1][rem],
            self.c01[i + 1][rem], self.c10[i + 1][rem])
    idx = dst
  end
end
LazyRangeSeg.getRange = function(self, left, right)
  if 1 < left then self:resolve(left - 1) end
  self:resolve(right)
  local stagenum = self.stagenum
  local n0, n1, c01, c10 = 0, 0, 0, 0
  while left <= right do
    local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
    local sz = bls(1, stagenum - stage)
    local pos = 1 + brs(left - 1, stagenum - stage)
    n0, n1, c01, c10
    = merge(n0, n1, c01, c10, self.n0[stage][pos], self.n1[stage][pos], self.c01[stage][pos], self.c10[stage][pos])
    left = left + sz
  end
  return c10
end
LazyRangeSeg.setRange = function(self, left, right)
  if 1 < left then self:resolve(left - 1) end
  self:resolve(right)
  local stagenum = self.stagenum
  while left <= right do
    local stage = mma(self.left_stage[left], self.sz_stage[right - left + 1])
    local sz = bls(1, stagenum - stage)
    local len = right - left + 1
    local idx = 1 + brs(left - 1, stagenum - stage)
    self:resolveRange(stage, idx)
    self.lazy[stage][idx] = not self.lazy[stage][idx]
    left = left + sz
  end
end
LazyRangeSeg.setAll = function(self, s)
  local n = brs(#s + 1, 1)
  local stagenum = self.stagenum
  for i = 1, n do
    local b = s:byte(i * 2 - 1)
    if b == 48 then
      self.n0[stagenum][i] = 1
    else
      self.n1[stagenum][i] = 1
    end
  end
  for i = stagenum - 1, 1, -1 do
    local cnt = bls(1, i - 1)
    for j = 1, cnt do
      self.n0[i][j], self.n1[i][j], self.c01[i][j], self.c10[i][j]
      = merge(self.n0[i + 1][j * 2 - 1], self.n1[i + 1][j * 2 - 1],
              self.c01[i + 1][j * 2 - 1], self.c10[i + 1][j * 2 - 1],
              self.n0[i + 1][j * 2], self.n1[i + 1][j * 2],
              self.c01[i + 1][j * 2], self.c10[i + 1][j * 2])
    end
  end
end
LazyRangeSeg.new = function(n)
  local obj = {}
  setmetatable(obj, {__index = LazyRangeSeg})
  obj:create(n)
  return obj
end

local n, q = io.read("*n", "*n", "*l")
local lzst = LazyRangeSeg.new(n)
lzst:setAll(io.read())
for iq = 1, q do
  local t, l, r = io.read("*n", "*n", "*n")
  if t == 1 then
    lzst:setRange(l, r)
  else
    print(lzst:getRange(l, r))
  end
end