hdu 4046 Panda 线段树

五场的网络赛的题目出现了大量的线段树的题目,每次都只能坐在那里干瞪眼。。

看来需要好好的啃啃线段树了。。

 

#include<iostream>
#include<string> 
#define maxn 50010 
#define lson l , m , rt << 1 
#define rson m + 1 , r , rt << 1 | 1 
using namespace std; 
int sum[maxn*4]; 
char str[maxn]; 
int a[maxn]; 
void PushUP(int rt)
 { 
    sum[rt] = sum[rt<<1] + sum[rt<<1|1]; 

void build(int l,int r,int rt)
 { 
    if (l == r) { 
        sum[rt]=a[l]; 
        return ; 
    } 
    int m = (l + r) >> 1; 
    build(lson); 
    build(rson); 
    PushUP(rt); 

void update(int p,int add,int l,int r,int rt)
 { 
    if (l == r)
 { 
        sum[rt] = add; 
        return ; 
    } 
    int m = (l + r) >> 1; 
    if (p <= m) update(p , add , lson); 
    else update(p , add , rson); 
    PushUP(rt); 

int query(int L,int R,int l,int r,int rt)  

    if (L <= l && r <= R) { 
        return sum[rt]; 
    } 
    int m = (l + r) >> 1; 
    int ret = 0; 
    if (L <= m) ret += query(L , R , lson); 
    if (R > m) ret += query(L , R , rson); 
    return ret; 

int main() 

    int cas,T=0,n,m,r,l,c; 
    char st; 
    scanf("%d",&cas); 
    while(cas--) 
    { 
        scanf("%d %d",&n,&m); 
        scanf("%s",str); 
        memset(a,0,sizeof(a)); 
        printf("Case %d:\n",++T); 
        for(int i=2;i<n;i++)//标记以该下标结尾的长度为3的子串是否符合要求 
        { 
            if(str[i]!='w') 
                a[i]=0; 
            else if(str[i-1]=='b'&&str[i-2]=='w') 
                a[i]=1; 
        } 
        build(0,n,1); 
        while(m--) 
        { 
            scanf("%d",&c); 
            if(c==0) 
            { 
                scanf("%d %d",&l,&r); 
    l+=2; 
                if(l>r) printf("0\n"); 
                else 
     printf("%d\n",query(l,r,0,n,1)); 
            } 
            else 
            { 
                scanf("%d %c",&l,&st); 
                if(str[l]==st)//若要就该的字符跟原先相同,则不用修改了 
                    continue; 
                str[l]=st; 
                if(l>=2&&str[l-2]=='w'&&str[l-1]=='b'&&str[l]=='w') 
                { 
                    if(a[l]==0)//若改之前是不符合要求的串,则更新 
                        update(l,1,0,n,1); 
                    a[l]=1; 
                } 
                else if(l>=2&&a[l]==1)//若改之前是符合要求的串,则更新 
                { 
                    update(l,0,0,n,1); 
                    a[l]=0; 
                } 
                if(l>=1&&str[l]=='b'&&str[l-1]=='w'&&str[l+1]=='w') 
                { 
                    if(a[l+1]==0) 
                        update(l+1,1,0,n,1); 
                    a[l+1]=1; 
                } 
                else if(l<n-1&&l>=1&&a[l+1]==1) a[l+1]=0,update(l+1,0,0,n,1); 
                if(str[l]=='w'&&str[l+1]=='b'&&str[l+2]=='w') 
                { 
                    if(a[l+2]==0) 
                        update(l+2,1,0,n,1); 
                    a[l+2]=1; 
                } 
                else
     if(l<n-2&&a[l+2]==1)
      a[l+2]=0,update(l+2,0,0,n,1); 
            } 
        } 
    } 
    return 0; 
}

转载于:https://www.cnblogs.com/Mu-Tou/archive/2011/09/21/2184445.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值