-
Notifications
You must be signed in to change notification settings - Fork 23
/
1234D.cpp
executable file
·68 lines (57 loc) · 1 KB
/
1234D.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
// Problem Code: 1234D
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class FenwickTree {
private:
vector<int> ft;
public:
FenwickTree(int n) : ft(n + 1) {}
int LSB(int x) {
return x & (-x);
}
void update(int i, int val) {
while (i < ft.size()) {
ft[i] += val;
i += LSB(i);
}
}
int rsq(int i) {
int sum = 0;
while (i != 0) {
sum += ft[i];
i -= LSB(i);
}
return sum;
}
int rsq(int i, int j) {
return rsq(j) - rsq(i - 1);
}
};
int main() {
int q;
string s;
cin >> s >> q;
vector<FenwickTree> ft(26, FenwickTree(s.length()));
for (int i = 0; i < s.length(); i++)
ft[s[i] - 'a'].update(i + 1, 1);
while (q--) {
char c;
int t, pos, l, r;
cin >> t;
if (t == 1) {
cin >> pos >> c;
ft[s[pos - 1] - 'a'].update(pos, -1);
ft[c - 'a'].update(pos, 1);
s[pos - 1] = c;
} else {
int cnt = 0;
cin >> l >> r;
for (int i = 0; i < ft.size(); i++)
cnt += (ft[i].rsq(l, r) > 0);
cout << cnt << endl;
}
}
return 0;
}