Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX

struct segment_tree {
  int seguki[530000], p = 524286;
  void update(int x, int diff) {
    x += p;
    seguki[x] += diff;
    while (x) {
      x = (x - 1) / 2;
      seguki[x] += diff;
    }
  }
  int count(int l, int r) {
    int diff = r - l + 1, now = l + p, size = 1;
    while (true) {
      if (now % 2 && size * 2 <= diff) {
        now = (now - 1) / 2;
        size *= 2;
      } else {
        int ans = seguki[now];
        l += size;
        if (l == r + 1)
          return ans;
        else
          return ans + count(l, r);
      }
    }
  }
};

int n, q;
string s;
segment_tree st[26];

signed main() {
  cin >> n >> s >> q;
  rep(i, n) {
    s[i] -= 'a';
    st[s[i]].update(i, 1);
  }
  rep(i, q) {
    int a;
    cin >> a;
    if (a == 1) {
      int b;
      char c;
      cin >> b >> c;
      --b;
      c -= 'a';
      st[s[b]].update(b, -1);
      st[c].update(b, 1);
      s[b] = c;
    } else {
      int l, r, cnt = 0;
      cin >> l >> r;
      --l, --r;
      rep(j, 26) if (st[j].count(l, r) != 0) cnt++;
      cout << cnt << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1000000007
#define INF LONG_LONG_MAX

struct segment_tree {
  int seguki[1048572], p = 524286;
  void update(int x, int diff) {
    x += p;
    seguki[x] += diff;
    while (x) {
      x = (x - 1) / 2;
      seguki[x] += diff;
    }
  }
  int count(int l, int r) {
    int diff = r - l + 1, now = l + p, size = 1;
    while (true) {
      if (now % 2 && size * 2 <= diff) {
        now = (now - 1) / 2;
        size *= 2;
      } else {
        int ans = seguki[now];
        l += size;
        if (l == r + 1)
          return ans;
        else
          return ans + count(l, r);
      }
    }
  }
};

int n, q;
string s;
segment_tree st[26];

signed main() {
  cin >> n >> s >> q;
  rep(i, n) {
    s[i] -= 'a';
    st[s[i]].update(i, 1);
  }
  rep(i, q) {
    int a;
    cin >> a;
    if (a == 1) {
      int b;
      char c;
      cin >> b >> c;
      --b;
      c -= 'a';
      st[s[b]].update(b, -1);
      st[c].update(b, 1);
      s[b] = c;
    } else {
      int l, r, cnt = 0;
      cin >> l >> r;
      --l, --r;
      rep(j, 26) if (st[j].count(l, r) != 0) cnt++;
      cout << cnt << endl;
    }
  }
}