-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Unique Characters in String.cpp
85 lines (77 loc) · 1.73 KB
/
Unique Characters in String.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//Problem Link and Solution : https://codeforces.com/contest/1234/submission/61672539
//Codeforces D
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define FR(i,j,k) for(int i=j;i<k;++i)
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
const int MAX = 1e5+5;
const int MOD = 1e9+7;
using namespace std;
string arr;
vector<vector<int>> T(4*MAX, vector<int>(26));
void build(int node,int s,int e){
if(s==e){
T[node][arr[s]-'a']++;
return;
}
int m=(s+e)>>1;
build(node<<1,s,m);
build(node<<1|1,m+1,e);
FR(i,0,26)T[node][i]=T[node<<1][i] + T[node<<1|1][i];
}
vector<int> query(int node,int s,int e,int l,int r){
vector<int> a(26);
if(e<l || s>r) return a; //no overlap
if(l<=s && r>=e) return T[node];
int mid = (s+e)>>1;
vector<int> a1 = query(node<<1,s,mid,l,r);
vector<int> a2 = query(node<<1|1,mid+1,e,l,r);
FR(i,0,26)a[i] = a1[i] + a2[i];
return a;
}
void upd(int node,int s,int e,int p,char new_val){
if(s == e){
T[node][arr[p] - 'a'] --;
T[node][new_val - 'a'] ++;
arr[p]=new_val;
}
else
{
int mid = (s+e)>>1;
if(p <= mid)
{
upd(node<<1, s, mid, p, new_val);
}
else
{
upd(node<<1|1, mid+1, e, p, new_val);
}
FR(i,0,26){T[node][i]=T[node<<1][i] + T[node<<1|1][i];}
}
}
int32_t main() {
IOS;
cin>>arr;
int ln=arr.length();
build(1,0,ln-1);
int q;
cin>>q;
while(q--){
int x;
cin>>x;
if(x==1){
int p;char ch;
cin>>p>>ch;
upd(1,0,ln-1,p-1,ch);
}else{
int l,r;
cin>>l>>r;
vector<int> res = query(1,0,ln-1,l-1,r-1);
int an=0;
FR(i,0,26){if(res[i]>0)an++;}
cout<<an<<endl;
}
}
return 0;
}