1234D.Distinct Characters Queries (tree array)

You have a lowercase Latin alphabet composed of a string s and q to query this string.

 

Recall that the substring s [l; r] of the string s is the string slsl + 1 ... sr. For example, the substring of "codeforce" is "code", "force", "f", "For", not "coder" and "top".

 

There are two types of queries:

 

1 pos c (1≤pos≤ | s |, c is a lowercase Latin letter): replace spos with c (set spos: = c);

2 lr (1≤l≤r≤ | s |): Calculate the number of different characters in the substring s [l; r].

 

answer:

Open 26 tree arrays, store the prefix of 26 letters.

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+100;
string s;
int c[26][maxn];
int len;
int lowbit (int x) {
    return x&-x;
}
void update (int id,int x,int val) {
    for (int i=x;i<=len;i+=lowbit(i)) c[id][i]+=val;
}
int getsum (int id,int x) {
    int ans=0;
    for (int i=x;i;i-=lowbit(i)) ans+=c[id][i];
    return ans;
}
int main () {
    cin>>s;
    int N;
    scanf("%d",&N);
    len=s.length();
    for (int i=1;i<=len;i++) 
        update(s[i-1]-'a',i,1);
    for (int i=1;i<=N;i++) {
        int f;
        scanf("%d",&f);
        if (f==1) {
            int x;
            char ch;
            scanf("%d %c",&x,&ch);
            update(s[x-1]-'a',x,-1);
            update(ch-'a',x,1);
            s[x-1]=ch;
        }
        else {
            int l,r;
            scanf("%d%d",&l,&r);
            int ans=0;
            for (int j=0;j<26;j++) {
                if (getsum(j,r)-getsum(j,l-1)>=1) ++ 
                    years ;
            }
            printf("%d\n",ans);
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/zhanglichen/p/12687506.html