fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long int
  4. const int M = 1e9 + 7;
  5.  
  6. int main()
  7. {
  8. ios_base::sync_with_stdio(false);
  9. cin.tie(NULL);
  10. int n;
  11. cin >> n;
  12. string s;
  13. cin >> s;
  14. int q;
  15. cin >> q;
  16. map<int, set<int>> mp;
  17.  
  18. for (int i = 0; i < n; i++)
  19. {
  20. int ch = s[i] - 'a' + 1;
  21. mp[ch].insert(i + 1);
  22. }
  23. vector<int> ans;
  24. while (q--)
  25. {
  26. int type;
  27. cin >> type;
  28. if (type == 1)
  29. {
  30. int i, x;
  31. cin >> i >> x;
  32. int ch = s[i-1] - 'a' + 1;
  33. mp[ch].erase(i);
  34. mp[x].insert(i);
  35. s[i-1] = char(x+96);
  36. }
  37. else
  38. {
  39. int l, r;
  40. cin >> l >> r;
  41. int cnt = 0;
  42. for (auto it : mp)
  43. {
  44. set<int> st = it.second;
  45. auto lt = st.lower_bound(l);
  46. if (lt == st.end() || (*lt>r))
  47. continue;
  48.  
  49. cnt++;
  50. }
  51. ans.push_back(cnt);
  52. }
  53. }
  54.  
  55. for (auto it : ans)
  56. cout << it << " ";
  57. return 0;
  58. }
Success #stdin #stdout 0.01s 5312KB
stdin
7
abccbda
5
2 3 6
1 2 3
2 2 4
1 1 5
2 1 7
stdout
3 1 5