#pragma GCC target("avx2,avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//using namespace __gnu_pbds;
//using i128 = __int128_t;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
constexpr char ln = '\n';
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
inline int topbit(int x) {return x == 0 ? -1 : 31-__builtin_clz(x);}
inline int topbit(long long x) {return x == 0 ? -1 : 63-__builtin_clzll(x);}
inline int botbit(int x) {return x == 0 ? 32 : __builtin_ctz(x);}
inline int botbit(long long x) {return x == 0 ? 64 : __builtin_ctzll(x);}
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
inline int kthbit(long long x, int k) {return (x>>k)&1;}
inline void print() {cout << "\n";}
template<class T>
inline void print(const vector<T> &v) {
    for (auto itr = v.begin(); itr != v.end(); ++itr) cout << *itr << " ";
    print();
}
template<class T, class... Args>
inline void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
#ifdef MINATO_LOCAL
#define dump(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
inline void debug() {cerr << endl;}
template<class T>
inline void debug(const vector<T> &v) {
    for (auto itr = v.begin(); itr != v.end(); ++itr) cerr << *itr << " ";
    debug();
}
template<class T, class... Args>
inline void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
#else
#define dump(x) void(0)
inline void debug() {}
template<class T> inline void debug(const vector<T> &v) {}
template<class T, class... Args> inline void debug(const T &x, const Args &... args) {}
#endif
struct Fast_ios {Fast_ios() {cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20);};} fast_ios;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

template<typename T, typename U, typename F, typename G, typename H>
struct LazySegmentTree {
    F f;
    G g;
    H h;
    T unitynode;
    U unitylazy;
    int N,height;
    vector<T> node;
    vector<U> lazy;

    LazySegmentTree() {}
    LazySegmentTree(F f, G g, H h, T unitynode, U unitylazy) : f(f), g(g), h(h), unitynode(unitynode), unitylazy(unitylazy) {}

    void init(int sz) {
        N = 1;
        height = 0;
        while (N < sz) {
            N <<= 1;
            ++height;
        }
        node.assign(2*N,unitynode);
        lazy.assign(2*N,unitylazy);
    }

    void set(int k, const T &val) {node[k+N] = val;}

    void build() {
        for (int i = N-1; i > 0; --i) {
            node[i] = f(node[i<<1|0],node[i<<1|1]);
        }
    }

    inline T reflect(int k) {
        return lazy[k] == unitylazy ? node[k] : g(node[k],lazy[k]);
    }

    void eval(int k) {
        if (lazy[k] == unitylazy) return;
        if (k < N) {
            lazy[k<<1|0] = h(lazy[k<<1|0],lazy[k]);
            lazy[k<<1|1] = h(lazy[k<<1|1],lazy[k]);
        }
        node[k] = reflect(k);
        lazy[k] = unitylazy;
    }

    inline void recalc(int k) {
        while (k >>= 1) {
            node[k] = f(reflect(k<<1|0),reflect(k<<1|1));
        }
    }

    // [l,r) (0-indexed)
    void update(int l, int r, U val) {
        if (l >= r) return;
        l += N; r += N-1;
        for (int i = height; i > 0; --i) {
            eval(l>>i);
            eval(r>>i);
        }
        int a = l; int b = r++;
        while (l < r) {
            if (l & 1) lazy[l] = h(lazy[l],val), ++l;
            if (r & 1) --r, lazy[r] = h(lazy[r],val);
            l >>= 1; r >>= 1;
        }
        recalc(a);
        recalc(b);
    }

    T get(int l, int r) {
        if (l >= r) return unitynode;
        l += N; r += N-1;
        for (int i = height; i > 0; --i) {
            eval(l>>i);
            eval(r>>i);
        }
        ++r;
        T vl = unitynode, vr = unitynode;
        while (l < r) {
            if (l & 1) vl = f(vl,reflect(l++));
            if (r & 1) vr = f(reflect(--r),vr);
            l >>= 1; r >>= 1;
        }
        return f(vl,vr);
    }

    template<typename C>
    int binarysearch(int s, C &check, T &acc, int k, int l, int r) {
        if(r-l==1) {
            acc = f(acc,reflect(k));
            return check(acc) ? k-N : -1;
        }
        eval(k);
        int m = (l+r)>>1;
        if(m <= s) return binarysearch(s, check, acc, (k<<1)|1, m, r);
        if(s <= l && !check(f(acc,node[k]))){
            acc = f(acc,node[k]);
            return -1;
        }
        int vl = binarysearch(s, check, acc, (k<<1)|0, l, m);
        if(~vl) return vl;
        return binarysearch(s, check, acc, (k<<1)|1, m, r);
    }

    template<typename C>
    int binarysearch(int s ,C &check) {
        T acc = unitynode;
        return binarysearch(s,check,acc,1,0,N);
    }
};

struct Node {
    ll cnt0, cnt1, cnt01, cnt10;
    Node (ll cnt0, ll cnt1, ll cnt01, ll cnt10) : cnt0(cnt0), cnt1(cnt1), cnt01(cnt01), cnt10(cnt10) {}
};

int main() {
    int N,Q; cin >> N >> Q;
    vector<int> A(N);
    rep(i,N) cin >> A[i];

    auto f=[](Node a, Node b) {
        return Node(a.cnt0+b.cnt0,a.cnt1+b.cnt1,a.cnt01+b.cnt01+a.cnt0*b.cnt1,a.cnt10+b.cnt10+a.cnt1*b.cnt0);
    };
    auto g=[](Node a, int val) {
        if (val) return Node(a.cnt1,a.cnt0,a.cnt10,a.cnt01);
        return a;
    };
    auto h=[](int a, int b) {return a^b;};
    const Node id0 = Node(0,0,0,0);
    const int id1 = 0;
    LazySegmentTree seg(f,g,h,id0,id1);
    seg.init(N);
    rep(i,N) seg.set(i,Node(1-A[i],A[i],0,0));
    seg.build();

    while (Q--) {
        int t,l,r; cin >> t >> l >> r;
        --l;
        if (t==1) seg.update(l,r,1);
        else {
            auto n = seg.get(l,r);
            cout << n.cnt10 << ln;
        }
    }
}


Measured memory_bytes: 22328