cf1234 Round #590 Div3-D【二维树状数组】

Date:2021.01.06

题意:
操作1:将第pos个位置改为字母c
操作2:计算[l,r]中字母种类数

思路:建26个BIT,编号0-25。设原序列为s,操作1即为在第s[pos]-‘a’个BIT中将第pos个位置-1;再在第c-'a’个BIT中将第pos个位置+1。操作2即为统计26棵BIT上有几个在[l,r]上的和>0。【注意BIT下标从1开始,别忘了初始化。】
代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1e5+10;
typedef long long LL;
LL n,t,tr[26][N];
char c[N];
LL lowbit(LL x)
{
    return x&-x;
}
void add(LL x,LL c,LL *tr)
{
    for(int i=x;i<=n;i+=lowbit(i)) tr[i]+=c; 
}
LL sum(LL x,LL *tr)
{
    LL res=1;
    for(int i=x;i;i-=lowbit(i)) res+=tr[i];
    return res;
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    cin>>c+1;n=strlen(c+1);
    for(int i=1;i<=n;i++) add(i,1,tr[c[i]-'a']);
    cin>>t;
    while(t--)
    {
        int op,l,r;char q;cin>>op;
        if(op==1)
        {
            cin>>l>>q;
            add(l,-1,tr[c[l]-'a']);
            add(l,1,tr[q-'a']);
            c[l]=q;
        }
        else
        {
            LL ans=0;
            cin>>l>>r;
            for(int i=0;i<26;i++)
            {
                LL ss=sum(r,tr[i])-sum(l-1,tr[i]);
                if(ss>0) ans++;
            }
            cout<<ans<<endl;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值