Panda

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

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<=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<=50000,m<=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<=L<=R<n)
Type 1: change the kth character to ch(0<=k<n and ch is ‘b’ or ‘w’)
 
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
 
题意:
    先输入n,m;   再输入n长的字符串,下面有m个询问;
    当输入0时,输出,( l , r )内," wbw "的数量;
    当输入1时,将下标为 k 的字符改为 字符 ch;
思路:
  线段树,判断每一个点 以该下标结束的长度为3的子串是否为"wbw", 若是,则等于1,否则等于0,保存在数组num[]中。
  所以,当进行区间询问时,对[a,b]的询问则应该改为对区间[a+2,b],当然这里对a+2,和b的大小还是要讨论的;
  当进行修改点的时候,要考虑几种情况,具体看代码;
代码:
 

#include "map"
#include "stack"
#include "queue"
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "iostream"
#include "algorithm"
using namespace std;
#define N 50012
#define inf 2000000000
#define pi 3.1415926535897932384626433832795028841971
int num[N*5];// 表示以该下标结束的长度为3的子串是否为"wbw"
char s[N*5];//输入的字符串
struct seg
{
int l;//左范围
int r;//右范围
int n;//这个区间代表的值
}T[N*4];
void build(int l,int r,int k)//建树(前序)
{
int mid;
mid=(l+r)/2;
T[k].l=l;
T[k].r=r;
T[k].n=0;
if(l==r)
{
T[k].n=num[l];
return;
}
build(l,mid,2*k);
build(mid+1,r,2*k+1);
T[k].n=T[2*k].n+T[2*k+1].n;//更新父亲节点
}
void insert(int n,int d,int k)//d:改变的叶子节点,n:改变的量
{
int mid;
if(T[k].l==T[k].r&&T[k].l==d)
{
T[k].n=n;
return ;
}
mid=(T[k].l+T[k].r)/2;
if(d<=mid) insert(n,d,2*k);
else insert(n,d,2*k+1);
T[k].n=T[2*k].n+T[2*k+1].n;//更新父亲节点
}
int search(int l,int r,int k)//l,r查找的区间
{
int mid;
if(T[k].l==l&&T[k].r==r)
{
return T[k].n;
}
mid=(T[k].l+T[k].r)/2;
if(r<=mid) return search(l,r,2*k);
else if(l>mid) return search(l,r,2*k+1);
else
{
return search(l,mid,2*k)+search(mid+1,r,2*k+1);
}
}
int main()
{
int t,n,m,i,p,l,r,cas=1;
char k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
scanf("%s",s);
memset(num,0,sizeof(num));
printf("Case %d:\n",cas++);
for(i=2;i<n;i++)
{
if(s[i-2]=='w'&&s[i-1]=='b'&&s[i]=='w')
num[i]=1;
else
num[i]=0;
}
build(0,n-1,1);
while(m--)
{
scanf("%d",&p);
if(!p)
{
scanf("%d%d",&l,&r);
if(l+2>r)//询问的区间小于3时,直接输出0
{
printf("0\n");
continue;
}
printf("%d\n",search(l+2,r,1));
}
else
{
scanf("%d %c",&i,&k);
if(s[i]==k)//相同就不改
continue;
//改变该点,可能改变三个字符串,一个它为第一个时,一个它为第二个时,一个它为第三个时
if(i>1&&i<n&&s[i-2]=='w'&&s[i-1]=='b'&&s[i]=='w')
{
insert(0,i,1);
num[i]=0;
}
if(i>1&&i<n&&s[i-2]=='w'&&s[i-1]=='b'&&k=='w')
{
insert(1,i,1);
num[i]=1;
}
if(i>0&&i<n-1&&s[i-1]=='w'&&s[i]=='b'&&s[i+1]=='w')
{
insert(0,i+1,1);
num[i+1]=0;
}
if(i>0&&i<n-1&&s[i-1]=='w'&&k=='b'&&s[i+1]=='w')
{
insert(1,i+1,1);
num[i+1]=1;
}
if(i>=0&&i<n-1&&s[i]=='w'&&s[i+1]=='b'&&s[i+2]=='w')
{
insert(0,i+2,1);
num[i+2]=0;
}
if(i>=0&&i<n-1&&k=='w'&&s[i+1]=='b'&&s[i+2]=='w')
{
insert(1,i+2,1);
num[i+2]=1;
}
s[i]=k;//字符记得改变
}
}
}
return 0;
}

线段树 hdu4046的更多相关文章

  1. hdu4046 不错的线段树单点更新

    题意:       给一个字符串,两种操作 0 a b 询问a,b之间有多少个wbw, 1 a c 就是把第a个改成c. 思路:       这个题目我们可以用线段树的点更新来做,一开始写了个好长好长 ...

  2. bzoj3932--可持久化线段树

    题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...

  3. codevs 1082 线段树练习 3(区间维护)

    codevs 1082 线段树练习 3  时间限制: 3 s  空间限制: 128000 KB  题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...

  4. codevs 1576 最长上升子序列的线段树优化

    题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...

  5. codevs 1080 线段树点修改

    先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...

  6. codevs 1082 线段树区间求和

    codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...

  7. PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树

    #44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...

  8. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

  9. 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序

    3779: 重组病毒 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 224  Solved: 95[Submit][Status][Discuss] ...

随机推荐

  1. Nginx模块之————RTMP模块在Ubuntu 14.04年的设置与搭建

    Nginx的设置,RTMP在Ubuntu 14.04 https://www.vultr.com/docs/setup-nginx-rtmp-on-ubuntu-14-04

  2. 初学者用div+css结构写网页的几个误区

    1.用div+css结构制作静态html网页不等于彻底抛弃古老的table写法.之所以不建议用table来布局网页是因为在网页加载很慢的时候要等table结构加载完成才能看到网页,其次是table的布 ...

  3. js 过滤script

    function stripscript(s) {      return s.replace(/<script.*?>.*?<\/script>/ig, '');  } 稍微 ...

  4. Android各组件/控件间通信利器之EventBus

    实际项目开发过程中,经常遇到如下场景:不同的应用程序组件的控件间具有一定的相互关联性,其中用户对后者进行的某种操作会引起前者的相应改变.举一个具体的场景:以糗事百科为例,在糗事列表页和详情页页,对于每 ...

  5. 【教程】Asset Server(联合开发)

    Unity Asset Server下载 https://unity3d.com/cn/unity/team-license http://tieba.baidu.com/p/2419391804 W ...

  6. 0020 Linux 文件操作命令

    1. 创建文件 touch 文件名 2. 删除文件 rm 文件名 3. 复制文件 cp 源文件 目录 4.剪切文件 mv 源文件 目标文件 5.重命名文件 mv 源文件名 新文件名 6.改变文件权限 ...

  7. 在eclipse中导入weka(小白在路上)

    第一步:新建一个java工程,new->javaproject,假设工程名为wekatest 第二步:导入weka.jar 第三步:src关联 导入后有许多的.class文件,直接双击打开是看不 ...

  8. PDF 补丁丁 0.4.3.1518 测试版发布:书签编辑器新增升级书签功能、优化PDF文档阅览器

    新的 PDF 补丁丁测试版上线啦! 新版本增加了升级书签的功能(见工具栏的“←”按钮),可以方便地将下级书签升级为上级书签. 另外,新版本还增强了书签编辑器功能中的 PDF阅读器,从之前的单页阅读模式 ...

  9. ios打包

    ios7.1及以上 itms-services://?spm=0.0.0.0.WIsvD2&action=download-manifest&url=https://mtl.aliba ...

  10. Grunt完成对LESS实时编译

    安装 安装grunt需要先安装node.js. 之后需要借助npm来安装grunt-cli,在cmd中npm install -g grunt-cli.(测试grunt --version看是否正确显 ...