Algorithm


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 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+1sr����+1…��. 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 ��� � (1pos|s|1≤���≤|�|c is lowercase Latin letter): replace spos���� with c (set spos:=c����:=�);
  • 2 l r2 � � (1lr|s|1≤�≤�≤|�|): 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 105105 lowercase Latin letters.

The second line of the input contains one integer q (1q1051≤�≤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
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

 

Code Examples

#1 Code Example with C++ Programming

Code - C++ Programming

#include <bits/stdc++.h>

using namespace std;

char c;
int q, ssqrt, t, pos, l, r;
string s;
vector<vector<int> > buckets;

void build() {
	for(int i = 0, j; i < s.length(); i += ssqrt) {
		j = i;
		vector<int> fr;
		for(int j = 0; j < 26; ++j)
			fr.push_back(0);
		while(j < s.length() && j < i + ssqrt)
			++fr[s[j++] - 'a'];
		buckets.push_back(fr);
	}
}

void update() {
	int bucket = (pos + ssqrt - 1) / ssqrt - 1;
	--buckets[bucket][s[pos - 1] - 'a'];
	s[pos - 1] = c;
	++buckets[bucket][s[pos - 1] - 'a'];
}

int get() {
	int lbucket = (l + ssqrt - 1) / ssqrt - 1 + 1;
	int rbucket = (r + ssqrt - 1) / ssqrt - 1 - 1;

	bool exist[26] = {false};
	if(lbucket < rbucket) {
		for(int i = lbucket; i <= rbucket; ++i)
			for(int j = 0; j < 26; ++j)
				exist[j] |= buckets[i][j] > 0;
		for(int i = l - 1; i < ssqrt * lbucket; ++i)
			exist[s[i] - 'a'] = true;
		for(int i = ssqrt * rbucket + ssqrt; i < r; ++i)
			exist[s[i] - 'a'] = true;
	} else {
		for(int i = l - 1; i < r; ++i)
			exist[s[i] - 'a'] = true;
	}

	int res = 0;
	for(int i = 0; i < 26; ++i)
		res += exist[i];
	return res;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("in", "r", stdin);
#endif

	cin >> s;
	ssqrt = sqrt(s.length());
	build();
	scanf("%d", &q);
	for(int i = 0; i < q; ++i) {
		scanf("%d", &t);
		if(t == 1) {
			scanf("%d %c", &pos, &c);
			update();
		} else {
			scanf("%d %d", &l, &r);
			printf("%d\n", get());
		}
	}

	return 0;
}
Copy The Code & Try With Live Editor

Input

x
+
cmd
abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7

Output

x
+
cmd
3
1
2
Advertisements

Demonstration


Codeforcess Solution D. Distinct Characters Queries-Solution in C, C++, Java, Python,Codeforcess Solution

Previous
Codeforces solution 1080-B-B. Margarite and the best present codeforces solution
Next
CodeChef solution DETSCORE - Determine the Score CodeChef solution C,C+