Problem p02763 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <functional>
#include <iomanip> //! for setprecision(10)
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <utility>

using namespace std;

typedef long long LL;

#define dump(c)                                                                \
  {                                                                            \
    for (auto it = c.begin(); it != c.end(); ++it)                             \
      if (it == c.begin())                                                     \
        cout << *it;                                                           \
      else                                                                     \
        cout << ' ' << *it;                                                    \
    cout << endl;                                                              \
  }
#define rep(i, n) for (int i = 0; i < (n); ++i)

const int MOD = 1000000007;

template <typename T> struct BIT {
  int n;
  int mod;
  std::vector<T> d;

  BIT(int n = 500001, int mod = 0) : n(n), mod(mod), d(n + 1) {}

  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
      if (mod) {
        d[i] %= mod;
      }
    }
  }

  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
      if (mod) {
        x %= mod;
      }
    }
    return x;
  }
};

BIT<int> bit[26]; //! bit for a to z

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

  int N;
  string S;
  cin >> N >> S;
  rep(i, N) {
    int a = (S[i] - 'a');
    bit[a].add(i + 1);
  }

  int Q;
  cin >> Q;
  rep(i, Q) {
    int t;
    cin >> t;
    if (t == 1) {
      int j;
      string c;
      cin >> j >> c;
      if (S[j - 1] != c[0]) {
        bit[c[0] - 'a'].add(j);
        bit[S[j - 1]].add(j, -1);
      }
    } else if (t == 2) {
      int l, r;
      cin >> l >> r;
      int cnt = 0;
      rep(a, 26) {
        int occ = bit[a].sum(r) - bit[a].sum(l - 1);
        if (0 < occ) {
          ++cnt;
        }
      }
      cout << cnt << '\n';
    }
  }
  cout << endl;

  return 0;
}


Fixed submission:

#include <algorithm>
#include <functional>
#include <iomanip> //! for setprecision(10)
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <utility>

using namespace std;

typedef long long LL;

#define dump(c)                                                                \
  {                                                                            \
    for (auto it = c.begin(); it != c.end(); ++it)                             \
      if (it == c.begin())                                                     \
        cout << *it;                                                           \
      else                                                                     \
        cout << ' ' << *it;                                                    \
    cout << endl;                                                              \
  }
#define rep(i, n) for (int i = 0; i < (n); ++i)

const int MOD = 1000000007;

template <typename T> struct BIT {
  int n;
  int mod;
  std::vector<T> d;

  BIT(int n = 500001, int mod = 0) : n(n), mod(mod), d(n + 1) {}

  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
      if (mod) {
        d[i] %= mod;
      }
    }
  }

  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
      if (mod) {
        x %= mod;
      }
    }
    return x;
  }
};

BIT<int> bit[26]; //! bit for a to z

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

  int N;
  string S;
  cin >> N >> S;
  rep(i, N) {
    int a = (S[i] - 'a');
    bit[a].add(i + 1);
  }

  int Q;
  cin >> Q;
  rep(i, Q) {
    int t;
    cin >> t;
    if (t == 1) {
      int j;
      string c;
      cin >> j >> c;
      if (S[j - 1] != c[0]) {
        int from = S[j - 1] - 'a';
        int to = c[0] - 'a';
        bit[from].add(j, -1);
        bit[to].add(j);
        S[j - 1] = c[0];
      }
    } else if (t == 2) {
      int l, r;
      cin >> l >> r;
      int cnt = 0;
      rep(a, 26) {
        int occ = bit[a].sum(r) - bit[a].sum(l - 1);
        if (0 < occ) {
          ++cnt;
        }
      }
      cout << cnt << '\n';
    }
  }
  cout << endl;

  return 0;
}