// Template
#include <bits/stdc++.h>
#include <atcoder/all>
#define rep_override(x, y, z, name, ...) name
#define rep2(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define rep3(i, l, r) for (ll i = (ll)(l); i < (ll)(r); ++i)
#define rep(...) rep_override(__VA_ARGS__, rep3, rep2)(__VA_ARGS__)
#define per(i, n) for (ll i = (ll)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
using namespace std;
using namespace atcoder;
using ll = long long;
constexpr int inf = 1001001001;
constexpr ll INF = 3003003003003003003LL;
template <typename T> inline bool chmin(T &x, const T &y) {if (x > y) {x = y; return 1;} return 0;}
template <typename T> inline bool chmax(T &x, const T &y) {if (x < y) {x = y; return 1;} return 0;}
struct IOSET {IOSET() {cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10);}} ioset;

// Lazy Segment Tree
template <typename Operator>
class LazySegmentTree {
    Operator OP;
    using NodeType = decltype(OP.NodeE);
    using FuncType = decltype(OP.FuncE);
    int length, height;
    vector<NodeType> node;
    vector<FuncType> lazy;
    vector<int> width;
    void eval(int n) {
        node[n] = OP.NodeFunc(node[n], lazy[n], width[n]);
        if (n < length) {
            lazy[(n << 1) | 0] = OP.MergeFunc(lazy[(n << 1) | 0], lazy[n]);
            lazy[(n << 1) | 1] = OP.MergeFunc(lazy[(n << 1) | 1], lazy[n]);
        }
        lazy[n] = OP.FuncE;
    }
public:
    LazySegmentTree(int n) {
        length = 1;
        height = 0;
        while (length < n) {
            length <<= 1;
            ++height;
        }
        node.resize(length << 1, OP.NodeE);
        lazy.resize(length << 1, OP.FuncE);
        width.resize(length << 1, 1);
        for (int i = length - 1; i > 0; --i) width[i] = width[i << 1] << 1;
    }
    LazySegmentTree(vector<NodeType> &vec) {
        length = 1;
        height = 0;
        while (length < vec.size()) {
            length <<= 1;
            ++height;
        }
        node.resize(length << 1, OP.NodeE);
        rep(i, vec.size()) node[i + length] = vec[i];
        for (int i = length - 1; i > 0; --i) node[i] = OP.MergeNode(node[(i << 1) | 0], node[(i << 1) | 1]);
        lazy.resize(length << 1, OP.FuncE);
        width.resize(length << 1, 1);
        for (int i = length - 1; i > 0; --i) width[i] = width[i << 1] << 1;
    }
    void update(int a, int b, FuncType x) {
        int l = a + length, r = b + length - 1;
        for (int i = height; i > 0; --i) {
            eval(l >> i);
            eval(r >> i);
        }
        ++r;
        while (r > l) {
            if (l & 1) {
                lazy[l] = OP.MergeFunc(lazy[l], x);
                eval(l);
                ++l;
            }
            if (r & 1) {
                --r;
                lazy[r] = OP.MergeFunc(lazy[r], x);
                eval(r);
            }
            l >>= 1; r >>= 1;
        }
        l = a + length; r = b + length - 1;
        while (l >>= 1) node[l] = OP.MergeNode(OP.NodeFunc(node[(l << 1) | 0], lazy[(l << 1) | 0], width[(l << 1) | 0]), OP.NodeFunc(node[(l << 1) | 1], lazy[(l << 1) | 1], width[(l << 1) | 1]));
        while (r >>= 1) node[r] = OP.MergeNode(OP.NodeFunc(node[(r << 1) | 0], lazy[(r << 1) | 0], width[(r << 1) | 0]), OP.NodeFunc(node[(r << 1) | 1], lazy[(r << 1) | 1], width[(r << 1) | 1]));
    }
    NodeType get(int a, int b) {
        int l = a + length, r = b + length - 1;
        for (int i = height; i >= 0; --i) {
            eval(l >> i);
            eval(r >> i);
        }
        ++r;
        NodeType vl = OP.NodeE, vr = OP.NodeE;
        while (l < r) {
            if (l & 1) {
                vl = OP.MergeNode(vl, OP.NodeFunc(node[l], lazy[l], width[l]));
                ++l;
            }
            if (r & 1) {
                --r;
                vr = OP.MergeNode(OP.NodeFunc(node[r], lazy[r], width[r]), vr);
            }
            l >>= 1; r >>= 1;
        }
        return OP.MergeNode(vl, vr);
    }
};
struct Node {
    ll zero, one, t, t_;
};
struct Tentou {
    using NodeType = Node;
    using FuncType = int;
    NodeType NodeE = Node{0, 0, 0, 0};
    FuncType FuncE = 0;
    NodeType MergeNode(NodeType x, NodeType y) {
        return Node{x.zero + y.zero, x.one + y.one, x.t + y.t + x.one * y.zero, x.t_ + y.t_ + x.zero * y.one};
    }
    FuncType MergeFunc(FuncType x, FuncType y) {
        return x ^ y;
    }
    NodeType NodeFunc(NodeType x, FuncType y, int len) {
        if (y) return Node{x.one, x.zero, x.t_, x.t};
        else return x;
    }
};

// Main
int main() {
    int n, q;
    cin >> n >> q;
    vector<Node> a;
    rep(i, n) {
        int a_;
        cin >> a_;
        if (a_) a.push_back(Node{0, 1, 0, 0});
        else a.push_back(Node{1, 0, 0, 0});
    }
    
    LazySegmentTree<Tentou> st(a);
    
    rep(i, q) {
        int t, l, r;
        cin >> t >> l >> r;
        --l;
        if (t == 1) st.update(l, r, 1);
        else cout << st.get(l, r).t << "\n";
    }
}


Measured memory_bytes: 30016