#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int len[N],a[N];
int n,m,ql,qr;
struct Seg{
int sum0,sum1;
int lmax0,rmax0,lmax1,rmax1;
int segmax0,segmax1;
int tag1=-1,tag2;
}s[4*N];
Seg merge(Seg L,Seg R){
Seg T;
T.sum0=L.sum0+R.sum0;
T.sum1=L.sum1+R.sum1;
T.lmax0=L.lmax0;
if(L.sum1==0)
T.lmax0=L.sum0+R.lmax0;
T.rmax0=R.rmax0;
if(R.sum1==0)
T.rmax0=R.sum0+L.rmax0;
T.lmax1=L.lmax1;
if(L.sum0==0)
T.lmax1=L.sum1+R.lmax1;
T.rmax1=R.rmax1;
if(R.sum0==0)
T.rmax1=R.sum1+L.rmax1;
T.segmax0=max({L.segmax0,R.segmax0,L.rmax0+R.lmax0});
T.segmax1=max({L.segmax1,R.segmax1,L.rmax1+R.lmax1});
return T;
}
void build(int p,int l,int r){
len[p]=r-l+1;
if(l==r){
s[p].sum0=s[p].lmax0=s[p].rmax0=s[p].segmax0=a[l]^1;
s[p].sum1=s[p].lmax1=s[p].rmax1=s[p].segmax1=a[l];
return;
}
int mid=l+r>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
s[p]=merge(s[p<<1],s[p<<1|1]);
}
void P(int p,int op){
if(op==0){
s[p].tag1=0;
s[p].tag2=0;
s[p].sum0=s[p].lmax0=s[p].rmax0=s[p].segmax0=len[p];
s[p].sum1=s[p].lmax1=s[p].rmax1=s[p].segmax1=0;
}
else if(op==1){
s[p].tag1=1;
s[p].tag2=0;
s[p].sum0=s[p].lmax0=s[p].rmax0=s[p].segmax0=0;
s[p].sum1=s[p].lmax1=s[p].rmax1=s[p].segmax1=len[p];
}
else{
s[p].tag2^=1;
Seg t=s[p];
swap(t.sum0,t.sum1);swap(t.lmax0,t.lmax1);swap(t.rmax0,t.rmax1);
swap(t.segmax0,t.segmax1);
s[p]=t;
}
}
void pushdown(int p){
if(s[p].tag1!=-1)
P(p<<1,s[p].tag1),P(p<<1|1,s[p].tag1);
if(s[p].tag2)
P(p<<1,2),P(p<<1|1,2);
s[p].tag1=-1;
s[p].tag2=0;
}
void RangeMoify(int p,int l,int r,int op){
if(l>=ql&&r<=qr){
P(p,op);
return;
}
pushdown(p);
int mid=l+r>>1;
if(ql<=mid)
RangeMoify(p<<1,l,mid,op);
if(qr>mid)
RangeMoify(p<<1|1,mid+1,r,op);
s[p]=merge(s[p<<1],s[p<<1|1]);
}
Seg query(int p,int l,int r){
if(l>=ql&&r<=qr)
return s[p];
pushdown(p);
int mid=l+r>>1;
if(qr<=mid)
return query(p<<1,l,mid);
if(ql>mid)
return query(p<<1|1,mid+1,r);
Seg ans=merge(query(p<<1,l,mid),query(p<<1|1,mid+1,r));
return ans;
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
cin>>a[i];
build(1,1,n);
while(m--){
int op;
cin>>op>>ql>>qr;
++ql;++qr;
if(op<3)
RangeMoify(1,1,n,op);
else if(op==3)
cout<<query(1,1,n).sum1<<"\n";
else
cout<<query(1,1,n).segmax1<<"\n";
}
return 0;
}