//
#include
#include
#include
#include
using namespace std;
const int maxn=2000000;
struct Node
{
int left,right;//左右区间
int cover;//是否需要翻转
int bcval,blval,brval;//black 当前最大区间,左面最大区间,右面最大区间
int wcval,wlval,wrval;//white 当前最大区间,左面最大区间,右面最大区间
};
int n;
Node tree[maxn];
int a[maxn];//初始状态 1black 0white
int getLen(int id)
{
return tree[id].right-tree[id].left+1;
}
void update(int id)
{
tree[id].bcval=max(tree[id<<1].bcval,tree[(id<<1)|1].bcval);
tree[id].bcval=max(tree[id].bcval,tree[id<<1].brval+tree[(id<<1)|1].blval);
tree[id].blval=tree[id<<1].blval;
if(tree[id<<1].blval==getLen(id<<1))
tree[id].blval=tree[id<<1].blval+tree[(id<<1)|1].blval;
tree[id].brval=tree[(id<<1)|1].brval;
if(tree[(id<<1)|1].brval==getLen((id<<1)|1))
tree[id].brval=tree[(id<<1)|1].brval+tree[id<<1].brval;
tree[id].wcval=max(tree[id<<1].wcval,tree[(id<<1)|1].wcval);
tree[id].wcval=max(tree[id].wcval,tree[id<<1].wrval+tree[(id<<1)|1].wlval);
tree[id].wlval=tree[id<<1].wlval;
if(tree[id<<1].wlval==getLen(id<<1))
tree[id].wlval=tree[id<<1].wlval+tree[(id<<1)|1].wlval;
tree[id].wrval=tree[(id<<1)|1].wrval;
if(tree[(id<<1)|1].wrval==getLen((id<<1)|1))
tree[id].wrval=tree[(id<<1)|1].wrval+tree[id<<1].wrval;
}
void buildtree(int id,int l,int r)
{
tree[id].left=l,tree[id].right=r;
tree[id].cover=0;
if(l==r)
{
if(a[l])
{
tree[id].bcval=tree[id].blval=tree[id].brval=1;
tree[id].wcval=tree[id].wlval=tree[id].wrval=0;
}
else
{
tree[id].bcval=tree[id].blval=tree[id].brval=0;
tree[id].wcval=tree[id].wlval=tree[id].wrval=1;
}
return ;
}
int mid=(l+r)>>1;
buildtree(id<<1,l,mid);
buildtree((id<<1)|1,mid+1,r);
update(id);
}
void push(int id)
{
if(tree[id].cover)
{
tree[id].cover=!tree[id].cover;
tree[id<<1].cover=!tree[id<<1].cover;
tree[(id<<1)|1].cover=!tree[(id<<1)|1].cover;
swap(tree[id].bcval,tree[id].wcval);
swap(tree[id].blval,tree[id].wlval);
swap(tree[id].brval,tree[id].wrval);
}
}
void insert(int id,int l,int r)
{
if(tree[id].left==l&&tree[id].right==r)
{
//important
tree[id].cover=!tree[id].cover;
push(id);
return ;
}
push(id);
int mid=(tree[id].left+tree[id].right)>>1;
if(r<=mid) insert(id<<1,l,r);
else if(l>=mid+1) insert((id<<1)|1,l,r);
else
{
insert(id<<1,l,mid);
insert((id<<1)|1,mid+1,r);
}
push(id<<1);
push((id<<1)|1);
update(id);
}
void fresh(Node &cs,Node ls,Node rs)
{
cs.bcval=max(ls.bcval,rs.bcval);
cs.bcval=max(cs.bcval,ls.brval+rs.blval);
cs.blval=ls.blval;
if(ls.blval==ls.right-ls.left+1)
cs.blval=ls.blval+rs.blval;
cs.brval=rs.brval;
if(rs.brval==rs.right-rs.left+1)
cs.brval=rs.brval+ls.brval;
cs.wcval=max(ls.wcval,rs.wcval);
cs.wcval=max(cs.wcval,ls.wrval+rs.wlval);
cs.wlval=ls.wlval;
if(ls.wlval==ls.right-ls.left+1)
cs.wlval=ls.wlval+rs.wlval;
cs.wrval=rs.wrval;
if(rs.wrval==rs.right-rs.left+1)
cs.wrval=rs.wrval+ls.wrval;
}
Node query(int id,int l,int r)
{
if(tree[id].left==l&&tree[id].right==r)
{
push(id);
return tree[id];
}
push(id);
int mid=(tree[id].left+tree[id].right)>>1;
if(r<=mid) return query(id<<1,l,r);
else if(l>=mid+1) return query((id<<1)|1,l,r);
else
{
Node cs,ls,rs;
ls=query(id<<1,l,mid);
rs=query((id<<1)|1,mid+1,r);
fresh(cs,ls,rs);
return cs;
}
}
int main()
{
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
buildtree(1,1,n);
int ci;scanf("%d",&ci);
while(ci--)
{
int order,x,y;scanf("%d%d%d",&order,&x,&y);
if(order==1)
{
insert(1,x,y);
}
else
{
Node cnt=query(1,x,y);
printf("%d\n",cnt.bcval);
}
}
}
return 0;
}