【树状数组】The 36th ACM/ICPC Asia Regional Beijing Site Online Contest - G Panda

 http://cnc.boj.me/onlinejudge/newoj/showProblem/show_problem.php?problem_id=217

其他人都说用线段树,可是自己敲着敲着就没想法,只好转向树状数组,发现可以实现,貌似比别人的线段树快。

 

方法:一直维护wbw中第一个w,把它记为1,然后整个数组更新。求和比较头疼,第一是下标要加1,第二是很容易求错。

后来想了想,发现只要sum(r-2)-sum(l-1),其中l和r都转化为以1为开始下标。

#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <string>
#include <time.h>
#include <cstdio>
#include <math.h>
#include <iomanip>
#include <cstdlib>
#include <limits.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;

#define LL long long
#define PI acos(-1.0)
#define MAX INT_MAX
#define MIN INT_MIN
#define FRE freopen("a.txt","r",stdin)
#define N 50005
#define MOD 10007

int c[N];
char str[N];
int lowbit(int t){
    return t&(-t);
}
void update(int pos,int v){
    while(pos<N){
        c[pos]+=v;
        pos+=lowbit(pos);
    }
}
int sum(int pos){
    int tot=0;
    while(pos){
        tot+=c[pos];
        pos-=lowbit(pos);
    }
    return tot;
}
int main(){
    int tt;
    scanf("%d",&tt);
    int ca=1;
    while(tt--){
        int n,m;
        scanf("%d%d",&n,&m);
        int i,j;
        scanf("%s",str);
        memset(c,0,sizeof(c));
        for(i=0;i+2<n;i++){
            if(str[i]=='w' && str[i+1]=='b' && str[i+2]=='w'){
                    update(i+1,1);
                }
        }
        printf("Case %d:\n",ca++);
        while(m--){
            int a;
            scanf("%d",&a);
            if(!a){
                int s,t;
                scanf("%d%d",&s,&t);
                if(t-s+1<=2)printf("%d\n",0);
                else{
                    int l=s+1;
                    int r=t+1;
                    printf("%d\n",sum(r-2)-sum(l-1));
                }
            }
            else{
                int pos;
                char cc;
                scanf("%d %c",&pos,&cc);
                int s;
                int t;
                s=(pos-2>=0) ? pos-2 : 0 ;
                t=(pos+2<n) ? pos+2 : n-1 ;
                for(i=s;i+2<=t;i++)
                if(str[i]=='w' && str[i+1]=='b' && str[i+2]=='w'){
                    update(i+1,-1);
                }
                str[pos]=cc;
                for(i=s;i+2<=t;i++)
                    if(str[i]=='w' && str[i+1]=='b' && str[i+2]=='w'){
                        update(i+1,1);
                    }
                }
            }
        }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值