Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define int long long
#define ll long long
#define mod (int)1000000007
#define P pair<int, int>
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define all(V) V.begin(), V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 10;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = DBL_EPSILON;
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
using namespace std;
inline int gcd(int a, int b) {
  while (b) {
    int c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool isprime(int n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int mypow(int a, int b) {
  if (!b)
    return 1;
  if (b & 1)
    return mypow(a, b - 1) * a;
  int memo = mypow(a, b >> 1);
  return memo * memo;
}
int modpow(int a, int b, int m = mod) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  int memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}
class BIT {
  int n;
  int *bit;

public:
  BIT(int n) : n(n) {
    bit = new int[n];
    fill(bit + 1, bit + n + 1, 0);
  }
  void add(int a, int x) {
    while (a < n) {
      bit[a] += x;
      a += a & -a;
    }
  }
  int query(int a) {
    int cnt = 0;
    while (a > 0) {
      cnt += bit[a];
      a -= a & -a;
    }
    return cnt;
  }
};
int n, q, t, a, b;
string s;
char c;
signed main() {
  cin >> n >> s >> q;
  vector<BIT> vec;
  rep(i, 26) { vec.emplace_back(n + 1); }
  rep(i, n) { vec[s[i] - 'a'].add(i + 1, 1); }
  rep(i, q) {
    cin >> t;
    if (t == 1) {
      cin >> a >> c;
      vec[s[a - 1] - 'a'].add(a, -1);
      vec[c - 'a'].add(a, 1);
      s[a - 1] = c;
    } else {
      cin >> a >> b;
      int ans = 0;
      rep(j, 26) {
        if (a > 1) {
          if (vec[j].query(b) - vec[j].query(a - 1))
            ans++;
        } else if (vec[j].query(b)) {
          ans++;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cfloat>
#include <complex>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define int long long
#define ll long long
#define mod (int)1000000007
#define P pair<int, int>
#define prique(T) priority_queue<T, vector<T>, greater<T>>
#define all(V) V.begin(), V.end()
#ifdef int
constexpr int INF = LLONG_MAX / 10;
#else
constexpr int INF = INT_MAX / 10;
#endif
constexpr double eps = DBL_EPSILON;
template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
using namespace std;
inline int gcd(int a, int b) {
  while (b) {
    int c = a;
    a = b;
    b = c % b;
  }
  return a;
}
inline int lcm(int a, int b) { return a / gcd(a, b) * b; }
bool isprime(int n) {
  if (n == 1)
    return false;
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0)
      return false;
  }
  return true;
}
int mypow(int a, int b) {
  if (!b)
    return 1;
  if (b & 1)
    return mypow(a, b - 1) * a;
  int memo = mypow(a, b >> 1);
  return memo * memo;
}
int modpow(int a, int b, int m = mod) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  int memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}
class BIT {
  int n;
  int *bit;

public:
  BIT(int n) : n(n) {
    bit = new int[n + 1];
    fill(bit + 1, bit + n + 1, 0);
  }
  void add(int a, int x) {
    while (a < n) {
      bit[a] += x;
      a += a & -a;
    }
  }
  int query(int a) {
    int cnt = 0;
    while (a > 0) {
      cnt += bit[a];
      a -= a & -a;
    }
    return cnt;
  }
};
int n, q, t, a, b;
string s;
char c;
signed main() {
  cin >> n >> s >> q;
  vector<BIT> vec;
  rep(i, 26) { vec.emplace_back(n + 1); }
  rep(i, n) { vec[s[i] - 'a'].add(i + 1, 1); }
  rep(i, q) {
    cin >> t;
    if (t == 1) {
      cin >> a >> c;
      vec[s[a - 1] - 'a'].add(a, -1);
      vec[c - 'a'].add(a, 1);
      s[a - 1] = c;
    } else {
      cin >> a >> b;
      int ans = 0;
      rep(j, 26) {
        if (a > 1) {
          if (vec[j].query(b) - vec[j].query(a - 1))
            ans++;
        } else if (vec[j].query(b)) {
          ans++;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}