Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#define RREP(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define INVREP(i, a, b) for (int i = a; i >= b; --i)
#define MOD 1000000007ll
#define BIT(i) (1ll << i)
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
} // aに更新したいものを
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

// 繰り返し2乗
ll modpow(ll N, ll P, ll M = MOD) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = modpow(N, P / 2, M);
    return t * t % M;
  }
  return (N * modpow(N, P - 1, M)) % M;
}
// 階乗
pair<vector<ll>, vector<ll>> fact(ll n) {
  vector<ll> f(n, 1);
  RREP(i, 2, n) f[i] = (f[i - 1] * i) % MOD;
  vector<ll> invf(n, 1);
  invf[n - 1] = modpow(f[n - 1], MOD - 2, MOD);
  INVREP(i, n - 1, 2) invf[i - 1] = (invf[i] * i) % MOD;
  return pair<vector<ll>, vector<ll>>(f, invf);
}
ll modmult(ll a, ll b, ll M = MOD) { return (a * b) % M; }
ll modadd(ll a, ll b, ll M = MOD) { return (a + b) % M; }
ll comb(pair<vector<ll>, vector<ll>> p, int n, int r) {
  return modmult(modmult(p.first[n], p.second[r]), p.second[n - r]);
}
/*
    auto a = fact(200009);
    cout << comb(a,15,5) << endl;
*/

void solve() {
  /* collection
  ll n;
  cin >> n;
  vector<ll> A;
  REP(i, n) cin >> A[i];
  vector<pair<ll, ll>> AB;
  REP(i, n) {cin >> AB[i].first;cin >> AB[i].second;}

  sort(ALL(vec),greater<int>());

  map<ll, ll> mp;
  map<ll, ll, greater<ll>> mp;
  REP(i, 100) mp[i] = i * 10;
  cout << mp.lower_bound(50)->second<<endl;
  cout << mp.upper_bound(50)->second << endl;
  cout << next(mp.upper_bound(50))->second << endl;
  cout << prev(mp.upper_bound(50))->second << endl;
  */
  int n, q;
  string s;
  cin >> n >> s >> q;
  int a = (int)'a';
  set<int> chars[26];
  REP(i, 26) { chars[i].insert(10000000); }
  REP(i, n) { chars[(int)s[i] - a].insert(i + 1); }

  REP(time, q) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      chars[int(s[i - 1]) - a].erase(i);
      s[i - 1] = c;
      chars[(int)c - a].insert(i);
    } else {
      int l, r, ans = 0;
      cin >> l >> r;
      REP(i, n) {
        if (*(chars[i].lower_bound(l)) <= r)
          ans++;
      }
      cout << ans << '\n';
    }
  }
}

int main() { solve(); }


Fixed submission:
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <string>
#include <tuple>
#include <vector>

#define RREP(i, a, b) for (int i = a; i < b; ++i)
#define REP(i, n) for (int i = 0; i < n; ++i)
#define INVREP(i, a, b) for (int i = a; i >= b; --i)
#define MOD 1000000007ll
#define BIT(i) (1ll << i)
using namespace std;
using ll = long long;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
} // aに更新したいものを
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

// 繰り返し2乗
ll modpow(ll N, ll P, ll M = MOD) {
  if (P == 0)
    return 1;
  if (P % 2 == 0) {
    ll t = modpow(N, P / 2, M);
    return t * t % M;
  }
  return (N * modpow(N, P - 1, M)) % M;
}
// 階乗
pair<vector<ll>, vector<ll>> fact(ll n) {
  vector<ll> f(n, 1);
  RREP(i, 2, n) f[i] = (f[i - 1] * i) % MOD;
  vector<ll> invf(n, 1);
  invf[n - 1] = modpow(f[n - 1], MOD - 2, MOD);
  INVREP(i, n - 1, 2) invf[i - 1] = (invf[i] * i) % MOD;
  return pair<vector<ll>, vector<ll>>(f, invf);
}
ll modmult(ll a, ll b, ll M = MOD) { return (a * b) % M; }
ll modadd(ll a, ll b, ll M = MOD) { return (a + b) % M; }
ll comb(pair<vector<ll>, vector<ll>> p, int n, int r) {
  return modmult(modmult(p.first[n], p.second[r]), p.second[n - r]);
}
/*
    auto a = fact(200009);
    cout << comb(a,15,5) << endl;
*/

void solve() {
  /* collection
  ll n;
  cin >> n;
  vector<ll> A;
  REP(i, n) cin >> A[i];
  vector<pair<ll, ll>> AB;
  REP(i, n) {cin >> AB[i].first;cin >> AB[i].second;}

  sort(ALL(vec),greater<int>());

  map<ll, ll> mp;
  map<ll, ll, greater<ll>> mp;
  REP(i, 100) mp[i] = i * 10;
  cout << mp.lower_bound(50)->second<<endl;
  cout << mp.upper_bound(50)->second << endl;
  cout << next(mp.upper_bound(50))->second << endl;
  cout << prev(mp.upper_bound(50))->second << endl;
  */
  int n, q;
  string s;
  cin >> n >> s >> q;
  int a = (int)'a';
  set<int> chars[26];
  REP(i, 26) { chars[i].insert(10000000); }
  REP(i, n) { chars[(int)s[i] - a].insert(i + 1); }

  REP(time, q) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      chars[int(s[i - 1]) - a].erase(i);
      s[i - 1] = c;
      chars[(int)c - a].insert(i);
    } else {
      int l, r, ans = 0;
      cin >> l >> r;
      REP(i, 26) {
        if (*(chars[i].lower_bound(l)) <= r)
          ans++;
      }
      cout << ans << '\n';
    }
  }
}

int main() { solve(); }