#include<bits/stdc++.h>
//#include<atcoder/all>

using namespace std;
//using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
#define rep(i,n) for(ll i = 0;i < (ll)n;i++)
#define ALL(x) (x).begin(),(x).end()
#define MOD 1000000007

template<typename T,typename E>
class LazySegtree{
  public:
  using F = function<T(T,T)>;
  using G = function<T(T,E)>;
  using H = function<E(E,E)>;
  int n,height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  LazySegtree(F f,G g,H h,T ti,E ei) : f(f),g(g),h(h),ti(ti),ei(ei){}
  void init(int N){
    n = 1;height = 0;
    while(n < N)n <<= 1,height++;
    dat.assign(n << 1,ti);
    laz.assign(n << 1,ei);
  }
  void build(const vector<T> &v){
    int N = v.size();
    init(N);
    rep(i,N)dat[i+n] = v[i];
    for(int i = n-1;i > 0;i--)dat[i] = f(dat[(i << 1)|0],dat[(i << 1)|1]);
  }
  inline void eval(int k){
    if(laz[k] == ei)return ;
    laz[(k<<1)|0] = h(laz[(k << 1)|0],laz[k]);
    laz[(k<<1)|1] = h(laz[(k << 1)|1],laz[k]);
    dat[k] = reflect(k);
    laz[k] = ei;
  }
  inline void thrust(int k){
    for(int i = height;i > 0;i--)eval(k>>i);
  }
  inline T reflect(int k){
    return laz[k] == ei ? dat[k] : g(dat[k],laz[k]);
  }
  inline void recalc(int k){
    while(k >>= 1)dat[k] = f(reflect((k<<1)|0),reflect((k<<1)|1));
  }
  void update(int a,int b,E x){
    thrust(a+=n);
    thrust(b+=n-1);
    for(int l = a,r = b+1;l < r;l >>= 1,r >>= 1){
      if(l & 1)laz[l] = h(laz[l],x),l++;
      if(r & 1)r--,laz[r] = h(laz[r],x);
    }
    recalc(a);
    recalc(b);
  }
  void set_val(int k,T x){
    thrust(k+=n);
    dat[k] = x;laz[k] = ei;
    recalc(k);
  }
  T get(int a,int b){
    thrust(a+=n);
    thrust(b+=n-1);
    T vl = ti,vr = ti;
    for(int l = a,r = b+1;l < r;l >>= 1,r >>= 1){
      if(l & 1)vl = f(vl,reflect(l++));
      if(r & 1)vr = f(reflect(--r),vr);
    }
    return f(vl,vr);
  }
};

struct info{
  ll cnt,bit,len;
};

int main(){
  
  int n,Q;
  cin >> n >> Q;
  vector<info> v(n,info{0,0,1});
  rep(i,n)cin >> v[i].bit;
  auto f = [](info a,info b){
    ll cnt = a.cnt+b.cnt;
    cnt += (b.len-b.bit)*a.bit;
    return info{cnt,a.bit+b.bit,a.len+b.len};
  };
  auto g = [](info a,bool b){
    if(b){
      a.bit = a.len-a.bit;
      a.cnt = (a.len-a.bit)*a.bit-a.cnt;
    }
    return a;
  };
  LazySegtree<info,bool> seg(f,g,
    [](bool a,bool b){return ((a && b) ? false : (a || b));},
    info{0,0,0},false
    );
  seg.build(v);
  while(Q--){
    int t,l,r;cin >> t >> l >> r;
    if(t == 1){
      seg.update(--l,r,true);
    }else{
      cout << seg.get(--l,r).cnt << "\n";
    }
  }


  return 0;
}