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);
        }
 
     }

}
 
发布了11 篇原创文章 · 获赞 1 · 访问量 392

猜你喜欢

转载自blog.csdn.net/hddddh/article/details/104942438