Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树,二进制)

Codeforces Round #590 (Div. 3) D. Distinct Characters Queries(线段树,二进制)

D. Distinct Characters Queries
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given a string s consisting of lowercase Latin letters and q queries for this string.

Recall that the substring s[l;r] of the string s is the string slsl+1…sr. For example, the substrings of “codeforces” are “code”, “force”, “f”, “for”, but not “coder” and “top”.

There are two types of queries:

1 pos c (1≤pos≤|s|, c is lowercase Latin letter): replace spos with c (set spos:=c);
2 l r (1≤l≤r≤|s|): calculate the number of distinct characters in the substring s[l;r].
Input
The first line of the input contains one string s consisting of no more than 105 lowercase Latin letters.

The second line of the input contains one integer q (1≤q≤105) — the number of queries.

The next q lines contain queries, one per line. Each query is given in the format described in the problem statement. It is guaranteed that there is at least one query of the second type.

Output
For each query of the second type print the answer for it — the number of distinct characters in the required substring in this query.

Examples
inputCopy
abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7
outputCopy
3
1
2
inputCopy
dfcbbcfeeedbaea
15
1 6 e
1 4 b
2 6 14
1 7 b
1 12 c
2 6 8
2 1 6
1 7 c
1 2 f
1 10 a
2 7 9
1 10 a
1 14 b
1 1 f
2 1 11
outputCopy
5
2
5
2
6
题意 给一个字符串
2 查询 l 到 r 出现的不同字符数
1 将 位置 pos 修改为字母
思路:
用二进制位保存该字母是否出现过 按位与 即为区间合并操作(想到的话就很好写 ,线段出题方式真多)

#include<iostream>
#include<cstdio>
#include<map>
#include<math.h>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+7;
const int mod=1e9+7;
ll sum[maxn<<2];
string str;
void pushup(ll rt){
      sum[rt]=sum[rt<<1]|sum[rt<<1|1];
}
void build (ll l, ll r,ll rt){
     if(l==r){
 
        sum[rt]=1<<(str[l-1]-'a');

        return ;
     }
     ll m=(l+r)>>1;
     build(l,m,rt<<1);
     build(m+1,r,rt<<1|1);
     pushup(rt);
}
void update(ll index,char c,ll l, ll r,ll rt){
     if(l==r){
        sum[rt]=1<<(c-'a');
        return ;
     }
     ll m=(l+r)>>1;
     if(index<=m) update(index,c,l,m,rt<<1);
     else         update(index,c,m+1,r,rt<<1|1);
     pushup(rt);
}
ll query(ll L,ll R,ll l, ll r,ll rt){
    ll s=0;
    if(L<=l&&R>=r){
        return sum[rt];
    }
    ll m=(l+r)>>1;
    if(L<=m) s|=query(L,R,l,m,rt<<1);
    if(R>m)  s|=query(L,R,m+1,r,rt<<1|1);
    return s;
}
int main (){
     cin>>str;
     ll len=str.size();
     build(1,len,1);
     int k;
     cin>>k;

     for(int i=0;i<k;i++){
        ll n,a,b;
        char c;
        scanf("%lld",&n);
        if(n==2){
            ll ans=0;
            scanf("%lld%lld",&a,&b);
            ll temp=query(a,b,1,len,1);
            for(int i=27;i>=0;i--){
                if((temp>>i)&1) ans++;
            }
            printf ("%lld\n",ans);
        }
        else {
            cin>>a>>c;
            update(a,c,1,len,1);
        }
 
     }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值