#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fr(i,j,k) for(int i=j;i<k;i++)
#define f(n) fr(i,0,n)
#define f1(n) fr(i,1,n+1)
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
const ll mod = 998244353;
const int maxn = 2e5+5;
ll c[maxn<<2][2];
ll inv[maxn<<2];
ll tg[maxn<<2];
void pull(int x) {
    inv[x] = inv[x<<1] + inv[x<<1|1] + c[x<<1][1] * c[x<<1|1][0];
    c[x][0] = c[x<<1][0] + c[x<<1|1][0];
    c[x][1] = c[x<<1][1] + c[x<<1|1][1];
}
pair<pair<int,int>,ll> pull(pair<pair<int,int>,ll>x,pair<pair<int,int>,ll>y) {
    return {{x.F.F+y.F.F,x.F.S+y.F.S},x.S+y.S + (ll)x.F.S * y.F.F};
}

void push(int x,int l,int r,int mid) {
    if (!tg[x]) {
        return;
    }
    swap(c[x<<1][0],c[x<<1][1]);
    tg[x<<1] ^= 1;
    inv[x<<1] = c[x<<1][0] * c[x<<1][1] - inv[x<<1];
    swap(c[x<<1|1][0],c[x<<1|1][1]);
    tg[x<<1|1] ^= 1;
    inv[x<<1|1] = c[x<<1|1][0] * c[x<<1|1][1] - inv[x<<1|1];
    tg[x] = 0;
}
void update(int x,int l,int r,int ql,int qr) {
    if (ql <= l && qr >= r) {
        swap(c[x][0],c[x][1]);
        tg[x] ^= 1;
        inv[x] = (ll)(c[x][0]) * (c[x][1])- inv[x];
        return;
    }
    int mid = (l + r) >> 1;
    push(x,l,r,mid);
    if (ql <= mid) {
        update(x<<1,l,mid,ql,qr);
    }
    if (qr > mid) {
        update(x<<1|1,mid+1,r,ql,qr);
    }
    pull(x);
}
pair<pair<int,int>,ll> query(int x,int l,int r,int ql,int qr) {
    if (ql <= l && qr >= r) {
        return {{c[x][0],c[x][1]},inv[x]};
    }
    int mid = (l + r) >> 1;
    push(x,l,r,mid);
    if (qr <= mid) {
        return query(x<<1,l,mid,ql,qr);
    }
    if (ql > mid) {
        return query(x<<1|1,mid+1,r,ql,qr);
    }
    return pull(query(x<<1,l,mid,ql,qr),query(x<<1|1,mid+1,r,ql,qr));
}
void build(int x,int l,int r) {
    if (l == r) {
        int val;
        cin >> val;
        c[x][val]++;
        return;
    }
    int mid = (l + r) >> 1;
    build(x<<1,l,mid);
    build(x<<1|1,mid+1,r);
    pull(x);
}
void solve() {
    int n, q;
    cin >> n >> q;
    build(1,1,n);
    while (q--) {
        int op,l,r;
        cin >> op >> l >> r;
        if (op == 1) {
            update(1,1,n,l,r);
        }
        else {
            cout << query(1,1,n,l,r).S << '\n';
        }
    }
}
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int c = 0;
    int t;
    if (!c) {
        t = 1;
    }
    else {
        cin >> t;
    }
    while (t--) {
        solve();
    }
    
}
