Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define mod 1000000007
#define lim 1000000000000000007
#define lim1 18446744073709551615 // Unsigned
#define sq(a) ((a) * (a))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define mms(v, i) memset(v, i, sizeof(v))
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define REPN(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> PL;
typedef pair<ll, int> PLI;
typedef pair<int, ll> PIL;
typedef pair<int, pair<int, int>> pii;
typedef pair<double, double> pdd;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
ll power(ll a, ll b, int m) {
  if (b == 0)
    return 1;
  ll c = power(a, b >> 1, m);
  c = (c * c) % m;
  if (b & 1)
    return (c * a) % m;
  return c;
}
ll powerwithoutmod(ll a, ll b) {
  if (b == 0)
    return 1;
  ll c = powerwithoutmod(a, b >> 1);
  c = (c * c);
  if (b & 1)
    return (c * a);
  return c;
}
ll gcdll(ll a, ll b) {
  if (b == 0)
    return a;
  return gcdll(b, a % b);
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

const int N = (int)1e5 + 5;
const int Q = 301;
const int logN = 16;

set<int> pos[26];
string s;
void solve() {
  int n, q, t, l, r;
  char ch;
  cin >> n;
  cin >> s;
  cin >> q;
  REP(i, 1, n) { pos[s[i - 1] - 'a'].insert(i); }
  while (q--) {
    cin >> t >> l;
    if (t == 1) {
      cin >> ch;
      r = ch - 'a';
      pos[s[l - 1] - 'a'].erase(pos[s[l - 1] - 'a'].find(l));
      pos[r].insert(l);
    } else {
      cin >> r;
      int ans = 0;
      REP(i, 0, 25) {
        auto it = pos[i].lower_bound(l);
        if (it != pos[i].end() && (*it) <= r) {
          ans++;
        }
      }
      cout << ans << "\n";
    }
  }
}
int main() {
  // freopen("output.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int T = 1;
  // cin>>T;
  REP(TC, 1, T) {
    // cout<<"Case #"<<TC<<": ";
    solve();
    // cout<<"\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define mod 1000000007
#define lim 1000000000000000007
#define lim1 18446744073709551615 // Unsigned
#define sq(a) ((a) * (a))
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define mms(v, i) memset(v, i, sizeof(v))
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define REP(i, a, b) for (int i = a; i <= b; i++)
#define REPN(i, a, b) for (int i = a; i >= b; i--)
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;
typedef pair<int, int> pi;
typedef pair<ll, ll> PL;
typedef pair<ll, int> PLI;
typedef pair<int, ll> PIL;
typedef pair<int, pair<int, int>> pii;
typedef pair<double, double> pdd;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    indexed_set;
ll power(ll a, ll b, int m) {
  if (b == 0)
    return 1;
  ll c = power(a, b >> 1, m);
  c = (c * c) % m;
  if (b & 1)
    return (c * a) % m;
  return c;
}
ll powerwithoutmod(ll a, ll b) {
  if (b == 0)
    return 1;
  ll c = powerwithoutmod(a, b >> 1);
  c = (c * c);
  if (b & 1)
    return (c * a);
  return c;
}
ll gcdll(ll a, ll b) {
  if (b == 0)
    return a;
  return gcdll(b, a % b);
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

const int N = (int)1e5 + 5;
const int Q = 301;
const int logN = 16;

set<int> pos[26];
string s;
void solve() {
  int n, q, t, l, r;
  char ch;
  cin >> n;
  cin >> s;
  cin >> q;
  REP(i, 1, n) { pos[s[i - 1] - 'a'].insert(i); }
  while (q--) {
    cin >> t >> l;
    if (t == 1) {
      cin >> ch;
      r = ch - 'a';
      pos[s[l - 1] - 'a'].erase(l);
      s[l - 1] = ch;
      pos[r].insert(l);
    } else {
      cin >> r;
      int ans = 0;
      REP(i, 0, 25) {
        auto it = pos[i].lower_bound(l);
        if (it != pos[i].end() && (*it) <= r) {
          ans++;
        }
      }
      cout << ans << "\n";
    }
  }
}
int main() {
  // freopen("output.txt","r",stdin);
  // freopen("output.txt","w",stdout);

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int T = 1;
  // cin>>T;
  REP(TC, 1, T) {
    // cout<<"Case #"<<TC<<": ";
    solve();
    // cout<<"\n";
  }
}