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
    if rem < idx then rem, idx = idx, rem end
    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
-- while true do
  local t, l, r = io.read("*n", "*n", "*n")
  -- if t == 0 then break end
  if t == 1 then
    lzst:setRange(l, r)
  else
    print(lzst:getRange(l, r))
  end
end
