难得写一道线段树,先上代码.....
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define MAXN 50002
struct node{ int l, mid, r; char ch; int v; char ll[2], rr[2]; int num; } a[MAXN*4];
int n, m;
void pushup(int e)
{
a[e].v=a[e<<1].v+a[e<<1|1].v;
a[e].num=a[e<<1].num+a[e<<1|1].num;
a[e].ll[0]=a[e<<1].ll[0], a[e].rr[1]=a[e<<1|1].rr[1];
if(a[e<<1].num>1) { a[e].ll[1]=a[e<<1].ll[1];}
else a[e].ll[1]=a[e<<1|1].ll[0];
if(a[e<<1|1].num>1) a[e].rr[0]=a[e<<1|1].rr[0];
else a[e].rr[0]=a[e<<1].rr[1];
if(a[e<<1].num>1 && a[e<<1].rr[0]=='w' && a[e<<1].rr[1]=='b' && a[e<<1|1].ll[0]=='w') a[e].v++;
else if(a[e<<1|1].num>1 && a[e<<1].rr[1]=='w' && a[e<<1|1].ll[0]=='b' && a[e<<1|1].ll[1]=='w') a[e].v++;
}
void build(int l, int r, int e) ////CALL: build(1, n, 1)
{
a[e].l=l, a[e].r=r;
if(l==r) { scanf("%c", &a[e].ch); a[e].num=1; a[e].ll[0]=a[e].ch;
a[e].rr[1]=a[e].ch; }
else
{
a[e].mid=(l+r)>>1;
build(l, a[e].mid, e<<1);
build(a[e].mid+1, r, e<<1|1);
pushup(e);
}
}
void update(int k, char x, int l, int r, int e)
{
if(l==r)
{
a[e].ch=x;
a[e].ll[0]=a[e].ch;
a[e].rr[1]=a[e].ch;
}
else
{
if(k<=a[e].mid) update(k, x, l, a[e].mid, e<<1);
else update(k, x, a[e].mid+1, r, e<<1|1);
pushup(e);
}
}
int query(int L, int R, int l, int r, int e)
{
int ret=0;
if(L<=l && r<=R) //当前区间完全包含所查区间
{
ret+=a[e].v;
}
else // l L R r
{
if(a[e<<1].r-L>0 && R-a[e<<1|1].l+1>0 && a[e<<1].num>1 && a[e<<1].rr[0]=='w' && a[e<<1].rr[1]=='b'&& a[e<<1|1].ll[0]=='w')
ret++;
if(R-a[e<<1|1].l>0 && a[e<<1].r-L+1>0 && a[e<<1|1].num>1 && a[e<<1].rr[1]=='w' && a[e<<1|1].ll[0]=='b' && a[e<<1|1].ll[1]=='w')
ret++;
if(L<=a[e].mid)
ret+=query(L, R, l, a[e].mid, e<<1);
if(a[e].mid<R) ret+=query(L, R, a[e].mid+1, r, e<<1|1);
}
return ret;
}
int main()
{
int t; scanf("%d", &t);
for(int i=0; i<t; i++)
{
memset(a, 0, sizeof(a));
printf("Case %d:\n", i+1);
char o;
scanf("%d%d%c", &n, &m, &o);
build(1, n, 1);
for(int j=0; j<m; j++)
{
int flag;
scanf("%d", &flag);
if(flag==0)
{
int a, b; scanf("%d%d", &a, &b);
printf("%d\n", query(a+1, b+1, 1, n, 1));
}
else if(flag==1)
{
int a; char ch[2]; scanf("%d%s", &a, &ch);
update(a+1, ch[0], 1, n, 1);
}
}
}
}
浙公网安备 33010602011771号