#include <bits/stdc++.h>

using namespace std;

#define print(a) cerr << "[" << #a << " = " << a << "] "
#define dbg(a) print(a) << "\n"
#define dbg2(a, b) print(a); print(b) << "\n"
#define dbg3(a, b, c) print(a); print(b); print(c) << "\n"

#define int long long
#define ll long long
#define pi pair<int, int>
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) ((int)(a).size())
#define divup(a, b) (((a) + (b) - 1) / (b))

template <class T> bool chmax(T&a, const T&b) {return b > a ? a = b, 1 : 0;};
template <class T> bool chmin(T&a, const T&b) {return b < a ? a = b, 1 : 0;};

const int mod = 1e9 + 7;
const int inf = (int)(1e18) + 7;

const int maxn = 2e5 + 7;
const int N = 1e4 + 7;
const int logn = 20;

void solve() {
    int n, q;
    cin >> n >> q;

    vector <int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        dbg(a[i]);
    }

    for (int qi = 0; qi < q; ++qi) {
        int t;
        cin >> t;
        dbg(t);
        if (t == 1) {
            int i;
            cin >> i;
            --i;
            a[i] = -a[i];
        } else {
            int l, r;
            cin >> l >> r;
            dbg2(l, r);
            --l; --r;
            vector <int> p(n, 0), s(n, 0);
            p[l] = (a[l]==1);
            s[r] = (a[r]==1);
            int ans = 0;
            for (int i = l; i <= r; ++i) {
                ans += (a[i]==1);
            }
            for (int i = l + 1; i <= r; ++i) {
                p[i] = p[i - 1] + (a[i] == 1);
            }
            for (int i = r - 1; i >= l; --i) {
                s[i] = s[i + 1] + (a[i] == 1);
            }
            int cur_p = 0, mn = s[l];
            for (int i = l; i <= r; ++i) {
                if (a[i] == -1) {
                    chmin(mn, s[i]);
                    if (cur_p > 0 && mn > 0) {
                        mn--;
                        ++ans;
                    }
                } else cur_p++;
            }
            cout << ans << '\n';
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt", "w", stderr);

    solve();
} 