您的位置:首页 > 其它

线段树 HDU 4046 panda

2012-08-30 16:18 337 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046

代码风格:www.notonlysuccess.com

题目大意:求wbw出现的个数

算法:线段树,区间合并

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define lson l, m, rt << 1
#define rson m+1, r, rt << 1 | 1
#define mid int m = (r+l) >> 1

int cnt[454545];
char s[465456];

void PushUp(int L, int R, int m, int rt)
{
cnt[rt] = cnt[rt << 1] + cnt[rt << 1 | 1];
if(L <= m && m+2 <= R)
if(s[m] == 'w' && s[m+1] == 'b' && s[m+2] == 'w')
cnt[rt] ++;
if(L <= m-1 && m + 1 <= R)
{
if(s[m-1] == 'w' && s[m] == 'b' && s[m+1] == 'w')
{
cnt[rt] ++;
}
}
}

void build(int l, int r, int rt)
{
if(r == l)
return ;
mid ;
build(lson);
build(rson);
PushUp(l, r, m, rt);
}

void update(int d, int l, int r, int rt)
{
if(l == r)
{
return ;
}
mid ;
if(d <= m)
update(d, lson);
else update(d, rson);
PushUp(l, r, m, rt);
}

int minz(int a, int b)
{
return a < b ? a : b;
}

int maxz(int a, int b)
{
return a > b ? a : b;
}

int query(int L, int R, int l, int r, int rt)
{
if(L <= l && r <= R)
return cnt[rt];
mid ;
int ret  = 0;
if(R <= m)
return query(L, R, lson);
else if(L > m)
return query(L, R, rson);
else
ret += query(L, R, lson) + query(L, R, rson);
if(maxz(l, L) <= m && m+2 <= minz(r, R))
if(s[m] == 'w' && s[m+1] == 'b' && s[m+2] == 'w')
ret ++;
if(maxz(l, L) <= m-1 && m + 1 <= minz(r, R))
if(s[m-1] == 'w' && s[m] == 'b' && s[m+1] == 'w')
ret ++;
return ret ;
}

int main()
{
int n, m, T;
int a, b, c;
int ica = 1;
char op[45];
scanf("%d", &T);
while(T --){
printf("Case %d:\n", ica ++);
memset(s, 0, sizeof(s));
memset(cnt, 0, sizeof(cnt));
scanf("%d%d", &n, &m);
scanf("%s", s);
build(0, n-1, 1);
while(m --){
scanf("%d%d", &a, &b);
if(a == 1)
{
scanf("%s", op);
s[b] = op[0];
update(b, 0, n-1, 1);
}
else
{
scanf("%d", &c);
printf("%d\n", query(b, c, 0, n-1, 1));
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: