Problem p02763 (C++)

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

#define debug(x) cout << #x << " :: " << x << "\n";
#define debug2(x, y)                                                           \
  cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\n";
#define debug3(x, y, z)                                                        \
  cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\t" << #z         \
       << " :: " << z << "\n";

#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define setprecn(x) cout << fixed << setprecision(x);
#define ll long long int
#define pb push_back
#define pll pair<ll, ll>
#define vll vector<ll>
#define ff first
#define ss second
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define mod 1000000007
#define inf 10000000000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repe(i, a, b) for (ll i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search

ll power(ll a, ll b, ll m = mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll c = power(a, b / 2, m);
  if (b % 2 == 0)
    return ((c % m) * (c % m)) % m;
  return ((((c % m) * (c % m)) % m) * (a % m)) % m;
}

/********************************************************/
//	d.erase(unique(all(d)), d.end());

int main() {
  IOS;
  ll n, k, m, i, j, c = 0, cs = 0, t;
  t = 1;

  string s;
  cin >> s >> t;
  n = s.length();
  set<ll> x[26];
  rep(i, n) { x[s[i] - 'a'].insert(i + 1); }
  while (t--) {
    char ch;
    cin >> n;
    // cout<<(x[0].lower_bound(7)==x[0].end());
    if (n == 1) {
      ll pos;
      cin >> pos >> ch;
      // pos--;
      rep(i, 26) {
        auto y = x[i].lower_bound(pos);
        if (y != x[i].end() && (*y) == pos) {
          x[i].erase(y);
          break;
        }
      }
      x[ch - 'a'].insert(pos);
    } else {
      c = 0;
      cin >> j >> k;
      rep(i, 26) {
        auto y = x[i].lower_bound(j);
        if (y != x[i].end() && (*y) <= k)
          c++;
      }
      cout << c;

      cout << "\n";
    }
  }
  return 0;
}

Fixed submission:
// In the name of GOD
#include <bits/stdc++.h>
using namespace std;

#define debug(x) cout << #x << " :: " << x << "\n";
#define debug2(x, y)                                                           \
  cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\n";
#define debug3(x, y, z)                                                        \
  cout << #x << " :: " << x << "\t" << #y << " :: " << y << "\t" << #z         \
       << " :: " << z << "\n";

#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define setprecn(x) cout << fixed << setprecision(x);
#define ll long long int
#define pb push_back
#define pll pair<ll, ll>
#define vll vector<ll>
#define ff first
#define ss second
#define all(a) (a).begin(), (a).end()
#define sz(x) (ll) x.size()
#define mod 1000000007
#define inf 10000000000000007
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repe(i, a, b) for (ll i = a; i < b; i++)
#define lbnd lower_bound
#define ubnd upper_bound
#define bs binary_search

ll power(ll a, ll b, ll m = mod) {
  if (b == 0)
    return 1;
  if (b == 1)
    return a;
  ll c = power(a, b / 2, m);
  if (b % 2 == 0)
    return ((c % m) * (c % m)) % m;
  return ((((c % m) * (c % m)) % m) * (a % m)) % m;
}

/********************************************************/
//	d.erase(unique(all(d)), d.end());

int main() {
  IOS;
  ll n, k, m, i, j, c = 0, cs = 0, t;
  t = 1;

  string s;
  cin >> n >> s >> t;
  n = s.length();
  set<ll> x[26];
  rep(i, n) { x[s[i] - 'a'].insert(i + 1); }
  while (t--) {
    char ch;
    cin >> n;
    // cout<<(x[0].lower_bound(7)==x[0].end());
    if (n == 1) {
      ll pos;
      cin >> pos >> ch;
      // pos--;
      rep(i, 26) {
        auto y = x[i].lower_bound(pos);
        if (y != x[i].end() && (*y) == pos) {
          x[i].erase(y);
          break;
        }
      }
      x[ch - 'a'].insert(pos);
    } else {
      c = 0;
      cin >> j >> k;
      rep(i, 26) {
        auto y = x[i].lower_bound(j);
        if (y != x[i].end() && (*y) <= k)
          c++;
      }
      cout << c;

      cout << "\n";
    }
  }
  return 0;
}