【线段树延迟更新】Codeforces Round #104 (Div. 1) E
2012-02-13 19:27
288 查看
#define N 1000005 int c4[N*5],c7[N*5]; int c47[N*5],c74[N*5]; bool mark[N*5]; char str ; 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
相关文章推荐
- POJ3468 线段树(区间更新,区间求和,延迟标记)
- 【线段树延迟更新】HDU 3275
- [uva11992]Fast Matrix Operations(多延迟标记,线段树,区间更新)
- poj 3468 A Simple Problem with Integers(线段树、延迟更新)
- zoj 1610 Count the Colors(线段树延迟更新)
- 线段树(4)--poj3468(区间更新 延迟更新)
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs+二分+线段树延迟操作、树形化线性
- Codeforces Round #263 (Div. 2)E(线段树点更新)
- 线段树区间更新区间求和(转延迟标记精讲)
- poj3468 A Simple Problem with Integers 线段树延迟标记区间更新区间求和
- hdu1698 Just a Hook 线段树更新延迟标记
- 线段树区间更新延迟标记
- FZU Problem 2105 Digits Count线段树之延迟更新(插线取线)
- [线段树]poj3667 Hotel(区间合并、更新、延迟/懒惰标记
- codevs 1082 线段树练习 3 区间更新+延迟标记
- poj3468 A Simple Problem with Integers 线段树 更新延迟
- Poj 3468 A Simple Problem with Integers(线段树 区间更新 延迟标记)
- 【线段树】基本写法,区间极值,区间延迟更新,多延迟标签
- poj3468 线段树+延迟更新
- poj4047(线段树+延迟更新)