HDUOJ--4046--Panda【线段树】

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4046

题意:题目先来了一大段情书,看的我莫名其妙,然后来了一段极像《那些年》歌词翻译的正文,然后才是真正的题目。给你一个字符串,最长50000,只有两种字母,w和b。然后m个操作,操作有两种,第一种是查询l,r区间中有几个字符串是”wbw“形式,第二种操作是把字符串下标k的字符变成输入的字符c。


一眼就能看出是线段树,但是怎么做?我觉得线段树写起来就那样,偶尔需要一些小技巧,多加些节点信息,或者和离散化结合起来。但是线段树最难的地方我觉得还不是这些,而是节点存储什么信息,明白了这个才能着手敲线段树,并且其他步骤就和裸线段树差不多了。我参考着这个博客的题来做线段树:http://blog.csdn.net/shiqi_614/article/details/8228102,有一些题目是基础的,都没问题,我不会做的要么就是英文没读懂,要么就是不知道节点存储什么信息,所以对于我来说,节点存什么,是线段树中最难的点。


这道题也一样,我想了很久,今天下午去踢球的路上想到一种方式:如果是w就用3代替,b就用5代替,再乘它所在的位数,比如”wbw“ 就是 1*3+2*5+3*3 = 22,这样wbw的数字是唯一的,存入线段树中,至于父节点的pushup,如果子节点%22为0再加入到父节点的值中,最后搜到的区间值除以22就是wbw的个数,wbw是三个字母,所以只有两个字母时肯定是不行的,r-l<2肯定也不行。查询的时候把左区间+2,再查询。

更新的时候需要考虑三种情况,当前k位置是第一个字母、第二个字母、第三个字母的情况。


更新位置逗比的写成了k,调试了半天没出样例。。。加了一堆输出中间量,终于发现写逗了。。。改之AC

#include<cstring>
#include<string>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cctype>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<ctime>
#include<cstdlib>
#include<functional>
#include<cmath>
using namespace std;
#define PI acos(-1.0)
#define MAXN 50100
#define eps 1e-7
#define INF 0x7FFFFFFF
#define seed 131
#define mod 1000000007
#define ll long long
#define ull unsigned ll
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

int sum[MAXN<<2];
int a[MAXN];
int n;
char s[MAXN];
void pushup(int rt){
    sum[rt] = (sum[rt<<1]%22==0?sum[rt<<1]:0) + (sum[rt<<1|1]%22==0?sum[rt<<1|1]:0);
}
void build(int l,int r,int rt){
    if(l==r){
        sum[rt] = a[l];
//        cout<<rt<<" aaa"<<a[l]<<endl;
        return ;
    }
    int m = (l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void update(int k,int c,int l,int r,int rt){
    if(l==r){
        sum[rt] = c;
        return ;
    }
    int m = (l+r)>>1;
    if(k<=m) update(k,c,lson);
    else    update(k,c,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
    if(L<=l&&r<=R){
//        cout<<rt<<" "<<sum[rt]<<endl;
        if(sum[rt]%22)  return 0;
        else    return sum[rt];
    }
    int m = (l+r)>>1;
    ll ret = 0;
    if(L<=m) ret += query(L,R,lson);
    if(R>m)    ret += query(L,R,rson);
    return ret;
}
int main(){
    int t,i,j,aa,b,c,k=1;
    int m;
    char str[5];
    scanf("%d",&t);
    while(t--){
        memset(a,0,sizeof(a));
        memset(sum,0,sizeof(sum));
        scanf("%d%d",&n,&m);
        scanf("%s",s);
        //cout<<s<<endl;
        int l = strlen(s);
        for(i=2;i<l;i++){
            a[i] = (s[i-2]=='w'?3:5)*1+(s[i-1]=='w'?3:5)*2+(s[i]=='w'?3:5)*3;
        }
        build(0,n,1);
//        for(i=0;i<n;i++){
//            cout<<a[i]<<" ";
//        }
//        cout<<endl;
        printf("Case %d:\n",k++);
        while(m--){
//            cout<<s<<endl;
//            for(i=0;i<n;i++){
//                cout<<sum[8]<<" "<<sum[9]<<" "<<sum[5]<<" "<<sum[12]<<" "<<sum[13]<<endl;
//            }
            scanf("%d",&aa);
            if(aa==0){
                scanf("%d%d",&b,&c);
                b += 2;
                if(b>c) puts("0");
                else    printf("%d\n",query(b,c,0,n,1)/22);
            }
            else{
                scanf("%d%s",&b,str);
                if(str[0]==s[b])    continue;
                s[b] = str[0];
                if(b>=2){
                    a[b] = (s[b-2]=='w'?3:5)*1+(s[b-1]=='w'?3:5)*2+(s[b]=='w'?3:5)*3;
                    update(b,a[b],0,n,1);
                }
                if(b>=1&&b<n-1){
                    a[b+1] = (s[b-1]=='w'?3:5)*1+(s[b]=='w'?3:5)*2+(s[b+1]=='w'?3:5)*3;
                    update(b+1,a[b+1],0,n,1);
                }
                if(b<n-2){
                    a[b+2] = (s[b]=='w'?3:5)*1+(s[b+1]=='w'?3:5)*2+(s[b+2]=='w'?3:5)*3;
                    update(b+2,a[b+2],0,n,1);
                }
            }
        }

    }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值