Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <regex>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define LINF (LLONG_MAX / 2)
#define EPS (1.0e-8)
#define mkpr make_pair
const int MGN = 10;
const int ARY_SZ_MAX = (int)1e7;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psl = pair<string, ll>;

struct BIT {
  vi a;
  vi sg; // セグメント
  int N;
  BIT(int n) {
    N = n;
    a = vi(N + 1, 0);
    sg = vi(N + 1, 0);
  }

  // a1+a2+...+ai を求める
  int sum(int i) {
    int res = 0;
    while (i > 0) {
      res += sg[i];
      i -= i & -i; // 末尾のビット値を減算
    }
    return res;
  }

  int sum(int l, int r) { return sum(r) - sum(l - 1); }

  // ai に x を加える
  void add(int i, int x) {
    while (i <= N) {
      sg[i] += x;
      i += i & -i; // 末尾のビット値を加算
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  string S;
  cin >> S;
  S = "0" + S;
  vector<BIT> vbit(26, BIT(N));
  FOR(i, 1, N + 1) { vbit[S[i] - 'a'].add(i, 1); }

  int Q;
  cin >> Q;
  REP(i, Q) {
    int Typ;
    cin >> Typ;
    if (Typ == 1) {
      int I;
      char C;
      cin >> I >> C;
      if (S[i] != C) {
        vbit[S[I] - 'a'].add(I, -1);
        vbit[C - 'a'].add(I, 1);
        S[I] = C;
      }
    } else {
      assert(Typ == 2);
      int L, R;
      cin >> L >> R;
      int ans = 0;
      REP(j, 26) {
        if (vbit[j].sum(L, R) > 0)
          ans++;
      }
      cout << ans << endl;
    }
  }

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#include <regex>
#define FOR(i, s, e) for (int i = int(s); i < int(e); ++i)
#define REP(i, e) FOR(i, 0, e)
#define INF (INT_MAX / 2)
#define LINF (LLONG_MAX / 2)
#define EPS (1.0e-8)
#define mkpr make_pair
const int MGN = 10;
const int ARY_SZ_MAX = (int)1e7;
using namespace std;
using ll = long long;
using ull = unsigned long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vc = vector<char>;
using vvc = vector<vc>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vs = vector<string>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using psl = pair<string, ll>;

struct BIT {
  vi a;
  vi sg; // セグメント
  int N;
  BIT(int n) {
    N = n;
    a = vi(N + 1, 0);
    sg = vi(N + 1, 0);
  }

  // a1+a2+...+ai を求める
  int sum(int i) {
    int res = 0;
    while (i > 0) {
      res += sg[i];
      i -= i & -i; // 末尾のビット値を減算
    }
    return res;
  }

  int sum(int l, int r) { return sum(r) - sum(l - 1); }

  // ai に x を加える
  void add(int i, int x) {
    while (i <= N) {
      sg[i] += x;
      i += i & -i; // 末尾のビット値を加算
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  string S;
  cin >> S;
  S = "0" + S;
  vector<BIT> vbit(26, BIT(N));
  FOR(i, 1, N + 1) { vbit[S[i] - 'a'].add(i, 1); }

  int Q;
  cin >> Q;
  REP(i, Q) {
    int Typ;
    cin >> Typ;
    if (Typ == 1) {
      int I;
      char C;
      cin >> I >> C;
      if (S[I] != C) {
        vbit[S[I] - 'a'].add(I, -1);
        vbit[C - 'a'].add(I, 1);
        S[I] = C;
      }
    } else {
      assert(Typ == 2);
      int L, R;
      cin >> L >> R;
      int ans = 0;
      REP(j, 26) {
        if (vbit[j].sum(L, R) > 0)
          ans++;
      }
      cout << ans << endl;
    }
  }

  return 0;
}