#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template <typename S>
struct LazySegmentTree {
    using V = typename S::V;
    using T = typename V::T;
    using O = typename S::O;
    using E = typename O::E;

    LazySegmentTree(int n) : LazySegmentTree(vector<T>(n, V::id)) {}
    LazySegmentTree(const vector<T>& v) {
        size = 1;
        height = 0;
        while (size < v.size()) size <<= 1, height++;
        node.resize(2 * size, V::id);
        lazy.resize(2 * size, O::id);
        copy(v.begin(), v.end(), node.begin() + size);
        for (int i = size - 1; i > 0; i--) node[i] = V::op(node[2 * i], node[2 * i + 1]);
    }

    T operator[](int k) {
        return query(k, k + 1);
    }

    void update(int a, int b, const E& x, int k = 1, int l = 0, int r = -1) {
        if (r == -1) r = size;

        push(k, r - l);
        if (r <= a || b <= l) return;
        if (a <= l && r <= b) {
            lazy[k] = O::op(lazy[k], x);
            push(k, r - l);
            return;
        }
        int m = (l + r) / 2;
        update(a, b, x, 2 * k, l, m);
        update(a, b, x, 2 * k + 1, m, r);
        node[k] = V::op(node[2 * k], node[2 * k + 1]);
    }

    T query(int a, int b, int k = 1, int l = 0, int r = -1) {
        if (r == -1) r = size;

        push(k, r - l);
        if (r <= a || b <= l) return V::id;
        if (a <= l && r <= b) return node[k];
        int m = (l + r) / 2;
        return V::op(query(a, b, 2 * k, l, m),
                     query(a, b, 2 * k + 1, m, r));
    }

private:
    int size, height;
    vector<T> node;
    vector<E> lazy;

    void push(int k, int len) {
        if (lazy[k] == O::id) return;
        if (k < size) {
            lazy[2 * k] = O::op(lazy[2 * k], lazy[k]);
            lazy[2 * k + 1] = O::op(lazy[2 * k + 1], lazy[k]);
        }
        node[k] = S::op(node[k], S::mul(lazy[k], len));
        lazy[k] = O::id;
    }
};

struct X {
    int zero, one;
    ll num1, num2;
};

struct S {
    struct V {
        using T = X;
        inline static const T id = {0, 0, 0, 0};
        static T op(T a, T b) {
            X c;
            c.zero = a.zero + b.zero;
            c.one = a.one + b.one;
            c.num1 = a.num1 + b.num1 + (ll) a.one * b.zero;
            c.num2 = a.num2 + b.num2 + (ll) a.zero * b.one;
            return c;
        }
    };

    struct O {
        using E = bool;
        inline static const E id = false;
        static E op(E a, E b) {
            return a ^ b;
        }
    };

    static V::T op(V::T a, O::E b) {
        return {a.one, a.zero, a.num2, a.num1};
    }

    static O::E mul(O::E a, int b) {
        return a;
    }
};


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int N, Q;
    cin >> N >> Q;
    vector<X> A(N);
    for (int i = 0; i < N; i++) {
        int a;
        cin >> a;
        if (a) A[i].one++;
        else A[i].zero++;
    }
    LazySegmentTree<S> st(A);
    for (int i = 0; i < Q; i++) {
        int T, L, R;
        cin >> T >> L >> R;
        if (T == 1) {
            st.update(L - 1, R, true);
        } else {
            cout << st.query(L - 1, R).num1 << "\n";
        }
    }
}

Measured memory_bytes: 20380