Codeforces Round #590 (Div. 3)

D. Distinct Characters Queries

Description

You are given a string ss consisting of lowercase Latin letters and qq queries for this string.

Recall that the substring s[l;r]s[l;r] of the string ss is the string slsl+1srslsl+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 c1 pos c (1pos|s|1≤pos≤|s|, cc is lowercase Latin letter): replace sposspos with cc (set spos:=cspos:=c);
  • 2 l r2 l r (1lr|s|1≤l≤r≤|s|): calculate the number of distinct characters in the substring s[l;r]s[l;r].

Input

The first line of the input contains one string ss consisting of no more than 105105 lowercase Latin letters.

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

The next qq 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

Input

flat
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7

Output

3
1
2

Correct solution:

Originally thought to be amended by the President with the tree, the tree together with the Chairman of the array tree.

Change template change for a long time have not seen.

Operating a: change a character

Operation II: count the number of interval [l, r] of different characters.

There are three ways:

 

26 letters Fenwick tree:

Every time you need for26 times, this letter is within the statistical range.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <set>
 6 #include <queue>
 7 #include <stack>
 8 #include <string>
 9 #include <cstring>
10 #include <vector>
11 #include <map>
12 //#include <unordered_map>
13 #define mem( a ,x ) memset( a , x ,sizeof(a) )
14 #define rep( i ,x ,y ) for( int i = x ; i<=y ;i++ )
15 #define lson  l ,mid ,pos<<1
16 #define rson mid+1 ,r ,pos<<1|1
17 using namespace std;
18 typedef long long ll ;
19 typedef pair<int ,int> pii;
20 typedef pair<ll ,int> pli;
21 const int inf = 0x3f3f3f3f;
22 const int N = 1e5+100;
23 const ll mod =1e9+7 ;
24 char s[N],kkk;
25 int n,m;
26 int aa,bb,cc;
27 int bit[30][N];
28 int bitsize(int x)
29 {
30     return x&(-x);
31 }
32 void update(int k,int id,int x)
33 {
34     while(id<=n)
35     {
36         bit[k][id]+=x;
37         id+=bitsize(id);
38     }
39 }
40 int query(int k,int id)
41 {
42     int ans=0;
43     while(id>0)
44     {
45         ans+=bit[k][id];
46         id-=bitsize(id);
47     }
48     return ans;
49 }
50 int main()
51 {
52     scanf("%s",s+1);
53     n=strlen(s+1);
54     for(int i=1;i<=n;i++)
55     {
56         update(s[i]-'a'+1,i,1);
57     }
58     scanf("%d",&m);
59     while(m--)
60     {
61         scanf("%d",&aa);
62         if(aa==1)
63         {
64             scanf("%d %c",&bb,&kkk);
65             update(s[bb]-'a'+1,bb,-1);
66             s[bb]=kkk;
67             update(s[bb]-'a'+1,bb,1);
68         }
69         else
70         {
71             scanf("%d%d",&bb,&cc);
72             int ans=0,res;
73             for(int i=1;i<=26;i++)
74             {
75                 res=query(i,cc)-query(i,bb-1);
76                 if(res>0)   ans++;
77             }
78             printf("%d\n",ans);
79         }
80     }
81 
82     return 0;
83 }
View Code

 

26 letters of the tree line:

 

Statistics for each letter of the index. To quickly delete a set of elements added.

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <set>
 6 #include <queue>
 7 #include <stack>
 8 #include <string>
 9 #include <cstring>
10 #include <vector>
11 #include <map>
12 //#include <unordered_map>
13 #define mem( a ,x ) memset( a , x ,sizeof(a) )
14 #define rep( i ,x ,y ) for( int i = x ; i<=y ;i++ )
15 #define lson  l ,mid ,pos<<1
16 #define rson mid+1 ,r ,pos<<1|1
17 using namespace std;
18 typedef long long ll ;
19 typedef pair<int ,int> pii;
20 typedef pair<ll ,int> pli;
21 const int inf = 0x3f3f3f3f;
22 const int N = 1e5+100;
23 const ll mod =1e9+7 ;
24 char s[N],kkk;
25 int n,m;
26 int aa,bb,cc;
27 set<int>st[30];
28 set<int>::iterator it;
29 int main()
30 {
31     scanf("%s",s+1);
32     n=strlen(s+1);
33     for(int i=1;i<=n;i++)
34         st[s[i]-'a'+1].insert(i);
35     scanf("%d",&m);
36     while(m--)
37     {
38         scanf("%d",&aa);
39         if(aa==1)
40         {
41             scanf("%d %c",&bb,&kkk);
42             st[s[bb]-'a'+1].erase(bb);
43             s[bb]=kkk;
44             st[s[bb]-'a'+1].insert(bb);
45         }
46         else
47         {
48             scanf("%d%d",&bb,&cc);
49             int ans=0;
50             for(int i=1;i<=26;i++)
51             {
52                 it=st[i].lower_bound(bb);
53                 if(it==st[i].end()) continue;
54                 if((*it)<=cc)
55                     ++ years ;
56              }
 57              printf ( " % d \ n " , year);
58          }
 59      }
 60  
61      return  0 ;
62 }
View Code

 

Guess you like

Origin www.cnblogs.com/Kaike/p/11617314.html