CodeForces Round #590 (Div 3)

1361 (+46), pupil
Rank: 1912(复兴号列车即将开动)

分析:树状数组题(比赛的时候忘了树状数组咋写了。。。)

代码:

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 1e5 + 10;
 4 char s[maxn];
 5 int c[maxn][30];
 6 int len;
 7 
 8 int lowbit(int x)
 9 {
10     return x & (-x);
11 }
12 
13 void ini()
14 {
15     for(int i = 1; i <= len; i++)
16         for(int j = i; j >= i - lowbit(i) + 1; j--)
17             c[i][s[j] - 'a' + 1]++;
18 }
19 
20 void update(int pos, char tar)
21 {
22     for(int i = pos; i <= len; i += lowbit(i))
23     {
24         c[i][s[pos] - 'a' + 1]--;
25         c[i][tar - 'a' + 1]++;
26     }
27     s[pos] = tar;
28 }
29 int sum(int a, int b)
30 {
31     int res[30];
32     memset(res, 0, sizeof(res));
33     int ans = 0;
34     for(int i = b; i >= 1; i -= lowbit(i))
35         for(int j = 1; j <= 26; j++)
36             res[j] += c[i][j];
37     for(int i = a; i >= 1; i -= lowbit(i))
38         for(int j = 1; j <= 26; j++)
39             res[j] -= c[i][j];
40     for(int i = 1; i <= 26; i++)
41         if(res[i]) ans++;
42     return ans;
43 }
44 
45 int main()
46 {
47     int ope;
48     cin >> (s + 1);
49     len = strlen(s + 1);
50     int n; cin >> n;
51     ini();
52     while(n--)
53     {
54         scanf("%d", &ope);
55         if(ope == 1)
56         {
57             int pos; scanf("%d", &pos);
58             char x[10]; scanf("%s", x);
59             update(pos, x[0]);
60         }
61         else
62         {
63             int a, b;
64             scanf("%d%d", &a, &b);
65             cout << sum(a - 1, b) << endl;
66         }
67     }
68 }

猜你喜欢

转载自www.cnblogs.com/liuwenhan/p/11622379.html