Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#ifdef LOCAL
#include <debug.h>
#else
#define db(...) 21
#endif

#define debug(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define FOR(it, b, e) for (typeof(b) it = (b); it != (e); ++it)
#define MSET(c, v) memset(c, v, sizeof(c))

const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const int NULO = -1;
const double EPS = 1e-10;
const ll mod = 1e9 + 7;

inline int cmp(double x, double y = 0) {
  if (fabs(x - y) < EPS)
    return 0;
  return x > y ? 1 : -1;
}

ll exp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}

const int maxn = 2e5 + 10;
ll fat[maxn], inv[maxn];

ll C(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return (((fat[n] * inv[k]) % mod) * inv[n - k]) % mod;
}

int bit[maxn][30];

void update(int idx, int l, int val) {
  idx += 2;
  while (idx < maxn) {
    bit[idx][l] += val;
    idx += idx & (-idx);
  }
}

vector<int> query(int idx) {
  idx += 2;
  vector<int> cnt(27, 0);
  while (idx) {
    for (int i = 0; i < 26; i++)
      cnt[i] += bit[idx][i];
    idx -= idx & (-idx);
  }
  return cnt;
}

int queryRange(int x, int y) {
  auto cntY = query(y);
  auto cntX = query(x - 1);
  int ans = 0;
  for (int i = 0; i < 26; i++) {
    // cout << i << " --> " << cntY[i] << " " << cntX[i] << endl;
    cntY[i] -= cntX[i];
    if (cntY[i] > 0)
      ans++;
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    update(i + 1, s[i] - 'a', 1);
  }
  int q;
  cin >> q;
  while (q--) {
    int op;
    cin >> op;
    if (op == 1) {
      int i;
      char c;
      cin >> i >> c;
      update(i, s[i - 1] - 'a', -1);
      update(i, c - 'a', 1);
      s[i - 1] = c;
    } else {
      int l, r;
      cin >> l >> r;
      cout << (queryRange(l, r)) << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#ifdef LOCAL
#include <debug.h>
#else
#define db(...) 21
#endif

#define debug(x) cout << #x << " = " << x << endl;
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define FOR(it, b, e) for (typeof(b) it = (b); it != (e); ++it)
#define MSET(c, v) memset(c, v, sizeof(c))

const int INF = 0x3F3F3F3F;
const int NEGINF = 0xC0C0C0C0;
const int NULO = -1;
const double EPS = 1e-10;
const ll mod = 1e9 + 7;

inline int cmp(double x, double y = 0) {
  if (fabs(x - y) < EPS)
    return 0;
  return x > y ? 1 : -1;
}

ll exp(ll a, ll b) {
  ll ans = 1;
  while (b) {
    if (b & 1)
      ans = (ans * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}

const int maxn = 6e5 + 10;
ll fat[maxn], inv[maxn];

ll C(int n, int k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return (((fat[n] * inv[k]) % mod) * inv[n - k]) % mod;
}

int bit[maxn][30];

void update(int idx, int l, int val) {
  idx += 2;
  while (idx < maxn) {
    bit[idx][l] += val;
    idx += idx & (-idx);
  }
}

vector<int> query(int idx) {
  idx += 2;
  vector<int> cnt(27, 0);
  while (idx) {
    for (int i = 0; i < 26; i++)
      cnt[i] += bit[idx][i];
    idx -= idx & (-idx);
  }
  return cnt;
}

int queryRange(int x, int y) {
  auto cntY = query(y);
  auto cntX = query(x - 1);
  int ans = 0;
  for (int i = 0; i < 26; i++) {
    // cout << i << " --> " << cntY[i] << " " << cntX[i] << endl;
    cntY[i] -= cntX[i];
    if (cntY[i] > 0)
      ans++;
  }
  return ans;
}

int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    update(i + 1, s[i] - 'a', 1);
  }
  int q;
  cin >> q;
  while (q--) {
    int op;
    cin >> op;
    if (op == 1) {
      int i;
      char c;
      cin >> i >> c;
      update(i, s[i - 1] - 'a', -1);
      update(i, c - 'a', 1);
      s[i - 1] = c;
    } else {
      int l, r;
      cin >> l >> r;
      cout << (queryRange(l, r)) << endl;
    }
  }

  return 0;
}