热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

HDU4046Panda[树状数组]【数据结构】

题目链接:http:acm.hdu.edu.cnshowproblem.php?pid4046—————————————————————.PandaTimeLi

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046

—————————————————————.

Panda

Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3439 Accepted Submission(s): 1113

Problem Description
When I wrote down this letter, you may have been on the airplane to U.S.
We have known for 15 years, which has exceeded one-fifth of my whole life. I still remember the first time we went to the movies, the first time we went for a walk together. I still remember the smiling face you wore when you were dressing in front of the mirror. I love your smile and your shining eyes. When you are with me, every second is wonderful.
The more expectation I had, the more disappointment I got. You said you would like to go to U.S.I know what you really meant. I respect your decision. Gravitation is not responsible for people falling in love. I will always be your best friend. I know the way is difficult. Every minute thinking of giving up, thinking of the reason why you have held on for so long, just keep going on. Whenever you’re having a bad day, remember this: I LOVE YOU.
I will keep waiting, until you come back. Look into my eyes and you will see what you mean to me.
There are two most fortunate stories in my life: one is finally the time I love you exhausted. the other is that long time ago on a particular day I met you.
Saerdna.

It comes back to several years ago. I still remember your immature face.
The yellowed picture under the table might evoke the countless memory. The boy will keep the last appointment with the girl, miss the heavy rain in those years, miss the love in those years. Having tried to conquer the world, only to find that in the end, you are the world. I want to tell you I didn’t forget. Starry night, I will hold you tightly.

Saerdna loves Panda so much, and also you know that Panda has two colors, black and white.
Saerdna wants to share his love with Panda, so he writes a love letter by just black and white.
The love letter is too long and Panda has not that much time to see the whole letter.
But it’s easy to read the letter, because Saerdna hides his love in the letter by using the three continuous key words that are white, black and white.
But Panda doesn’t know how many Saerdna’s love there are in the letter.
Can you help Panda?

Input
An integer T means the number of test cases T<&#61;100
For each test case:
First line is two integers n, m
n means the length of the letter, m means the query of the Panda. n<&#61;50000,m<&#61;10000
The next line has n characters ‘b’ or ‘w’, ‘b’ means black, ‘w’ means white.
The next m lines
Each line has two type
Type 0: answer how many love between L and R. (0<&#61;L<&#61;R Type 1: change the kth character to ch(0<&#61;k

Output
For each test case, output the case number first.
The answer of the question.

Sample Input
2

5 2
bwbwb
0 0 4
0 1 3
5 5
wbwbw
0 0 4
0 0 2
0 2 4
1 2 b
0 0 4

Sample Output
Case 1:
1
1
Case 2:
2
1
1
0

Source
The 36th ACM/ICPC Asia Regional Beijing Site —— Online Contest

—————————————————————.

题目大意&#xff1a;
就是最开始有一个n这么长的串 有m次操作
操作&#xff10;&#xff0c;L&#xff0c;R&#xff0c;询问L~R中内包含”wbw”的个数。
操作&#xff11;&#xff0c;k&#xff0c;ch&#xff0c;将第k个字符更换为ch。

解题思路&#xff1a;
就是用树状数组维护一下 &#xff0c;在“wbw”中以左边为界放到树状数组中就行了。

然后在更换字符的时候
枚举一下几种情况就行了

附本题代码
—————————————–.

#include #define abs(x) (((x)>0)?(x):-(x))
#define lalal puts("*********")
using namespace std;
/**************************************/
const int N &#61; 50000&#43;5;
#define lowbit(x) (x&-x)
int sum[N],cnt;
void update(int index,int val){for(int i&#61;index; i<&#61;cnt; i&#43;&#61;lowbit(i)){sum[i]&#43;&#61;val;}return ;
}int getSum(int index){int ans &#61; 0;for(int i&#61;index; i>0; i-&#61;lowbit(i)){ans&#43;&#61;sum[i];}return ans ;
}char a[N];
int main(){int _,kc;while(~scanf("%d",&_)){kc&#61;0;while(_--){memset(sum,0,sizeof(sum));int n,m;scanf("%d %d",&n,&m);cnt&#61;n;scanf("%s",a&#43;1);for(int i&#61;3; i<&#61;n; i&#43;&#43;){if(a[i-2]&#61;&#61;&#39;w&#39;&&a[i-1]&#61;&#61;&#39;b&#39;&&a[i]&#61;&#61;&#39;w&#39;)update(i,1);}int order,l,r,k;char ch;printf("Case %d:\n",&#43;&#43;kc);while(m--){scanf("%d",&order);if(1&#61;&#61;order){scanf("%d %c",&k,&ch);k&#43;&#43;;if(a[k]&#61;&#61;ch) continue;if(k>&#61;3&&a[k-2]&#61;&#61;&#39;w&#39;&&a[k-1]&#61;&#61;&#39;b&#39;&&a[k]&#61;&#61;&#39;w&#39;)update(k,-1);if(k>&#61;3&&a[k-2]&#61;&#61;&#39;w&#39;&&a[k-1]&#61;&#61;&#39;b&#39;&&ch&#61;&#61;&#39;w&#39;)update(k,1);if(k>&#61;2&&k&#43;1<&#61;n&&a[k-1]&#61;&#61;&#39;w&#39;&&a[k]&#61;&#61;&#39;b&#39;&&a[k&#43;1]&#61;&#61;&#39;w&#39;)update(k&#43;1,-1);if(k>&#61;2&&k&#43;1<&#61;n&&a[k-1]&#61;&#61;&#39;w&#39;&&ch&#61;&#61;&#39;b&#39;&&a[k&#43;1]&#61;&#61;&#39;w&#39;)update(k&#43;1,1);if(k&#43;2<&#61;n&&a[k]&#61;&#61;&#39;w&#39;&&a[k&#43;1]&#61;&#61;&#39;b&#39;&&a[k&#43;2]&#61;&#61;&#39;w&#39;)update(k&#43;2,-1);if(k&#43;2<&#61;n&&ch&#61;&#61;&#39;w&#39;&&a[k&#43;1]&#61;&#61;&#39;b&#39;&&a[k&#43;2]&#61;&#61;&#39;w&#39;)update(k&#43;2,1);a[k]&#61;ch;}if(0&#61;&#61;order){scanf("%d %d",&l,&r);if(r-l<2) puts("0");else printf("%d\n",getSum(r&#43;1)-getSum(l&#43;1&#43;1));}}}}return 0;
}

推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • 自动轮播,反转播放的ViewPagerAdapter的使用方法和效果展示
    本文介绍了如何使用自动轮播、反转播放的ViewPagerAdapter,并展示了其效果。该ViewPagerAdapter支持无限循环、触摸暂停、切换缩放等功能。同时提供了使用GIF.gif的示例和github地址。通过LoopFragmentPagerAdapter类的getActualCount、getActualItem和getActualPagerTitle方法可以实现自定义的循环效果和标题展示。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • 电话号码的字母组合解题思路和代码示例
    本文介绍了力扣题目《电话号码的字母组合》的解题思路和代码示例。通过使用哈希表和递归求解的方法,可以将给定的电话号码转换为对应的字母组合。详细的解题思路和代码示例可以帮助读者更好地理解和实现该题目。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • SpringBoot uri统一权限管理的实现方法及步骤详解
    本文详细介绍了SpringBoot中实现uri统一权限管理的方法,包括表结构定义、自动统计URI并自动删除脏数据、程序启动加载等步骤。通过该方法可以提高系统的安全性,实现对系统任意接口的权限拦截验证。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
author-avatar
jAne
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有