#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1000000007;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const double pi = acos(-1.0);
const double EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

template<typename Monoid, typename Operator_Monoid>
struct Lazy_Segment_Tree{
    vector<Monoid> seg;
    vector<Operator_Monoid> lazy;
    const Monoid e1;
    const Operator_Monoid e2;
    const int n;

    Monoid f(const Monoid &a, const Monoid &b) const{
        return {a.x+b.x, a.y+b.y, a.z1+b.z1+a.y*b.x, a.z2+b.z2+a.x*b.y};
    }

    Monoid g(const Monoid &a, const Operator_Monoid &b) const{
        if(b) return {a.y, a.x, a.z2, a.z1};
        return a;
    }

    Operator_Monoid h(const Operator_Monoid &a, const Operator_Monoid &b) const {return a^b;}
    
    Lazy_Segment_Tree(int N, const Monoid &e1, const Operator_Monoid &e2)
        : e1(e1), e2(e2), n(1<<(32-__builtin_clz(N-1))){
        seg.assign(2*n, e1), lazy.assign(2*n, e2);
    }

    void set(int i, const Monoid &x) {seg[n+i] = x;}

    void build(){
        rep3(i, n-1, 1) seg[i] = f(seg[2*i], seg[2*i+1]);
    }

    void init(int N, const Monoid &x){
        rep(i, N) set(i, x);
        build();
    }
    
    void eval(int i, int l, int r){
        if(lazy[i] != e2){
            //seg[i] = g(seg[i], lazy[i]*(r-l));
            seg[i] = g(seg[i], lazy[i]);
            if(r-l > 1){
                lazy[2*i] = h(lazy[2*i] ,lazy[i]);
                lazy[2*i+1] = h(lazy[2*i+1], lazy[i]);
            }
            lazy[i] = e2;
        }
    }
    
    void add(int a, int b, const Operator_Monoid &x, int i, int l, int r){
        eval(i, l, r);
        if(a >= r || b <= l) return;
        if(a <= l && r <= b){
            lazy[i] = h(lazy[i], x);
            eval(i, l, r);
        }
        else{
            add(a, b, x, 2*i, l, (l+r)/2);
            add(a, b, x, 2*i+1, (l+r)/2, r);
            seg[i] = f(seg[2*i], seg[2*i+1]);
        }
    }

    void add(int a, int b, const Operator_Monoid &x) {add(a, b, x, 1, 0, n);}
    
    Monoid query(int a, int b, int i, int l, int r){
        eval(i, l, r);
        if(a >= r || b <= l) return e1;
        if(a <= l && r <= b) return seg[i];
        Monoid vl = query(a, b, 2*i, l, (l+r)/2);
        Monoid vr = query(a, b, 2*i+1, (l+r)/2, r);
        return f(vl, vr);
    }

    Monoid query(int a, int b) {return query(a, b, 1, 0, n);}
    
    void update(int i, int l, int r){
        seg[i] = g(seg[i], lazy[i]);
        if(r-l > 1){
            lazy[2*i] = f(lazy[2*i], lazy[i]);
            lazy[2*i+1] = f(lazy[2*i+1], lazy[i]);
            update(2*i, l, (l+r)/2);
            update(2*i+1, (l+r)/2, r);
        }
        lazy[i] = e2;
    }

    void update() {update(1, 0, -1);}
    
    Monoid operator [] (int i) const {return seg[n+i];}
    
    void clear(){
        fill(all(seg), e1), fill(all(lazy), e2);
    }
};

struct Data{
    ll x, y, z1, z2;
    Data(ll x, ll y, ll z1, ll z2) : x(x), y(y), z1(z1), z2(z2) {}
};

int main(){
    int N, Q;
    cin >> N >> Q;
    Lazy_Segment_Tree<Data, int> seg(N, {0, 0, 0, 0}, 0);
    rep(i, N){
        int A; cin >> A;
        if(A) seg.set(i, {0, 1, 0, 0});
        else seg.set(i, {1, 0, 0, 0});
    }
    seg.build();
    while(Q--){
        int q, l, r; cin >> q >> l >> r; l--;
        if(q == 1) seg.add(l, r, 1);
        else cout << seg.query(l, r).z1 << endl;
    }
}

Measured memory_bytes: 21752