Problem p02763 (C++)

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

#define error(args...)                                                         \
  {                                                                            \
    cerr << "LINE " << __LINE__;                                               \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
    cerr << endl;                                                              \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << ' ' << *it << " = " << a;
  err(++it, args...);
}
typedef long long ll;
const int N = 26, M = 1e5 + 9, OO = 0x3f3f3f3f, MOD = 1e9 + 7;
set<int> arr[N];
char str[M];

int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", str);
  for (int i = 0; i < n; ++i)
    arr[str[i] - 'a'].insert(i);

  int m;
  scanf("%d", &m);
  error(m);
  while (m--) {
    int op, l;
    scanf("%d%d", &op, &l);
    l--;
    if (op == 1) {
      char c;
      scanf(" %c", &c);
      arr[str[l] - 'a'].erase(l);
      arr[c - 'a'].insert(l);
      str[l] = c;
    } else {
      int r, ans = 0;
      scanf("%d", &r);
      r--;
      for (int i = 0; i < 26; ++i)
        if (!arr[i].empty() && arr[i].lower_bound(l) != arr[i].end() &&
            *arr[i].lower_bound(l) <= r)
          ans++;

      printf("%d\n", ans);
    }
  }

#ifdef _LOCAL_DEFINE
  cerr << (double)clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define popcnt(a) __builtin_popcount(a)

#define error(args...)                                                         \
  {                                                                            \
    cerr << "LINE " << __LINE__;                                               \
    string _s = #args;                                                         \
    replace(_s.begin(), _s.end(), ',', ' ');                                   \
    stringstream _ss(_s);                                                      \
    istream_iterator<string> _it(_ss);                                         \
    err(_it, args);                                                            \
    cerr << endl;                                                              \
  }
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << ' ' << *it << " = " << a;
  err(++it, args...);
}
typedef long long ll;
const int N = 26, M = 5e5 + 1, OO = 0x3f3f3f3f, MOD = 1e9 + 7;
set<int> arr[N];
char str[M];

int main() {
  int n;
  scanf("%d", &n);
  scanf("%s", str);
  for (int i = 0; i < n; ++i)
    arr[str[i] - 'a'].insert(i);

  int m;
  scanf("%d", &m);
  error(m);
  while (m--) {
    int op, l;
    scanf("%d%d", &op, &l);
    l--;
    if (op == 1) {
      char c;
      scanf(" %c", &c);
      arr[str[l] - 'a'].erase(l);
      arr[c - 'a'].insert(l);
      str[l] = c;
    } else {
      int r, ans = 0;
      scanf("%d", &r);
      r--;
      for (int i = 0; i < 26; ++i)
        if (!arr[i].empty() && arr[i].lower_bound(l) != arr[i].end() &&
            *arr[i].lower_bound(l) <= r)
          ans++;

      printf("%d\n", ans);
    }
  }

#ifdef _LOCAL_DEFINE
  cerr << (double)clock() * 1.0 / CLOCKS_PER_SEC << endl;
#endif
  return 0;
}