class Lazy_SegTree
    def initialize(v, e, id)
        @n = v.size
        @e = e
        @id = id

        @log = (@n-1).bit_length
        @size = 1 << @log
        @d = Array.new(2 * @size, e)
        @lz = Array.new(@size, id)

        @n.times {|i| @d[@size + i] = v[i]}
        (@size - 1).downto(1) {|i| update(i)}
    end

    def op(s1, s2)
        [s1[0] + s2[0], s1[1] + s2[1], s1[2] + s2[2] + s1[1] * s2[0]]
    end

    def mapping(f, s)
        f == 0 ? s : [s[1], s[0], s[0] * s[1] - s[2]]
    end

    def composition(f1, f2)
        f1 ^ f2
    end

    def update(k); @d[k] = op(@d[2 * k], @d[2 * k + 1]); end

    def push(k)
        all_apply(2 * k, @lz[k])
        all_apply(2 * k + 1, @lz[k])
        @lz[k] = @id
    end

    def prod(l, r)
        return @e if l == r
        l += @size
        r += @size

        @log.downto(1) do |i|
            push(l >> i) if (l >> i) << i != l
            push(r >> i) if (r >> i) << i != r
        end

        sml = smr = @e
        while l < r
            (sml = op(sml, @d[l]); l += 1) if l[0] == 1
            smr = op(@d[r -= 1], smr) if r[0] == 1
            l >>= 1
            r >>= 1
        end

        op(sml, smr)
    end

    def all_apply(k, f) 
        @d[k] = mapping(f, @d[k])
        @lz[k] = composition(f, @lz[k]) if k < @size
    end

    def range_apply(l, r, f)
        return if l == r
        l += @size
        r += @size

        @log.downto(1) do |i|
            push(l >> i) if (l >> i) << i != l
            push((r - 1) >> i) if (r >> i) << i != r
        end

        l2, r2 = l, r
        while l < r
            (all_apply(l, f); l += 1) if l[0] == 1
            all_apply(r -= 1, f) if r[0] == 1
            l >>= 1
            r >>= 1
        end

        l, r = l2, r2
        1.upto(@log) do |i|
            update(l >> i) if (l >> i) << i != l
            update((r - 1) >> i) if (r >> i) << i != r
        end
    end
end

n, q = gets.split.map(&:to_i)
a = gets.split.map{|e| e = e.to_i; [e ^ 1, e, 0]}

lst = Lazy_SegTree.new(a, [0,0,0], 0)

q.times do
    t, l, r = gets.split.map(&:to_i)

    if t == 1
        lst.range_apply(l-1, r, 1)
    else
        puts lst.prod(l-1, r)[2]
    end
end