#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "cp/debug.h"
#else
#define debug(...)
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MOD = (int) 1e9 + 7;
const int INF = 0x3f3f3f3f;
struct ODT {
map<int, int> tree;
using It = map<int, int>::iterator;
It split(int x) {
It it = tree.upper_bound(x);
assert(it != tree.begin());
it--;
if (it->first == x) return it;
return tree.emplace(x, it->second).first;
}
pair<It, It> split(int l, int r) {
return {split(l), split(r + 1)};
}
void assign(int l, int r, int v) {
auto [it_l, it_r] = split(l, r);
while (it_l != it_r) {
tree.erase(it_l++);
}
tree[l] = v;
}
void reverse(int l, int r) {
auto [it_l, it_r] = split(l, r);
while (it_l != it_r) {
it_l->second = !it_l->second;
it_l++;
}
}
int count_one(int l, int r) {
auto [it_l, it_r] = split(l, r);
int ones = 0;
while (it_l != it_r) {
It prev = it_l++;
ones += prev->second * (it_l->first - prev->first);
}
return ones;
}
int longest_sequence(int l, int r) {
auto [it_l, it_r] = split(l, r);
int res = 0, cur = 0;
while (it_l != it_r) {
It prev = it_l++;
if (prev->second == 0) cur = 0;
else cur += it_l->first - prev->first;
res = max(res, cur);
}
return res;
}
};
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<int> arr(n);
ODT odt_tree;
for (int i = 0; i < n; ++i) {
cin >> arr[i];
odt_tree.tree[i] = arr[i];
}
odt_tree.tree[n] = -1;
for (int w = 0; w < q; ++w) {
int type, l, r;
cin >> type >> l >> r;
if (type == 0 || type == 1) {
odt_tree.assign(l, r, type);
}
else if (type == 2) {
odt_tree.reverse(l, r);
}
else if (type == 3) {
cout << odt_tree.count_one(l, r) << '\n';
} else if (type == 4) {
cout << odt_tree.longest_sequence(l, r) << '\n';
} else {
assert(false);
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgojaWZkZWYgTE9DQUwKI2luY2x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