标签:des style blog color os io div ar
线段树单点更新,要注意两段合并多出的答案的计算即可
//============================================================================ // Name : D.cpp // Author : L_Ecry // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> #include <iostream> #define N 50050 using namespace std; int value[N*4]; char s[N]; inline int cal(int l,int r,int mid) { int res=0; if(mid>l&&s[mid-1]==‘w‘&&s[mid]==‘b‘&&s[mid+1]==‘w‘)res++; if(mid+1<r&&s[mid]==‘w‘&&s[mid+1]==‘b‘&&s[mid+2]==‘w‘)res++; return res; } void build(int l,int r,int i) { if(l==r) { value[i]=0; return ; } int mid=(l+r)>>1; int lson=(i<<1),rson=(i<<1|1); build(l,mid,lson); build(mid+1,r,rson); value[i]=value[lson]+value[rson]+cal(l,r,mid); } void update(int l,int r,int p,int i) { if(l==r) { return; } int mid=(l+r)>>1; int lson=(i<<1),rson=(i<<1|1); if(p<=mid)update(l,mid,p,lson); else update(mid+1,r,p,rson); value[i]=value[lson]+value[rson]+cal(l,r,mid); } int query(int l,int r,int pl,int pr,int i) { if(l==pl&&r==pr) { return value[i]; } int mid=(l+r)>>1; if(pr<=mid)return query(l,mid,pl,pr,i<<1); else if(pl>mid)return query(mid+1,r,pl,pr,i<<1|1); else { return query(l,mid,pl,mid,i<<1)+query(mid+1,r,mid+1,pr,i<<1|1)+cal(pl,pr,mid); } } int n,m; void init() { scanf("%d%d",&n,&m); scanf(" %s",s+1); build(1,n,1); } void solve() { while(m--) { int x,y,z; char c; scanf("%d%d",&x,&y); if(x==0) { scanf("%d",&z); y++;z++; printf("%d\n",query(1,n,y,z,1)); } else { scanf(" %c",&c); y++; s[y]=c; update(1,n,y,1); } } } int main() { int tt,ri=0; scanf("%d",&tt); while(tt--) { init(); printf("Case %d:\n",++ri); solve(); } return 0; }
标签:des style blog color os io div ar
原文地址:http://www.cnblogs.com/L-Ecry/p/3871471.html