#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
  IOSetup() {
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

template <typename Monoid, typename OperatorMonoid = Monoid>
struct LazySegmentTree {
  using MM = function<Monoid(Monoid, Monoid)>;
  using MOI = function<Monoid(Monoid, OperatorMonoid, int)>;
  using OO = function<OperatorMonoid(OperatorMonoid, OperatorMonoid)>;

  LazySegmentTree(int sz, MM mm, MOI moi, OO oo, const Monoid M_UNITY, const OperatorMonoid O_UNITY)
    : mm(mm), moi(moi), oo(oo), M_UNITY(M_UNITY), O_UNITY(O_UNITY) {
    init(sz);
    dat.assign(n << 1, M_UNITY);
  }

  LazySegmentTree(const vector<Monoid> &a, MM mm, MOI moi, OO oo, const Monoid M_UNITY, const OperatorMonoid O_UNITY)
    : mm(mm), moi(moi), oo(oo), M_UNITY(M_UNITY), O_UNITY(O_UNITY) {
    int a_sz = a.size();
    init(a_sz);
    dat.resize(n << 1);
    REP(i, a_sz) dat[i + n] = a[i];
    for (int i = n - 1; i > 0; --i) dat[i] = mm(dat[i << 1], dat[(i << 1) + 1]);
  }

  void update(int a, int b, OperatorMonoid val) { update(a, b, val, 1, 0, n); }

  Monoid query(int a, int b) { return query(a, b, 1, 0, n); }

  Monoid operator[](const int idx) { return query(idx, idx + 1); }

private:
  int n = 1;
  MM mm;
  MOI moi;
  OO oo;
  const Monoid M_UNITY;
  const OperatorMonoid O_UNITY;
  vector<Monoid> dat;
  vector<OperatorMonoid> lazy;
  vector<bool> need_to_be_eval;

  void init(int sz) {
    while (n < sz) n <<= 1;
    lazy.assign(n << 1, O_UNITY);
    need_to_be_eval.assign(n << 1, false);
  }

  inline void evaluate(int node, int len) {
    if (need_to_be_eval[node]) {
      dat[node] = moi(dat[node], lazy[node], len);
      if (node < n) {
        lazy[node << 1] = oo(lazy[node << 1], lazy[node]);
        lazy[(node << 1) + 1] = oo(lazy[(node << 1) + 1], lazy[node]);
        need_to_be_eval[node << 1] = need_to_be_eval[(node << 1) + 1] = true;
      }
      lazy[node] = O_UNITY;
      need_to_be_eval[node] = false;
    }
  }

  void update(int a, int b, OperatorMonoid val, int node, int left, int right) {
    evaluate(node, right - left);
    if (right <= a || b <= left) return;
    if (a <= left && right <= b) {
      lazy[node] = oo(lazy[node], val);
      need_to_be_eval[node] = true;
      evaluate(node, right - left);
    } else {
      update(a, b, val, node << 1, left, (left + right) >> 1);
      update(a, b, val, (node << 1) + 1, (left + right) >> 1, right);
      dat[node] = mm(dat[node << 1], dat[(node << 1) + 1]);
    }
  }

  Monoid query(int a, int b, int node, int left, int right) {
    evaluate(node, right - left);
    if (right <= a || b <= left) return M_UNITY;
    if (a <= left && right <= b) return dat[node];
    return mm(query(a, b, node << 1, left, (left + right) >> 1), query(a, b, (node << 1) + 1, (left + right) >> 1, right));
  }
};

int main() {
  int n, q; cin >> n >> q;
  struct Node {
    vector<int> cnt;
    vector<ll> ans;
    Node() : cnt(2, 0), ans(2, 0) {}
    Node(const vector<int> &cnt, const vector<ll> &ans) : cnt(cnt), ans(ans) {}
  };
  function<Node(Node, Node)> mm = [](Node a, Node b) {
    vector cnt(2, 0);
    vector ans(2, 0LL);
    REP(i, 2) cnt[i] = a.cnt[i] + b.cnt[i];
    REP(i, 2) ans[i] = a.ans[i] + b.ans[i] + 1LL * a.cnt[i] * b.cnt[i ^ 1];
    return Node(cnt, ans);
  };
  function<Node(Node, int, int)> moi = [](Node node, int val, int) {
    if (val) {
      swap(node.cnt[0], node.cnt[1]);
      swap(node.ans[0], node.ans[1]);
    }
    return node;
  };
  function<int(int, int)> oo = [](int a, int b) { return a ^ b; };
  vector<Node> init(n);
  REP(i, n) {
    int a; cin >> a;
    init[i].cnt[a] = 1;
  }
  LazySegmentTree<Node, int> seg(init, mm, moi, oo, Node(), 0);
  while (q--) {
    int t, l, r; cin >> t >> l >> r; --l; --r;
    if (t == 1) {
      seg.update(l, r + 1, 1);
    } else if (t == 2) {
      cout << seg.query(l, r + 1).ans[1] << '\n';
    }
  }
  return 0;
}


Measured memory_bytes: 84520