#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
using ll = long long;

struct TreeNode {
    int max_pref{};
    int min_pref{};
    int max_suf{};
    int min_suf{};
    int sum{};
    int len{};
    int pref_minus1{};
    int suf_minus1{};

    static TreeNode merge(const TreeNode& a, const TreeNode& b) {
        if (a.len == 0) {
            return {
                b.max_pref,
                b.min_pref,
                b.max_suf,
                b.min_suf,
                b.sum,
                b.len,
                b.pref_minus1 + a.pref_minus1,
                b.suf_minus1 + (b.len == 0 ? a.pref_minus1 : 0),
            };
        } else if (b.len == 0) {
            return {
                a.max_pref,
                a.min_pref,
                a.max_suf,
                a.min_suf,
                a.sum,
                a.len,
                a.pref_minus1 + (a.len == 0 ? b.pref_minus1 : 0),
                a.suf_minus1 + b.pref_minus1,
            };
        };

        int added_minus1 = min({ a.suf_minus1 + b.pref_minus1, a.sum + b.min_pref, a.min_pref, b.sum + a.min_suf, b.min_suf });

        return {
            max(a.max_pref, b.max_pref + a.sum - added_minus1),
            min({ a.min_pref, b.min_pref + a.sum - added_minus1, a.sum - added_minus1 }),
            max(b.max_suf, a.max_suf + b.sum - added_minus1),
            min({ b.min_suf, a.min_suf + b.sum - added_minus1, b.sum - added_minus1 }),
            a.sum + b.sum - added_minus1,
            a.len + b.len + added_minus1,
            a.pref_minus1,
            b.suf_minus1,
        };
    }
};

struct SegmentTree {
    int size;
    vector<TreeNode> tree{};

    SegmentTree(int size) : size(size) {
        tree.resize(size * 4);
    }

    void build(const vector<int>& initial_data) {
        do_build(0, 0, size, initial_data);
    }

    void do_build(int node, int low, int high, const vector<int>& initial_data) {
        if (low == high - 1) {
            int val = initial_data[low];

            if (val == 1) {
                tree[node] = TreeNode { 1, 1, 1, 1, 1, 1, 0, 0 };
            } else {
                tree[node] = TreeNode { 0, 0, 0, 0, 0, 0, 1, 1 };
            }

            return;
        }

        int middle = (low + high) / 2;

        do_build(node * 2 + 1, low, middle, initial_data);
        do_build(node * 2 + 2, middle, high, initial_data);

        tree[node] = TreeNode::merge(tree[node * 2 + 1], tree[node * 2 + 2]);
        // cout << low << " " << high << " " << tree[node].len << "\n";
    }

    int get(int low, int high) {
        return do_get(0, 0, size, low, high).len;
    }

    TreeNode do_get(int node, int low, int high, int q_low, int q_high) {
        if (q_low >= high || q_high <= low) {
            return TreeNode { 0, 0, 0, 0, 0, 0, 0, 0 };
        }

        if (q_low == low && q_high == high) {
            return tree[node];
        }

        int middle = (low + high) / 2;

        TreeNode l = do_get(node * 2 + 1, low, middle, q_low, min(q_high, middle));
        TreeNode r = do_get(node * 2 + 2, middle, high, max(q_low, middle), q_high);

        return TreeNode::merge(l, r);
    }

    void set(int index, int value) {
        do_set(0, 0, size, index, value);
    }

    void do_set(int node, int low, int high, int index, int val) {
        if (low == high - 1 && low == index) {
            if (val == 1) {
                tree[node] = TreeNode { 1, 1, 1, 1, 1, 1, 0, 0 };
            } else {
                tree[node] = TreeNode { 0, 0, 0, 0, 0, 0, 1, 1 };
            }

            return;
        }

        int middle = (low + high) / 2;

        if (index < middle) {
            do_set(node * 2 + 1, low, middle, index, val);
        } else {
            do_set(node * 2 + 2, middle, high, index, val);
        }

        tree[node] = TreeNode::merge(tree[node * 2 + 1], tree[node * 2 + 2]);
    }
};

int main() {
#ifndef DEBUG
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
#endif

    int n, q;
    cin >> n >> q;

    vector<int> arr(n);

    for (auto& it : arr) {
        cin >> it;
    }

    SegmentTree tree(n);
    tree.build(arr);

    for (int i = 0; i < q; i++) {
        int t;
        cin >> t;

        if (t == 1) {
            int p;
            cin >> p;

            p -= 1;

            arr[p] = -arr[p];
            tree.set(p, arr[p]);
        } else if (t == 2) {
            int l, r;
            cin >> l >> r;

            l -= 1;

            cout << tree.get(l, r) << "\n";
        }
    }

    return 0;
}
