import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.PrintWriter
import java.util.*

class lazySegTree<Monoid, F>(private val n: Int, private val op: (Monoid, Monoid) -> Monoid, private val e: Monoid, private val mapping: (F, Monoid) -> Monoid, private val composition: (F, F) -> F, private val id: F) {
    private var d: MutableList<Monoid>
    private var lz: MutableList<F>
    private var size = 1
    private var log = 0

    init {
        while (size < n) {
            size *= 2
            log++
        }
        d = MutableList(2 * size) { e }
        lz = MutableList(size) { id }
    }

    constructor(n: Int, op: (Monoid, Monoid) -> Monoid, e: Monoid, mapping: (F, Monoid) -> Monoid, composition: (F, F) -> F, id: F, a: Array<Monoid>) : this(n, op, e, mapping, composition, id) {
        for (i in 0 until n) {
            d[size + i] = a[i]
        }
        for (i in size - 1 downTo 1) {
            update(i)
        }
    }

    operator fun set(p: Int, x: Monoid) {
        if (p !in 0 until n) {
            throw Exception()
        }
        val p = p + size
        for (i in log downTo 1) {
            push(p shr i)
        }
        d[p] = x
        for (i in 1..log) {
            update(p shr i)
        }
    }

    operator fun get(p: Int): Monoid {
        if (p !in 0 until n) {
            throw Exception()
        }
        val p = p + size
        for (i in log downTo 1) {
            push(p shr i)
        }
        return d[p]
    }

    fun prod(l: Int, r: Int): Monoid {
        if (l !in 0..r || r !in l..n) {
            throw Exception()
        }
        if (l == r) return e
        var l = l + size
        var r = r + size
        for (i in log downTo 1) {
            if ((l shr i) shl i != l) push(l shr i)
            if ((r shr i) shl i != r) push(r shr i)
        }
        var sml = e
        var smr = e
        while (l < r) {
            if (l and 1 != 0) sml = op(sml, d[l++])
            if (r and 1 != 0) smr = op(d[--r], smr)
            l /= 2
            r /= 2
        }
        return op(sml, smr)
    }

    fun allProd(): Monoid {
        return d[1]
    }

    fun apply(p: Int, f: F) {
        if (p !in 0 until n) {
            throw Exception()
        }
        val p = p + size
        for (i in log downTo 1) {
            push(p shr i)
        }
        d[p] = mapping(f, d[p])
        for (i in 1..log) {
            update(p shr i)
        }
    }

    fun apply(l: Int, r: Int, f: F) {
        if (l !in 0..r || r !in l..n) {
            throw Exception()
        }
        if (l == r) return
        var l = l + size
        var r = r + size
        for (i in log downTo 1) {
            if ((l shr i) shl i != l) push(l shr i)
            if ((r shr i) shl i != r) push((r - 1) shr i)
        }
        val l2 = l
        val r2 = r
        while (l < r) {
            if (l and 1 != 0) allApply(l++, f)
            if (r and 1 != 0) allApply(--r, f)
            l /= 2
            r /= 2
        }
        l = l2
        r = r2
        for (i in 1..log) {
            if ((l shr i) shl i != l) update(l shr i)
            if ((r shr i) shl i != r) update((r - 1) shr i)
        }
    }

    fun maxRight(l: Int, g: (Monoid) -> Boolean): Int {
        if (l !in 0..n || !g(e)) {
            throw Exception()
        }
        if (l == n) return n
        var l = l + size
        for (i in log downTo 1) push(l shr i)
        var sm = e
        do {
            while (l % 2 == 0) l /= 2
            if (!g(op(sm, d[l]))) {
                while (l < size) {
                    push(l)
                    l = 2 * l
                    if (g(op(sm, d[l]))) {
                        sm = op(sm, d[l])
                        l++
                    }
                }
                return l - size
            }
            sm = op(sm, d[l])
            l++
        } while ((l and -l) != l)
        return n
    }

    fun minLeft(r: Int, g: (Monoid) -> Boolean): Int {
        if (r !in 0..n || !g(e)) {
            throw Exception()
        }
        if (r == 0) return 0
        var r = r + size
        for (i in log downTo 1) push((r - 1) shr i)
        var sm = e
        do {
            r--
            while (r > 1 && r % 2 != 0) r /= 2
            if (!g(op(d[r], sm))) {
                while (r < size) {
                    push(r)
                    r = 2 * r + 1
                    if (g(op(d[r], sm))) {
                        sm = op(d[r], sm)
                        r--
                    }
                }
                return r + 1 - size
            }
            sm = op(d[r], sm)
        } while ((r and -r) != r)
        return 0
    }

    private fun update(k: Int) {
        d[k] = op(d[2 * k], d[2 * k + 1])
    }

    private fun allApply (k: Int, f: F) {
        d[k] = mapping(f, d[k])
        if (k < size) {
            lz[k] = composition(f, lz[k])
        }
    }

    private fun push(k: Int) {
        allApply(2 * k, lz[k])
        allApply(2 * k + 1, lz[k])
        lz[k] = id
    }
}

data class S(val zero: Long, val one: Long, val inversion: Long)

fun op(l: S, r: S): S {
    return S(l.zero + r.zero, l.one + r.one, l.inversion + r.inversion + l.one * r.zero)
}

fun mapping(l: Boolean, r: S): S {
    if (!l) return r
    return S(r.one, r.zero, r.one * r.zero - r.inversion)
}

fun composition(l: Boolean, r: Boolean): Boolean {
    return (l and !r) or (!l and r)
}

fun PrintWriter.solve(sc: FastScanner) {
    val e = S(0, 0, 0)
    val id = false
    val n = sc.nextInt()
    val q = sc.nextInt()
    val a = Array(n) { sc.nextInt() }
    val b = a.map { if (it == 0) S(1, 0, 0) else S(0, 1, 0) }.toTypedArray()
    val seg = lazySegTree(n, ::op, e, ::mapping, ::composition, id, b)
    for (i in 0 until q) {
        val t = sc.nextInt()
        val l = sc.nextInt() - 1
        val r = sc.nextInt()
        if (t == 1) {
            seg.apply(l, r, true)
        } else {
            println(seg.prod(l, r).inversion)
        }
    }
}

fun main() {
    val writer = PrintWriter(System.out, false)
    writer.solve(FastScanner(System.`in`))
    writer.flush()
}

class FastScanner(s: InputStream) {
    private var st = StringTokenizer("")
    private val br = BufferedReader(InputStreamReader(s))

    fun next(): String {
        while (!st.hasMoreTokens()) st = StringTokenizer(br.readLine())

        return st.nextToken()
    }

    fun nextInt() = next().toInt()
    fun nextLong() = next().toLong()
    fun nextLine() = br.readLine()
    fun nextDouble() = next().toDouble()
    fun ready() = br.ready()
}

Measured memory_bytes: 101712