hdu 4046(树状数组)
来源:互联网 发布:sql delete 编辑:程序博客网 时间:2024/06/10 07:44
题意:有一个字符串,由字母w和b构成,然后有q次询问,0 a b 表示下标a到b出现了多少个wbw,重叠也算,1 a c 表示把下标a处的字母变为c。0操作每次都有输出。
题解:树状数组类型题,主要输出注意边界Sum(r - 1) - Sum(l),因为c[i]是包含str[i - 1]、str[i]、str[i +1]三个字符得到的,所以当询问[l , r]的答案时,应该右边界是r - 1,左边以l + 1为起点,要把Sum(l)减掉。
#include <stdio.h>#include <string.h>const int N = 50005;int n, c[N], q;char str[N];int lowbit(int x) { //x的因子最多有2的几次幂,从右到左数的0的个数 return x & (-x);}int Sum(int x) { int sum = 0; while (x > 0) { sum += c[x]; x -= lowbit(x); } return sum;}void modify(int i, int x) { while (i <= n) { c[i] += x; i += lowbit(i); }}void init() { memset(c, 0, sizeof(c)); for (int i = 2; i < n; i++) if (str[i - 2] == 'w' && str[i - 1] == 'b' && str[i] == 'w') modify(i - 1, 1);}int main() { int t, cas = 1; scanf("%d", &t); while (t--) { scanf("%d%d%s", &n, &q, str); printf("Case %d:\n", cas++); init(); int a, b, l, r; for (int i = 0; i < q; i++) { scanf("%d", &a); if (a == 1) { char cc; scanf("%d %c", &b, &cc); if (str[b] != cc) { if (b >= 2 && str[b - 2] == 'w' && str[b - 1] == 'b' && str[b] == 'w') modify(b - 1, -1); if (b >= 1 && b < n - 1 && str[b - 1] == 'w' && str[b] == 'b' && str[b + 1] == 'w') modify(b, -1); if (b < n - 2 && str[b] == 'w' && str[b + 1] == 'b' && str[b + 2] == 'w') modify(b + 1, -1); if (b >= 2 && str[b - 2] == 'w' && str[b - 1] == 'b' && str[b] == 'b') modify(b - 1, 1); if (b >= 1 && b < n - 1 && str[b - 1] == 'w' && str[b] == 'w' && str[b + 1] == 'w') modify(b, 1); if (b < n - 2 && str[b] == 'b' && str[b + 1] == 'b' && str[b + 2] == 'w') modify(b + 1, 1); } str[b] = cc; } else { scanf("%d%d", &l, &r); if (r - l < 2) printf("0\n"); else printf("%d\n", Sum(r - 1) - Sum(l)); } } } return 0;}
0 0
- HDU 4046 树状数组
- hdu 4046 树状数组
- hdu 4046 树状数组
- hdu 4046 树状数组
- hdu 4046 树状数组
- hdu 4046(树状数组)
- HDU 4046 Panda 树状数组
- HDU 4046 Panda(树状数组)
- HDU 4046 Panda (树状数组)
- HDU 4046 Panda(树状数组)
- HDU 4046 Panda [树状数组]【数据结构】
- HDU(4046)树状数组入门题
- HDU 1394 树状数组
- 树状数组 hdu
- hdu 1166 树状数组
- HDU 2838 树状数组
- hdu 2492【树状数组】
- hdu 3015【树状数组】
- cocos2dx 3.x 命令行创建和编译工程
- 基于Redis实现分布式消息队列(汇总目录)
- MySQL Fabric
- 生成APK的签名
- 答辩完的随笔
- hdu 4046(树状数组)
- 交换两个变量值的方法
- Leetcode: Maximum Depth of Binary Tree
- 黑马程序员——c语言基础:一维数组
- 乐乐音乐播放器(三) 第一次引导页面
- cocos2dx 背包界面的实现
- UVALive 3641 Leonardo's Notebook(置换)
- 黑马程序员:iOS基础——个人笔记(七)
- Android 获取本地文件(遍历本地文件)