线段树 hdu4046
Panda
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2900 Accepted Submission(s): 966
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?
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’)
The answer of the question.

#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的更多相关文章
- hdu4046 不错的线段树单点更新
题意: 给一个字符串,两种操作 0 a b 询问a,b之间有多少个wbw, 1 a c 就是把第a个改成c. 思路: 这个题目我们可以用线段树的点更新来做,一开始写了个好长好长 ...
- bzoj3932--可持久化线段树
题目大意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,Pi)描述,(Si,Ei,Pi)表示任务从第Si秒开始,在第 ...
- codevs 1082 线段树练习 3(区间维护)
codevs 1082 线段树练习 3 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题目描述 Description 给你N个数,有两种操作: 1:给区 ...
- codevs 1576 最长上升子序列的线段树优化
题目:codevs 1576 最长严格上升子序列 链接:http://codevs.cn/problem/1576/ 优化的地方是 1到i-1 中最大的 f[j]值,并且A[j]<A[i] .根 ...
- codevs 1080 线段树点修改
先来介绍一下线段树. 线段树是一个把线段,或者说一个区间储存在二叉树中.如图所示的就是一棵线段树,它维护一个区间的和. 蓝色数字的是线段树的节点在数组中的位置,它表示的区间已经在图上标出,它的值就是这 ...
- codevs 1082 线段树区间求和
codevs 1082 线段树练习3 链接:http://codevs.cn/problem/1082/ sumv是维护求和的线段树,addv是标记这歌节点所在区间还需要加上的值. 我的线段树写法在运 ...
- PYOJ 44. 【HNSDFZ2016 #6】可持久化线段树
#44. [HNSDFZ2016 #6]可持久化线段树 统计 描述 提交 自定义测试 题目描述 现有一序列 AA.您需要写一棵可持久化线段树,以实现如下操作: A v p x:对于版本v的序列,给 A ...
- CF719E(线段树+矩阵快速幂)
题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
随机推荐
- 《BI那点儿事》数据流转换——导入列、导出列
导入列: 导入列例子现在来做一个例子:创建路径D:\Pictures随便在路径D:\Pictures中粘贴4个比较小的图像文件命名为01.png.02.png.03.png.04.png在路径D:\P ...
- Scrum Meeting 1-20151201
任务安排 姓名 今日任务 明日任务 困难 董元财 学习下拉刷新的实现 完成下拉刷新的实现 手机的点击动作长按和下拉有类似的地方,比较难解决 胡亚坤 学习圆形头像代码设计 完成圆形头像代码设计 无 刘猛 ...
- [Git] 怎么使用Git让代码回到以前的某个节点
我们可以使某个文件回到以前的某个节点,也可以使整个文件夹下面的文件都回到以前的某个节点,下面只介绍某个文件的,全部的类似. 按步骤操作完成后本地你选中的文件会变成红色,此时的代码还是原来的代码,但是G ...
- Listview 隐藏item
隐藏某一项item(防止list改变后导致复用convertview而错乱--如果删掉list中的该项,会导致复用convertview混乱) 方法: convertView.setVisibilit ...
- BootStrapt iCheck表单美化插件使用方法详解(含参数、事件等) 全选 反选
特色: 1.在不同浏览器(包括ie6+)和设备上都有相同的表现 — 包括 桌面和移动设备 2.支持触摸设备 — iOS.Android.BlackBerry.Windows Phone等系统 4.方便 ...
- Linux系统下Apache2.4.17的安装过程
Linux系统下安装Apache Server2.4.17.还是先声明一下,Linux命令我不进行讲解,因为我不是讲Linux命令的.有需要注意的地方,我会上图,没什么值得的注意的地方,我就不上图了. ...
- h.Connector的SSL属性实现
前面分析了Connector的配置,第一步,Digester已经将上述的属性设置到Connector和xxxEndpoint中了. 下面对于一些核心属性,看看Tomcat是如何使用的: 1.SSLEn ...
- archaism remains
Time past cannot be called back again. 时间不能倒流. Time tries all. 路遥知马力,日久见人心. Tit for tat is fair p ...
- python窗体——pyqt初体验
连续两周留作业要写ftp的作业,从第一周就想实现一个窗体版本的,但是时间实在太短,qt零基础选手表示压力很大,幸好又延长了一周时间,所以也就有了今天这篇文章...只是为了介绍一些速成的方法,还有初学者 ...
- windows添加虚拟网卡