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
中文题意:
给定一个字符串 S,然后有 q 组查询,每次查询为 "1" 时,将 s[pos] 替换为 c;查询为 "2" 时,输出从 l 到 r 区间,s[l...r] 这个子串里的不同字符的个数。
分析:
每一位都取往左移当前对应的数字,从叶子结点往上结点为子节点的或运算,当最后求的某区间对应的数值后,再与26位字母对应的数字与运算,如果不为0,说明该区间有这个数字。
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#pragma warning(disable:4996)
using namespace std;
const int maxn = 100005;
typedef long long ll;
ll tree[maxn<<2];
char s[maxn];
int a[maxn];
void pushup(int rt) {
tree[rt] = tree[rt << 1] | tree[rt << 1 | 1];
}
void build(int l, int r, int rt) {
if (l == r) {
tree[rt] = ll(1 << a[l]);
return;
}
int mid = (l + r) >> 1;
build(l, mid, rt << 1);
build(mid + 1, r, rt << 1 | 1);
pushup(rt);
}
void update(int l, int r, int c, int val, int rt) {
if (l == r) {
tree[rt] = (1ll << val);
return;
}
int mid = (l + r) >> 1;
if (c <= mid) {
update(l, mid, c, val, rt << 1);
}
else {
update(mid + 1, r, c, val, rt << 1 | 1);
}
pushup(rt);
}
ll query(int l, int r, int L, int R, int rt) {
if (L <= l && R >= r) {
return tree[rt];
}
ll ans = 0;
int mid = (l + r) >> 1;
if (L <= mid) {
ans |= query(l, mid, L, R, rt << 1);
}
if (R > mid) {
ans |= query (mid + 1, r, L, R, rt << 1 | 1);
}
return ans;
}
int main() {
cin >> s+1;
int len = strlen(s+1);
int n;
scanf("%d", &n);
for (int i = 1; i <= len; i++) {
a[i] = s[i] - 'a';
}
build(1, len, 1);
while (n--) {
int op;
cin >> op;
if (op == 1) {
int x;
char c[20];
cin >> x >> c;
int val = c[0] - 'a';
update(1, len, x, val, 1);
}
else {
int l, r;
cin >> l >> r;
ll ans = query(1, len, l, r, 1);
int cnt = 0;
for (int i = 0; i < 30; i++) {
if ((1 << i)&ans) {
cnt++;
}
}
printf("%d\n", cnt);
}
}
return 0;
}