HDU4046 Panda 线段树 单点更新

博客介绍了如何利用线段树解决一个关于字符串'wbw'查询和单点更新的问题。给定一个只包含'b'和'w'的字符串,任务是查询特定范围内的'wbw'子串数量,并能进行单个字符的替换。解题思路是建立线段树,叶节点存储从当前位置到末尾是否能构成'wbw',父节点值为子节点值之和,特殊处理字符串长度小于3的情况。
摘要由CSDN通过智能技术生成

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

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的只含有w和b的字符串,有两种命令:
命令1 查询a-b中有几个”wbw”
命令2 将第a个字符换成b

解题思路

可以使用线段树来求解,叶节点的意义是从此处加上后面的两个是否构成”wbw”,构成就是1,构不成就是0.然后线段树中父节点等于两个叶子结点的和。初始化用0填充线段树。
需要注意字符串长度小于3时需要特判!

代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 50010;
char s[maxn];
int chk[maxn];
int segTree[maxn<<2];
int len;
int kase = 0;
void build(int l,int r,int node)
{
    if(l == r) {
        segTree[node] = chk[l];
        return;
    }
    build(l,(l+r)/2,node<<1);
    build((l+r)/2+1,r,(node<<1)+1);
    segTree[node] = segTree[node<<1] + segTree[(node<<1)+1];
}
int query(int a,int b,int l,int r,int node)
{
    if(r < a || l > b) return 0;
    if(a <= l && r <= b) return segTree[node];
    return query(a,b,l,(l+r)/2,node<<1)+query(a,b,(l+r)/2+1,r,(node<<1)+1);
}
void update(int x,int status,int l,int r,int node)
{
    if(l == r && l == x) {
        segTree[node] = status;
        return;
    }
    if(x <= (l+r)/2) update(x,status,l,(l+r)/2,node<<1);
    else update(x,status,(l+r)/2+1,r,(node<<1)+1);
    segTree[node] = segTree[node<<1] + segTree[(node<<1)+1];
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--) {
        memset(chk,0,sizeof chk);
        for(int i = 0 ; i < (maxn<<2) ; i ++) segTree[i] = 0;
        int q;
        scanf("%d%d",&len,&q);
        scanf("%s",s+1);
        if(len < 3) {
            printf("Case %d:\n",++kase);
            while(q--) {
                char s1[5],s2[5],s3[5];
                scanf("%s%s%s",s1,s2,s3);
                if(s1[0] == '0') printf("0\n");
            }
            continue;
        }
        for(int i = 1 ; i <= len-2 ; i ++) {
            if(s[i] == 'w' && s[i+1] == 'b' && s[i+2] == 'w') chk[i] = 1;
        }
        build(1,len-2,1);
        printf("Case %d:\n",++kase);
        while(q--) {
            int cmd;
            scanf("%d",&cmd);
            if(cmd == 1) {//更新
                int x;
                char p;
                scanf("%d %c",&x,&p);
                x ++;
                if(s[x] == p) continue;
                s[x] = p;
                if(x == 1){
                    if(s[x] == 'w' && s[x+1] == 'b' && s[x+2] == 'w') {
                        update(x,1,1,len-2,1);
                    }else {
                        update(x,0,1,len-2,1);
                    }
                }else if(x == 2) {
                    if(s[x-1] == 'w' && s[x] == 'b' && s[x+1] == 'w') {
                        update(x-1,1,1,len-2,1);
                    }else {
                        update(x-1,0,1,len-2,1);
                    }
                    if(s[x] == 'w' && s[x+1] == 'b' && s[x+2] == 'w') {
                        update(x,1,1,len-2,1);
                    }else {
                        update(x,0,1,len-2,1);
                    }
                }else if(x == len) {
                    if(s[x-2] == 'w' && s[x-1] == 'b' && s[x] == 'w') {
                        update(x-2,1,1,len-2,1);
                    }else {
                        update(x-2,0,1,len-2,1);
                    }
                }else if(x == len-1) {
                    if(s[x-1] == 'w' && s[x] == 'b' && s[x+1] == 'w') {
                        update(x-1,1,1,len-2,1);
                    }else {
                        update(x-1,0,1,len-2,1);
                    }
                    if(s[x-2] == 'w' && s[x-1] == 'b' && s[x] == 'w') {
                        update(x-2,1,1,len-2,1);
                    }else {
                        update(x-2,0,1,len-2,1);
                    }
                }else {
                    if(s[x-2] == 'w' && s[x-1] == 'b' && s[x] == 'w') {
                        update(x-2,1,1,len-2,1);
                    }else {
                        update(x-2,0,1,len-2,1);
                    }
                    if(s[x-1] == 'w' && s[x] == 'b' && s[x+1] == 'w') {
                        update(x-1,1,1,len-2,1);
                    }else {
                        update(x-1,0,1,len-2,1);
                    }
                    if(s[x] == 'w' && s[x+1] == 'b' && s[x+2] == 'w') {
                        update(x,1,1,len-2,1);
                    }else {
                        update(x,0,1,len-2,1);
                    }
                }
            }else {//查询
                int a,b;
                scanf("%d%d",&a,&b);
                printf("%d\n",query(a+1,b-1,1,len-2,1));
            }
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值