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. Export Data from mysql Workbench 6.0

    原文地址:export-data-from-mysql-workbench-6-0 问题描述 I'm trying to export my database, using MySQL Workben ...

  2. Reverse a singly linked list

    Reverse a singly linked list. /** * Definition for singly-linked list. * struct ListNode { * int val ...

  3. How to change owner of PostgreSql database?

    ALTER DATABASE name OWNER TO new_owner;

  4. Openvpn 本地密码验证

    1.修改配置文件.(添加下列配置) auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env #开启用户密码脚本 client-cert-not-r ...

  5. laravel 加中间件的方法 防止直接打开后台

    路由 routes.php Route::group(['middleware' => ['web','admin.login.login']], function () { //后台首页路由 ...

  6. ssm(spring,springmvc,mybatis)

    1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  7. css——手机端图片正确显示

    这两天遇到的问题汇总(一): 1.图片在app端显示有差异:如下                        左边图片是:图片显示太大,以至于让整个页面都失真的效果:右边是调好样式之后的效果,知道增 ...

  8. SEO命令之”site“运用详解

    一.“site”基本介绍: 都知道要想查询一个特定网站的收录状况一般会分为两种情况:一.结果中有返回数据,则表明该网站已被收录:二.如果返回数据为空,则该网站未被收录.如果是以前已被收录的,现在来查没 ...

  9. iOS开发拓展篇—音频处理(音乐播放器3)

    iOS开发拓展篇—音频处理(音乐播放器3) 说明:这篇文章主要介绍音频工具类和播放工具类的封装. 一.控制器间数据传递 1.两个控制器之间数据的传递 第一种方法:self.parentViewCont ...

  10. MySQL删除表数据

    原文请点这里 在MySQL中有两种方法可以删除数据,一种是DELETE语句,另一种是TRUNCATE TABLE语句.DELETE语句可以通过WHERE对要删除的记录进行选择.而使用TRUNCATE ...