#define N 1000005int c4[N*5],c7[N*5];int c47[N*5],c74[N*5];bool mark[N*5];char str[N];void up(int id){ c4[id] = c4[id<<1] + c4[id<<1|1]; c7[id] = c7[id<<1] + c7[id<<1|1]; c47[id] = max(c47[id<<1] + c7[id<<1|1] , c4[id<<1] + c47[id<<1|1]); c74[id] = max(c74[id<<1] + c4[id<<1|1] , c7[id<<1] + c74[id<<1|1]);}void build(int s,int t,int id){ if(s>t)return ; mark[id] = 0; if(s == t){ if(str[s] == '4'){ c4[id] = 1; c7[id] = 0; } else { c4[id] = 0; c7[id] = 1; } c47[id] = c74[id] = 1; return ; } int mid = (s + t)>>1; build(s,mid,id<<1); build(mid+1,t,id<<1|1); up(id);}void rev(int id){ mark[id] = !mark[id]; swap(c47[id],c74[id]); swap(c4[id],c7[id]);}void down(int id){ if(mark[id]){ mark[id] = 0; rev(id<<1); rev(id<<1|1); }}void sw(int s,int t,int id,int l,int r){ if(s<=l && r<=t){ rev(id); return ; } down(id); int mid = (l+r)>>1; if(s<=mid)sw(s,t,id<<1,l,mid); if(mid+1<=t)sw(s,t,id<<1|1,mid+1,r); up(id);}int main(){ int n,m; while(scanf("%d%d",&n,&m) != -1){ int i,j; scanf("%s",str+1); build(1,n,1); while(m--){ char op[10]; scanf("%s",op); if(op[0] == 'c'){ printf("%d\n",c47[1]); }else { int x,y; scanf("%d%d",&x,&y); sw(x,y,1,1,n); } } } return 0;}
http://codeforces.com/contest/145/problem/E