#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
template<class T> using vc = vector<T>;
template<class T> using vvc = vector<vector<T>>;

#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define repr(i, n) for (ll i = (n)-1; i >= 0; i--)
#define repe(i, l, r) for (ll i = (l); i < (r); i++)
#define reper(i, l, r) for (ll i = (r)-1; i >= (l); i--)
#define repa(i,n) for (auto& i: n)

template<class T> inline bool chmax(T &a, const T &b) {if (a<b) { a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, const T &b) {if (b<a) { a=b; return 1;} return 0;}
struct init{init(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}}init_;

#ifdef DEBUG
template <class T, class N> void verr(const T& a, const N& n) { rep(i, n) cerr << a[i] << " "; cerr << "\n" << flush; }
ll dbgt = 1; void err() { cerr << "passed " << dbgt++ << "\n" << flush; }
template<class H, class... T> void err(H&& h,T&&... t){ cerr<< h << (sizeof...(t)?" ":"\n") << flush; if(sizeof...(t)>0) err(forward<T>(t)...); }
#endif

const ll INF = 4e18;
const ld EPS = 1e-11;
const ld PI = acos(-1.0L);
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
//--------------------------------------------------------------------------------//
template <
    class T,
    class F,
    T (*fOperate)(T, T),
    T (*fAct)(T, F),
    F (*fMerge)(F, F)
>
struct LazySegTree {
    int n, height;
    // 値配列, 値の単位元
    vector<T> data;
    T dataId;
    // 遅延配列, 遅延値の単位元
    vector<F> lazy;
    F lazyId;

    LazySegTree(
        int _n, T _did, F _lid
    )
        : dataId(_did), lazyId(_lid){
        n = 1, height = 0;
        while (n < _n) n *= 2, height++;
        data = vector<T>(2 * n, dataId);
        lazy = vector<F>(2 * n, lazyId);
    }

    inline T act(int k){
        return lazy[k] == lazyId ? data[k] : fAct(data[k], lazy[k]);
    }

    inline void propagate(int k){
        if (lazy[k] == lazyId) return;
        lazy[2 * k + 0] = fMerge(lazy[2 * k + 0], lazy[k]);
        lazy[2 * k + 1] = fMerge(lazy[2 * k + 1], lazy[k]);
        data[k] = fAct(data[k], lazy[k]), lazy[k] = lazyId;
    }


    // [a, b)を更新
    inline void update(int a, int b, F x) {
        if (a == b) return;
        a += n, b += n - 1;
        for (int i = height; i > 0; i--) propagate(a >> i), propagate(b >> i);
        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
            if (l & 1) lazy[l] = fMerge(lazy[l], x), l++;
            if (r & 1) r--, lazy[r] = fMerge(lazy[r], x);
        }
        for (int l = a >> 1, r = b >> 1; l > 0; l >>= 1, r >>= 1) {
            data[l] = fOperate(act(2 * l + 0), act(2 * l + 1));
            data[r] = fOperate(act(2 * r + 0), act(2 * r + 1));
        }
    }

    // [l, r)の区間クエリ
    inline T query(int a, int b){
        a += n, b += n - 1;
        for (int i = height; i > 0; i--) propagate(a >> i), propagate(b >> i);
        T Lret = dataId, Rret = dataId;
        for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1){
            if (l & 1) Lret = fOperate(Lret, act(l++));
            if (r & 1) Rret = fOperate(act(--r), Rret);
        }
        return fOperate(Lret, Rret);
    }

    inline void build(vector<T> vec){
        for (int i = 0; i < vec.size(); i++) data[i + n] = vec[i];
        for (int i = n - 1; i > 0; i--) data[i] = fOperate(data[2 * i + 0], data[2 * i + 1]);
    }

    // 添字でアクセス
    inline T& operator[](int i) {
        return data[i + n];
    }
};

struct Data {
    ll cnt, zero, one;
};
Data dataid = {0, 0, 0};

struct Lazy{
    ll val;
    constexpr bool operator == (const Lazy& a) const noexcept {
        return val == a.val;
    }
};
Lazy lazyid = {0};

Data fop(Data a, Data b){
    return {a.cnt + b.cnt + a.one * b.zero, a.zero + b.zero, a.one + b.one};
}
Data fac(Data a, Lazy f){
    if(f.val){
        return {a.one * a.zero - a.cnt, a.one, a.zero};
    }else{
        return a;
    }
}
Lazy fme(Lazy f, Lazy g){
    return {g.val ^ f.val};
}

// LazySegTree<Data, Lazy, fop, fac, fme> seg(N, dataid, lazyid);
// <data型, lazy型, monoidｘmonoid, monoidｘop, opｘop> (個数, data単位元, lazy単位元)

// ACL template
int main() {
    ll N, Q;
    cin >> N >> Q;
    vc<Data> A(N);
    rep(i, N){
        ll a;
        cin >> a;
        if(a) A[i] = {0, 0, 1};
        else A[i] = {0, 1, 0};  
    }
    LazySegTree<Data, Lazy, fop, fac, fme> seg(N, dataid, lazyid);
    seg.build(A);

    rep(_, Q){
        ll t, l, r;
        cin >> t >> l >> r, l--;

        if(t == 1){
            seg.update(l, r, {1});
        }else{
            cout << seg.query(l, r).cnt << '\n';
        }
    }
}

Measured memory_bytes: 29044