#include <bits/stdc++.h>

#include <atcoder/all>

#define DEF_LAZYSEGTREE(name, T, op, e, F, mapping, composition)                             \
  namespace name {                                                                           \
  struct _T {                                                                                \
    T value;                                                                                 \
    int left, right, size;                                                                   \
    inline _T(const T& value, int left, int right, int size)                                 \
        : value(value), left(left), right(right), size(size) {}                              \
    inline _T(const T& value, int left = 0, int right = 1)                                   \
        : _T(value, left, right, right - left) {}                                            \
    inline _T() : _T((e), -1, -1, 0) {}                                                      \
  };                                                                                         \
  inline _T _op(_T _a, _T _b) {                                                              \
    const auto &a = _a.value, &b = _b.value;                                                 \
    int newLeft = (_a.size > 0 ? _a : _b).left;                                              \
    int newRight = (_b.size > 0 ? _b : _a).right;                                            \
    int newSize = _a.size + _b.size;                                                         \
    return _T((op), newLeft, newRight, newSize);                                             \
  }                                                                                          \
  inline _T _e() { return _T(); }                                                            \
  struct _F {                                                                                \
    F value;                                                                                 \
    bool enabled;                                                                            \
    inline _F(const F& value, bool enabled = true)                                           \
        : value(value), enabled(enabled) {}                                                  \
    inline _F() : _F(F(), false) {}                                                          \
  };                                                                                         \
  inline _T _mapping(_F _f, _T _a) {                                                         \
    if (!_f.enabled) return _a;                                                              \
    const auto& f = _f.value;                                                                \
    const auto& a = _a.value;                                                                \
    int l = _a.left, r = _a.right, s = _a.size;                                              \
    return _T((mapping), _a.left, _a.right, _a.size);                                        \
  }                                                                                          \
  inline _F _composition(_F _f, _F _g) {                                                     \
    if (!_f.enabled) return _g;                                                              \
    if (!_g.enabled) return _f;                                                              \
    const auto &f = _f.value, &g = _g.value;                                                 \
    return _F((composition), true);                                                          \
  }                                                                                          \
  inline _F _id() { return _F(); }                                                           \
  using type = typename atcoder::lazy_segtree<_T, _op, _e, _F, _mapping, _composition, _id>; \
  inline type generate(int n) {                                                              \
    std::vector<_T> _v(n);                                                                   \
    for (int i = 0; i < n; ++i) _v[i] = _T(e, i, i + 1, 1);                                  \
    return type(std::move(_v));                                                              \
  }                                                                                          \
  inline type generate(const std::vector<T>& v) {                                            \
    const int n = v.size();                                                                  \
    std::vector<_T> _v(n);                                                                   \
    for (int i = 0; i < n; ++i) _v[i] = _T(v[i], i, i + 1, 1);                               \
    return type(std::move(_v));                                                              \
  }                                                                                          \
  }

// #LIB#

using namespace std;
using namespace atcoder;
using ll = long long;
#define double long double
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define repr(i, a, b) for (auto i = (a); i < (b); ++i)
#define itr(x, c) for (auto&& x : (c))
#define updatemax(t, v) ((t) = std::max((t), (v)))
#define updatemin(t, v) ((t) = std::min((t), (v)))
#ifdef ONLINE_JUDGE
#define endl _endl
const char _endl = (cin.tie(0), cout.tie(0), ios::sync_with_stdio(0), cout.precision(16), '\n');
#endif

struct T {
  ll zero, one, conv;
};

DEF_LAZYSEGTREE(seg,
                T,
                (T{a.zero + b.zero, a.one + b.one, a.conv + b.conv + a.one* b.zero}),
                (T{0, 0, 0}),
                bool,
                f ? (T{a.one, a.zero, a.zero* a.one - a.conv}) : a,
                f ^ g)

int main() {
  ll n, q;
  cin >> n >> q;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  vector<ll> t(q), l(q), r(q);
  rep(i, q) cin >> t[i] >> l[i] >> r[i];
  vector<T> a_(n);
  rep(i, n) a_[i] = {a[i] == 0, a[i] == 1, 0};
  auto st = seg::generate(a_);
  rep(i, q) {
    if (t[i] == 1) {
      st.apply(l[i] - 1, r[i], true);
    } else {
      cout << st.prod(l[i] - 1, r[i]).value.conv << endl;
    }
  }
}


Measured memory_bytes: 43068