线段树 HDU 4046 panda
2012-08-30 16:18
337 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046
代码风格:www.notonlysuccess.com
题目大意:求wbw出现的个数
算法:线段树,区间合并
代码风格: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; }
相关文章推荐
- HDU 4046 Panda(线段树单点更新)
- hdu 4046 Panda (线段树 单点更新 区间查询)
- hdu 4046 Panda 线段树
- HDU 4046 Panda(RMQ 线段树 树状数组)
- [HDU 4046]Panda[线段树]
- hdu 4046 Panda 线段树 单点修改 求区间010个数
- HdU 4046 Panda 线段树
- [置顶] HDU 4046 Panda (分块 或 线段树区间合并)
- hdu 4046 Panda (线段树,单点更新,区间求和)
- HDU 4046 Panda(线段树)
- hdu 4046 Panda [线段树]
- HDU 4046 - Panda (线段树 + 单点更新)
- hdu 4046 Panda(线段树+单点更新)
- hdu_4046 Panda线段树_点修改_区间求和
- HDU 4046 Panda
- HDU - 4046 Panda
- hdu 4046 Panda
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
- HDU 4046 Panda(树状数组)
- hdu 4046 探查wbw组合数 线段树