hdu 4046 树状数组
2013-08-19 18:44
274 查看
View Code
#include<stdio.h> #include<string.h> int c[50010]; char str[50010]; int n,m; int lowbit(int x) { return x&(-x); } void update(int x,int d) { while(x<=n) { c[x]+=d; x+=lowbit(x); } } int sum(int x) { int ans=0; while(x>0) { ans+=c[x]; x-=lowbit(x); } return ans; } int sum(int l,int r) { if (l > r) return 0; if (l == 0) return sum(r); return sum(r) - sum(l - 1); } int main() { int i,j,type,L,R,pos; int t,cases=1; char s[5]; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); memset(c,0,sizeof(c)); scanf("%s",str+1); printf("Case %d:\n",cases++); for(i=1;i<=n-2;i++) { if(str[i]=='w'&&str[i+1]=='b'&&str[i+2]=='w') update(i,1); } while(m--) { int ret; scanf("%d",&type); if(type==0) { scanf("%d%d",&L,&R); ret=sum(L+1,R-1); printf("%d\n",ret); } else { scanf("%d%s",&pos,s); pos++; char ch=str[pos]; str[pos]=s[0]; if(pos-2>=1) { if(str[pos-2]=='w'&&str[pos-1]=='b'&&ch=='w') { if(str[pos]=='b') update(pos-2,-1); } else { if(str[pos-2]=='w'&&str[pos-1]=='b'&&str[pos]=='w') update(pos-2,1); } } if(pos-1>=1&&pos+1<=n) { if(str[pos-1]=='w'&&ch=='b'&&str[pos+1]=='w') { if(str[pos]=='w') update(pos-1,-1); } else { if(str[pos-1]=='w'&&str[pos]=='b'&&str[pos+1]=='w') update(pos-1,1); } } if(pos+2<=n) { if(ch=='w'&&str[pos+1]=='b'&&str[pos+2]=='w') { if(str[pos]=='b') update(pos,-1); } else { if(str[pos]=='w'&&str[pos+1]=='b'&&str[pos+2]=='w') update(pos,1); } } } } } }
最后更新的部分可能写烦了*_*,思想才是最重要的
相关文章推荐
- hdu 4046 树状数组
- hdu 4046 Panda 树状数组
- hdu 4046 树状数组
- hdu 4046(树状数组)2011年预选网络赛
- HDU 4046 Panda(RMQ 线段树 树状数组)
- hdu 4046 树状数组
- hdu 1559 最大子矩阵(二维树状数组)
- hdu 3333 树状数组
- HDU 1166 敌兵布阵(线段树,树状数组)
- hdu 5592 ZYB's Game 树状数组
- hdu 2642(比较裸的二维树状数组)
- hdu 4638 Group 树状数组
- HDU 2492 Ping pong【树状数组好题! + 思维】
- HDU 5147 Sequence II 【树状数组维护信息 + 后缀和思想】
- HDU 3874 树状数组
- HDU 3792 Twin Prime Conjecture(素数筛法 + 树状数组)
- HDU 1892 二维树状数组
- POJ 2352 & HDU 1541 Stars ( 树状数组)
- HDU 4417 Super Mario (树状数组+离线处理)(划分树+二分答案)
- HDU 3584 Cube (三维树状数组)