D. Distinct Characters Queries
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
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+1…srslsl+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 (1≤pos≤|s|1≤pos≤|s|, cc is lowercase Latin letter): replace sposspos with cc (set spos:=cspos:=c);
- 2 l r2 l r (1≤l≤r≤|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 (1≤q≤1051≤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
Copy
abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7
output
Copy
3
1
2
input
Copy
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
output
Copy
5
2
5
2
6
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
char[] chs;
void run(){
work();
out.flush();
}
int sqrt(long n) {
long l=0,r=n;
while(l<r) {
long m=(l+r)/2;
if(m*m<n) {
l=m+1;
}else {
r=m;
}
}
return (int)l;
}
long mod=1000000007;
long gcd(long a,long b) {
return b==0?a:gcd(b,a%b);
}
void work() {
//分块
String s=in.next();
chs=s.toCharArray();
int n=chs.length;
int d=sqrt(n);
int[] div=new int[d];
for(int i=0,p=0;p<n;i++) {
int v=0;
for(int j=0;j<d&&p<n;j++,p++) {
v|=(1<<(chs[p]-'a'));
}
div[i]=v;
}
int q=in.nextInt();
for(int i=0;i<q;i++) {
int t=in.nextInt();
if(t==1) {
int index=in.nextInt()-1;
char c=in.next().charAt(0);
chs[index]=c;
int p=index/d;
int v=0;
for(int j=p*d;j<n&&j<(p+1)*d;j++) {
v|=(1<<(chs[j]-'a'));
}
div[p]=v;
}else {
int l=in.nextInt()-1;
int r=in.nextInt()-1;
int p1=l/d;
int p2=r/d;
int v=0;
for(int j=p1+1;j<=p2-1;j++) {//中间
v|=div[j];
}
if(p1==p2) {//在同一个div
for(int j=l;j<=r;j++) {//左边
v|=(1<<(chs[j]-'a'));
}
}else {
for(int j=l;j<(p1+1)*d&&j<n;j++) {//左边
v|=(1<<(chs[j]-'a'));
}
for(int j=p2*d;j<=r;j++) {//右边
v|=(1<<(chs[j]-'a'));
}
}
int ret=0;
for(int j=0;j<26;j++) {
if(((1<<j)&v)>0) {
ret++;
}
}
out.println(ret);
}
}
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public String next()
{
if(st==null || !st.hasMoreElements())
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
}