Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, a, b) for (ll i = a; i > b; i--)
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (1); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;

ll N, Q;
string S;
set<int> st[26];

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

  cin >> N >> S >> Q;
  FOR(i, 0, N) {
    int k = int(S[i] - 'a');
    st[k].insert(i);
  }

  FOR(i, 0, Q) {
    int kind;
    cin >> kind;

    if (kind == 1) {
      int j;
      char c;
      cin >> j >> c;
      j--;
      c = int(c - 'a');
      st[int(S[j] - 'a')].erase(j);
      st[int(c)].insert(j);
      S[j] = char(c);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      FOR(j, 0, 26) {
        auto p = st[j].lower_bound(l);
        if (p != st[j].end() && *p <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, a, b) for (ll i = a; i > b; i--)
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (1); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;

ll N, Q;
string S;
set<int> st[26];

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

  cin >> N >> S >> Q;
  FOR(i, 0, N) {
    int k = int(S[i] - 'a');
    st[k].insert(i);
  }

  FOR(i, 0, Q) {
    int kind;
    cin >> kind;

    if (kind == 1) {
      int j;
      char c;
      cin >> j >> c;
      j--;
      c = int(c - 'a');
      st[int(S[j] - 'a')].erase(j);
      st[int(c)].insert(j);
      S[j] = char(c + 'a');
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      FOR(j, 0, 26) {
        auto p = st[j].lower_bound(l);
        if (p != st[j].end() && *p <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }

  return 0;
}