hdu 4046 Panda [线段树]
Panda
Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2565 Accepted Submission(s): 861
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.
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
1
1
Case 2:
2
1
1
0
13075023 | 2015-03-09 18:29:04 | Accepted | 4046 | 1466MS | 5532K | 4484 B | G++ | czy |
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #define N 50005
- using namespace std;
- int n,m;
- int T;
- int ccnt;
- char s[N];
- typedef struct
- {
- int num;
- int sw;
- int sbw;
- int ew;
- int ewb;
- }PP;
- PP p[*N];
- int mp[*N];
- int ll[*N],rr[*N];
- PP build(int i,int l,int r)
- {
- ll[i]=l;
- rr[i]=r;
- if(l==r){
- mp[l]=i;
- p[i].num=;
- if(s[l]=='b') {p[i].ew=p[i].ewb=p[i].sw=p[i].sbw=;}
- else {p[i].sw=;p[i].sbw=;p[i].ew=;p[i].ewb=;}
- // printf(" i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,p[i].num,p[i].sw,p[i].sbw,p[i].ew,p[i].ewb);
- return p[i];
- }
- PP le,ri;
- int mid=(l+r)/;
- le=build(i*,l,mid);
- ri=build(i*+,mid+,r);
- p[i].num=le.num+ri.num;
- if(le.ew * ri.sbw ==){
- p[i].num++;
- }
- if(le.ewb * ri.sw ==){
- p[i].num++;
- }
- p[i].sw=le.sw;
- p[i].ew=ri.ew;
- if(p[i].sw== && s[l+]=='w'){
- p[i].sbw=;
- }
- else{
- p[i].sbw=;
- }
- if(p[i].ew== && s[r-]=='w'){
- p[i].ewb=;
- }
- else{
- p[i].ewb=;
- }
- // printf(" i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,p[i].num,p[i].sw,p[i].sbw,p[i].ew,p[i].ewb);
- return p[i];
- }
- void ini()
- {
- scanf("%d%d",&n,&m);
- scanf("%s",s+);
- //printf(" n=%d m=%d s=%s\n",n,m,s+1 );
- build(,,n);
- // for(int i=1;i<=9;i++) printf("i=%d num=%d\n",i,p[i].num );
- }
- PP query(int i,int l,int r,int L ,int R)
- {
- PP te;
- if(r<L || l>R){
- te.num=;te.sw=te.sbw=te.ew=te.ewb=;
- return te;
- }
- if(l==r) return p[i];
- if(l>=L && r<=R){
- //printf(" q i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,p[i].num,p[i].sw,p[i].sbw,p[i].ew,p[i].ewb);
- return p[i];
- }
- int mid=(l+r)/;
- PP le,ri;
- if(mid>=L){
- le=query(i*,l,mid,L,R);
- }
- else{
- le.num=;le.sw=le.sbw=le.ew=le.ewb=-;
- }
- if(mid<R){
- ri=query(i*+,mid+,r,L,R);
- }
- else{
- ri.num=;ri.sw=ri.sbw=ri.ew=ri.ewb=-;
- }
- te.num=le.num+ri.num;
- if(le.ew * ri.sbw ==){
- te.num++;
- }
- if(le.ewb * ri.sw ==){
- te.num++;
- }
- te.sw=le.sw;
- te.ew=ri.ew;
- if(l+<=R && te.sw== && s[l+]=='w'){
- te.sbw=;
- }
- else{
- te.sbw=;
- }
- if(r->=L && te.ew== && s[r-]=='w'){
- te.ewb=;
- }
- else{
- te.ewb=;
- }
- //printf(" le q i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,le.num,le.sw,le.sbw,le.ew,le.ewb);
- //printf(" ri q i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,ri.num,ri.sw,ri.sbw,ri.ew,ri.ewb);
- //printf(" q i=%d l=%d r=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,l,r,te.num,te.sw,te.sbw,te.ew,te.ewb);
- return te;
- }
- void updata(int i)
- {
- int l,r;
- l=ll[i];
- r=rr[i];
- if(i==) return;
- PP le,ri;
- le=p[i*];ri=p[i*+];
- p[i].num=le.num+ri.num;
- if(le.ew * ri.sbw ==){
- p[i].num++;
- }
- if(le.ewb * ri.sw ==){
- p[i].num++;
- }
- p[i].sw=le.sw;
- p[i].ew=ri.ew;
- if(p[i].sw== && s[l+]=='w'){
- p[i].sbw=;
- }
- else{
- p[i].sbw=;
- }
- if(p[i].ew== && s[r-]=='w'){
- p[i].ewb=;
- }
- else{
- p[i].ewb=;
- }
- updata(i/);
- }
- void solve()
- {
- int t,L,R;
- int k;
- char ch[];
- int i,j;
- PP ans;
- for(j=;j<=m;j++){
- scanf("%d",&t);
- // printf(" j=%d t=%d\n",j,t );
- if(t==){
- scanf("%d%d",&L,&R);
- // printf(" L=%d R=%d\n",L,R );
- ans=query(,,n,L+,R+);
- printf("%d\n", ans.num);
- }
- else{
- scanf("%d%s",&k,ch);
- // printf(" j=%d t=%d k=%d ch=%s\n", j,t,k,ch);
- k++;
- s[k]=ch[];
- i=mp[k];
- p[i].num=;
- if(s[k]=='b') {p[i].ew=p[i].ewb=p[i].sw=p[i].sbw=;}
- else {p[i].sw=;p[i].sbw=;p[i].ew=;p[i].ewb=;}
- updata(i/);
- // for(int i=1;i<=9;i++) printf(" i=%d num=%d sw=%d sbw=%d ew=%d ewb=%d\n", i,p[i].num,p[i].sw,p[i].sbw,p[i].ew,p[i].ewb);
- }
- }
- }
- int main()
- {
- scanf("%d",&T);
- for(ccnt=;ccnt<=T;ccnt++){
- ini();
- printf("Case %d:\n",ccnt );
- solve();
- }
- }
hdu 4046 Panda [线段树]的更多相关文章
- HdU 4046 Panda 段树
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046 意甲冠军:到了bw组成的长度为n的字符串(n<=50000).有m次操作(m<=1000 ...
- HDU 4046 Panda(树状数组)
Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- hdu 4031 attack 线段树区间更新
Attack Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)Total Subm ...
- hdu 4288 离线线段树+间隔求和
Coder Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- hdu 3016 dp+线段树
Man Down Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- HDU—4046 Panda (线段树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4046 题意:给出一个字符串,统计这个字符串任意区间中"wbw"出现的次数. 规定两 ...
- HDU 5877 dfs+ 线段树(或+树状树组)
1.HDU 5877 Weak Pair 2.总结:有多种做法,这里写了dfs+线段树(或+树状树组),还可用主席树或平衡树,但还不会这两个 3.思路:利用dfs遍历子节点,同时对于每个子节点au, ...
- HDU 3308 LCIS (线段树区间合并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...
随机推荐
- Spring-bean(零)
内容提要:红为1,黄2,绿3 -----配置形式:基于xml文件的方式:基于注解的方式 -----Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),Facto ...
- PKU_campus_2018_A Wife
思路: 题目链接http://poj.openjudge.cn/practice/C18A/ 先说一个结论,每一天要么7要么0,由此提供一种状态压缩dp的解法. 实现: #include <bi ...
- SQL数据库学习,常用语句查询大全
数据库学习 sql server数据库基本概念 使用文件保存数据存在几个缺点: 1.文件的安全性问题: 2.文件不利于查询和对数据的管理: 3.文件不利于存放海量数据 4.文件在程序中控制不方便. 数 ...
- 两个已排序数组的合并-C语言
最近在纸上写一个已排序数组的合并时,花了超过预期的时间.仔细想想,这种要放到毕业找工作那会两下就出来了,原因还在于工作后对基础没有重视,疏于练习. 说开一点,现在搜索引擎的发达确实给问题的解决带来了便 ...
- 分类IP地址(A、B、C类)的指派范围、一般不使用的特殊IP地址
分类IP地址(A.B.C类)的指派范围.一般不使用的特殊IP地址 A类地址:0开头,8位网络号 B类地址:10开头,16位网络号 C类地址:110开头,24位网络号 D类地址:1110开头,多播地址 ...
- Makeflie自动生成依赖,自动化编译
在netbeans里开发,有一个重要文件makefile,是用来编译所有的文件. 项目的目录结构如下,扁平的目录结构,如何实现自动化编译,写makefile呢? 第一版 基础版: CC = g++ C ...
- VBA Promming——入门教程
VBA Visual Basic for Applications(VBA)是Visual Basic的一种宏语言,是微软开发出来在其桌面应用程序中执行通用的自动化(OLE)任务的编程语言.主要能用来 ...
- uva1614 Hell on the Markets
贪心部分的理论依据:前i个数可以凑出1-sum[i]的所有整数. 证明:第二类数学归纳,n=1时成立,假设n=k之前所有项都成立,当n=k+1时.sum[k+1]=sum[k]+a[k+1].只需证明 ...
- 【转】C#的版本
这年头啥东东都喜欢过段时间整个啥新版本出来.汽车,手机如此,软件就更是如此了啊.比如啥Iphone 4,Iphone 5,Windows 8,Oracle 12C,SQL Server 2010. 版 ...
- 日常[splay]:水题记——营业额统计
没错这就是让我深陷splay之中的罪魁祸首,昨天打了一下午结果发现是玄学错误的那个 人生第一棵splay平衡树 题目大意:求一段序列,小于当前元素的最大值和大于当前元素的最小值.从该元素前面的元素找. ...