Advertisement
a53

StringQuery_Of

a53
May 16th, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. /// Solutie - Moca Andrei - 100p
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4. const int Dim(26);
  5. int n, q, op, x, y, res, p2[Dim];
  6. char ch;
  7. string s;
  8. class AINT {
  9. public:
  10. AINT() {}
  11. AINT(const int& _n) : n(_n) {
  12. arb = vector<int>(4 * n);
  13. Build(1, 1, n);
  14. }
  15. inline void Update(const int& pos, const int& val) {
  16. Update(1, 1, n, pos, val);
  17. }
  18. inline int Query(const int& st, const int& dr) {
  19. return Query(1, 1, n, st, dr);
  20. }
  21. private:
  22. inline void Build(int node, int st, int dr) {
  23. if (st == dr) {
  24. arb[node] = p2[s[st - 1] - 'a'];
  25. return;
  26. }
  27. int mid = (st + dr) / 2;
  28. Build(2 * node, st, mid);
  29. Build(2 * node + 1, mid + 1, dr);
  30. arb[node] = (arb[2 * node] | arb[2 * node + 1]);
  31. }
  32. inline void Update(int node, int st, int dr, const int& pos, const int& val) {
  33. if (st == dr) {
  34. arb[node] = val;
  35. return;
  36. }
  37. int mid = (st + dr) / 2;
  38. if (pos <= mid)
  39. Update(2 * node, st, mid, pos, val);
  40. else Update(2 * node + 1, mid + 1, dr, pos, val);
  41. arb[node] = (arb[2 * node] | arb[2 * node + 1]);
  42. }
  43. inline int Query(int node, int st, int dr, const int& l, const int& r) {
  44. if (l <= st && dr <= r)
  45. return arb[node];
  46. int mid = (st + dr) / 2, r1(0), r2(0);
  47. if (l <= mid)
  48. r1 = Query(2 * node, st, mid, l, r);
  49. if (mid < r)
  50. r2 = Query(2 * node + 1, mid + 1, dr, l, r);
  51. return (r1 | r2);
  52. }
  53. private:
  54. int n;
  55. vector<int> arb;
  56. };
  57. int main() {
  58. ios_base::sync_with_stdio(false);
  59. cin.tie(0), cout.tie(0);
  60. p2[0] = 1;
  61. for (int i = 1; i < Dim; ++i)
  62. p2[i] = p2[i - 1] * 2;
  63. cin >> n >> s >> q;
  64. AINT ai(n);
  65. for (int i = 1; i <= q; ++i) {
  66. cin >> op >> x;
  67. if (op == 1) {
  68. cin >> ch;
  69. ai.Update(x, p2[ch - 'a']);
  70. }
  71. else {
  72. cin >> y;
  73. cout << __builtin_popcount(ai.Query(x, y)) << '\n';
  74. }
  75. }
  76. return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement