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. jquery总结01-基本概念和选择器

    dom元素和jquery元素的区别 dom元素支持dom元素自带的属性和方法,jquery元素支持jquery元素自带的属性和方法 dom                  var div = doc ...

  2. linux tar.gz

    tar命令用于对文件打包压缩或解压,格式为:“tar [选项] [文件]”. 打包并压缩文件:“tar -czvf 压缩包名.tar.gz 文件名” 解压并展开压缩包:“tar -xzvf 压缩包名. ...

  3. python—基础类的那点儿所以然

    老师说:‘要知其然,更要知其所以然’~~~那么今天就来说点儿所以然,对python中的int,str,lst,dict和tuple等基础类中的方法做一些解析 那么类是什么呢? 官方的解释是这样的:对象 ...

  4. jQuery对input select操作小结

    //遍历option和添加.移除optionfunction changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE] ...

  5. log4net.redis+logstash+kibana+elasticsearch+redis 实现日志系统

    前端时间写了个随笔 log4net.NoSql +ElasticSearch 实现日志记录 ,因项目原因需要把日志根java平台的同事集成采用logstash+kibana+elasticsearch ...

  6. 为什么高手离不了Linux系统?这就是我的理由

    摘要: 通过本文来记录下我在Linux系统的学习经历,聊聊我为什么离不了Linuxx系统,同时也为那些想要尝试Linux而又有所顾忌的用户答疑解惑,下面将为你介绍我所喜欢的Linux系统,这里有一些你 ...

  7. ROS 使用自带和usb摄像头获取图像

    笔记本自带的摄像头的设备号一般为/dev/video0 第一步:安装Webcam 驱动 $ sudo apt-get install git-core $ cd ~/catkin_ws/src $ g ...

  8. 【个人使用.Net类库】(3)Excel文件操作类(基于NPOI)

    Web开发工作中经常要根据业务的需要生成对应的报表.经常采用的方法如下: 将DataTable导出至Excel文件; 读取模板Excel文件; 修改模板Excel文件对应的内容. 因此,便想到封装一个 ...

  9. linux命令每日一练习-top free

    top看看进程的内存使用情况 free产看内存使用情况 top -n 2 -b > log.txt   将更新两次的结果输入到log.txt cat > log.txt //清空文件并写入 ...

  10. 原生javascript和jquery实现简单的ajax例子

    后台C#代码 public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/p ...