Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define frw(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define pb push_back
#define in insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) int(a.size())
#define pii pair<int, int>
#define piii pair<int, pii>
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define vpii vector<pii>
#define vpiii vector<piii>
#define vpll vector<pll>
#define vplll vector<plll>
#define mod 1000000007

int modInverse(ll a, ll m) {
  ll m0 = m;
  ll y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}
ll dp[200000];

int po(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}

ll nck(ll n, ll k) {
  ll ans = 1;
  for (int i = 1; i <= k; ++i)
    ans = ans * (n - i + 1) % mod * dp[i] % mod;
  return ans;
}
int main() {
  int n, q;
  string s;
  cin >> n >> q;
  cin >> s;
  vector<set<int>> v(26);
  for (int i = 0; i < n; i++)
    v[s[i] - 'a'].insert(i);
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int x;
      char y;
      cin >> x >> y;
      v[s[x - 1] - 'a'].erase(x - 1);
      s[x - 1] = y;
      v[y - 'a'].insert(x - 1);
    } else {
      int x, y;
      cin >> x >> y;
      x--;
      y--;

      ll ans = 0;
      for (int i = 0; i < 26; i++) {
        auto z = v[i].lower_bound(x);
        if (z != v[i].end() && *z <= y)
          ans++;
      }
      cout << ans << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
using namespace std;

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define frw(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define pb push_back
#define in insert
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define sz(a) int(a.size())
#define pii pair<int, int>
#define piii pair<int, pii>
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define vpii vector<pii>
#define vpiii vector<piii>
#define vpll vector<pll>
#define vplll vector<plll>
#define mod 1000000007

int modInverse(ll a, ll m) {
  ll m0 = m;
  ll y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m, a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}
ll dp[200000];

int po(ll x, ll y) {
  ll res = 1;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % mod;
    y = y >> 1;
    x = (x * x) % mod;
  }
  return res;
}

ll nck(ll n, ll k) {
  ll ans = 1;
  for (int i = 1; i <= k; ++i)
    ans = ans * (n - i + 1) % mod * dp[i] % mod;
  return ans;
}
int main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  // cin>>s;
  vector<set<int>> v(26);
  for (int i = 0; i < n; i++)
    v[s[i] - 'a'].insert(i);
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int x;
      char y;
      cin >> x >> y;
      v[s[x - 1] - 'a'].erase(x - 1);
      s[x - 1] = y;
      v[y - 'a'].insert(x - 1);
    } else {
      int x, y;
      cin >> x >> y;
      x--;
      y--;

      ll ans = 0;
      for (int i = 0; i < 26; i++) {
        auto z = v[i].lower_bound(x);
        if (z != v[i].end() && *z <= y)
          ans++;
      }
      cout << ans << endl;
    }
  }
}