n=int(input()) a=list(map(int,input().split())) B=317 st1=[(0,0,0,0)]*B*B st2=[(0,0,0,0)]*B lt=[0]*B def op(x,y): x1,x2,x3,x4=x y1,y2,y3,y4=y z1=x1+y1 z2=max(x1,x2)+y2 z3=max(x1,x2,x3)+y3 z4=max(x1,x2,x3,x4)+y4 z=(z1,z2,z3,z4) return z def mp(f,x): match f: case 0: return x case 1: return (1,0,0,0) case 2: return (0,1,0,0) case 3: return (0,0,1,0) case 4: return (0,0,0,1) return def cp(f,g): return g for i in range(n): st1[i]=mp(a[i],st1[i]) for i in range(B): for j in range(i*B,i*B+B): st2[i]=op(st2[i],st1[j]) Q=int(input()) for _ in range(Q): query=list(map(int,input().split())) t=query[0] if t==1: _,l,r=query l-=1 r-=1 yl=l//B yr=r//B for i in range(yl*B,yl*B+B): st1[i]=mp(lt[yl],st1[i]) lt[yl]=0 for i in range(yr*B,yr*B+B): st1[i]=mp(lt[yr],st1[i]) lt[yr]=0 a=(0,0,0,0) if yl==yr: for i in range(l,r+1): a=op(a,st1[i]) else: for i in range(l,yl*B+B): a=op(a,st1[i]) for i in range(yl+1,yr): a=op(a,st2[i]) for i in range(yr*B,r+1): a=op(a,st1[i]) a1,a2,a3,a4=a print(max(a1,a2,a3,a4)) else: _,l,r,x=query l-=1 r-=1 yl=l//B yr=r//B for i in range(yl*B,yl*B+B): st1[i]=mp(lt[yl],st1[i]) lt[yl]=0 for i in range(yr*B,yr*B+B): st1[i]=mp(lt[yr],st1[i]) lt[yr]=0 if yl==yr: for i in range(l,r+1): st1[i]=mp(x,st1[i]) for i in range(yl*B,yl*B+B): st2[yl]=op(st2[yl],st1[i]) else: for i in range(l,yl*B+B): st1[i]=mp(lt[yl],st1[i]) for i in range(yl*B,yl*B+B): st2[yl]=op(st2[yl],st1[i]) for i in range(yl+1,yr): match x: case 1: st2[i]=(B,0,0,0) case 2: st2[i]=(0,B,0,0) case 3: st2[i]=(0,0,B,0) case 4: st2[i]=(0,0,0,B) lt[i]=x for i in range(yr*B,r+1): st1[i]=mp(lt[yr],st1[i]) for i in range(yr*B,yr*B+B): st2[yr]=op(st2[yr],st1[i])