#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#include <array>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define all(v) v.begin(),v.end()
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;
template<class T> void chmin(T &a,const T &b){if(a>b) a=b;}
template<class T> void chmax(T &a,const T &b){if(a<b) a=b;}

#include <functional>

template< typename T, typename S >
struct LazySegmentTree{
    int n;
    vector<T> data;
    vector<S> lazy;
    T te;
    S se;

    inline void merge_functions(S& lazy,S& val){
        //lazy+=val;
        //lazy=val;
        lazy=1-lazy;
    }
    inline void operate(T& data,S& val,int len){
        //data+=val*len;
        //data=val*len;
        //data+=val;
        //data=val;
        if(val==0) return;

        ll n=len;
        ll zero=data[0];ll one=data[1];ll rev=data[2];
        data[0]=one;
        data[1]=zero;
        data[2]=n*(n-1)/2-one*(one-1)/2-zero*(zero-1)/2-rev;
    }
    inline T merge_values(T& x,T& y){
        //return x+y;
        //return min(x,y);
        //return max(x,y);
        array<ll,3> a;
        a[0]=x[0]+y[0];
        a[1]=x[1]+y[1];
        a[2]=x[2]+y[2]+x[1]*y[0];
        return a;
    }

    LazySegmentTree(){}
    LazySegmentTree(int sz,T te,S se):te(te),se(se){
        n=1;
        while(n<sz) n*=2;
        data.resize(2*n-1,te);
        lazy.resize(2*n-1,se);
    }

    void build(const vector<T> &A){
        for(int k=0;k<int(A.size());k++) data[k+n-1]=A[k];
        for(int k=n-2;k>=0;k--) data[k]=merge_values(data[2*k+1],data[2*k+2]);
    }

    void eval(int k,int l,int r){
        if(lazy[k]==se) return;
        operate(data[k],lazy[k],r-l);
        if(r-l>1){
            merge_functions(lazy[2*k+1],lazy[k]);
            merge_functions(lazy[2*k+2],lazy[k]);
        }
        lazy[k]=se;
    }

    void update(int a,int b,S val,int k=0,int l=0,int r=-1){
        if(r<0) r=n;
        eval(k,l,r);
        if(b<=l||r<=a) return;
        if(a<=l&&r<=b){
            merge_functions(lazy[k],val);
            eval(k,l,r);
        }else{
            update(a,b,val,2*k+1,l,(l+r)/2);
            update(a,b,val,2*k+2,(l+r)/2,r);
            data[k]=merge_values(data[2*k+1],data[2*k+2]);
        }
    }

    T query(int a,int b,int k=0,int l=0,int r=-1){
        if(r<0) r=n;
        eval(k,l,r);
        if(b<=l||r<=a) return te;
        if(a<=l&&r<=b) return data[k];

        T vl=query(a,b,2*k+1,l,(l+r)/2);
        T vr=query(a,b,2*k+2,(l+r)/2,r);
        return merge_values(vl,vr);
    }

    T get(int p){
        return query(p,p+1);
    }
};

/*
  LazySegmentTree<ll,ll> seg(N,te,se);
  vector<ll> init(N,0);
  seg.build(init);
*/

typedef array<ll,3> AR;
AR E;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

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

  rep(i,3) E[i]=0;
  LazySegmentTree<AR,int> seg(N,E,0);
  {
      vector<AR> init(N);
      rep(i,N){
          init[i][A[i]]=1;
          init[i][1-A[i]]=0;
          init[i][2]=0;
      }
      seg.build(init);
  }

  rep(q,Q){
      int T;cin>>T;
      if(T==1){
          int L,R;
          cin>>L>>R;
          L--;R--;
          seg.update(L,R+1,1);
      }else{
          int L,R;
          cin>>L>>R;
          L--;R--;
          auto res=seg.query(L,R+1);
          cout<<res[2]<<"\n";
      }
  }

  return 0;
}


Measured memory_bytes: 22992