struct N{ ll aa,ab,ac,ad,bb,bc,bd,cc,cd,dd; }; N segtree_ph_func(N a,N b){ N r; r.aa=a.aa+b.aa; r.ab=min(a.aa+b.ab,a.ab+b.bb); r.ac=min(a.aa+b.ac,a.ab+b.bc,a.ac+b.cc); r.ad=min(a.aa+b.ad,a.ab+b.bd,a.ac+b.cd,a.ad+b.dd); r.bb=a.bb+b.bb; r.bc=min(a.bb+b.bc,a.bc+b.cc); r.bd=min(a.bb+b.bd,a.bc+b.cd,a.bd+b.dd); r.cc=a.cc+b.cc; r.cd=min(a.cc+b.cd,a.cd+b.dd); r.dd=a.dd+b.dd; return r; } N leaf[]={ {0,0,0,0,1,1,1,1,1,1}, {1,0,0,0,0,0,0,1,1,1}, {1,1,0,0,1,0,0,0,0,1}, {1,1,1,0,1,1,0,1,0,0}, }; { ll@n,@q; segtree_pht; t.malloc(n); t.setN(n); rep(i,n){ char@c; t[i]=leaf[c-'A']; } t.build(); rep(q){ ll@a; if(a==1){ ll@x--; char@c; t.change(x,leaf[c-'A']); } if(a==2){ ll@l--,@r; wt(t.get(l,r).ad); } } }