Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC target("avx2")
#pragma GCC optimize("O3", "unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define int long long

#define fastIO()                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define RANDOM()                                                               \
  random_device __rd;                                                          \
  mt19937 __gen = mt19937(__rd());                                             \
  uniform_int_distribution<int> __dis(0, MAXN);                                \
  auto rnd = bind(__dis, __gen);

const int MAXN = 1E6;
const int maxn = 1 << 19;

int BIT[26][maxn];

int lb(int x) { return x & -x; }

void Add(int alpha, int idx, int val) {
  for (; idx < maxn; idx += lb(idx))
    BIT[alpha][idx] += val;
}

int Sum(int alpha, int idxL, int idxR, int sum = 0) {
  --idxL;
  for (; idxR; idxR -= lb(idxR))
    sum += BIT[alpha][idxR];
  for (; idxL; idxL -= lb(idxL))
    sum -= BIT[alpha][idxL];
  return sum;
}

int32_t main() {
  fastIO();

  int n, q, op, idx, l, r, cnt;
  char c;
  string s;
  cin >> n >> s >> q;
  for (int i = 0; i < n; ++i)
    Add(s[i] - 'a', i + 1, 1);

  while (q--) {
    cin >> op;
    if (op == 1) {
      cin >> idx >> c;
      Add(s[idx] - 'a', idx + 1, -1);
      s[idx] = c;
      Add(s[idx] - 'a', idx + 1, 1);
    } else {
      cin >> l >> r, cnt = 0;
      for (int i = 0; i < 26; ++i)
        cnt += (Sum(i, l, r) > 0);
      cout << cnt << "\n";
    }
  }

  return 0;
}


Fixed submission:
#pragma GCC target("avx2")
#pragma GCC optimize("O3", "unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define int long long

#define fastIO()                                                               \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define RANDOM()                                                               \
  random_device __rd;                                                          \
  mt19937 __gen = mt19937(__rd());                                             \
  uniform_int_distribution<int> __dis(0, MAXN);                                \
  auto rnd = bind(__dis, __gen);

const int MAXN = 1E6;
const int maxn = 1 << 19;

int BIT[26][maxn];

int lb(int x) { return x & -x; }

void Add(int alpha, int idx, int val) {
  for (; idx < maxn; idx += lb(idx))
    BIT[alpha][idx] += val;
}

int Sum(int alpha, int idxL, int idxR, int sum = 0) {
  --idxL;
  for (; idxR; idxR -= lb(idxR))
    sum += BIT[alpha][idxR];
  for (; idxL; idxL -= lb(idxL))
    sum -= BIT[alpha][idxL];
  return sum;
}

int32_t main() {
  fastIO();

  int n, q, op, idx, l, r, cnt;
  char c;
  string s;
  cin >> n >> s >> q;
  for (int i = 0; i < n; ++i)
    Add(s[i] - 'a', i + 1, 1);

  while (q--) {
    cin >> op;
    if (op == 1) {
      cin >> idx >> c, --idx;
      Add(s[idx] - 'a', idx + 1, -1);
      s[idx] = c;
      Add(s[idx] - 'a', idx + 1, 1);
    } else {
      cin >> l >> r, cnt = 0;
      for (int i = 0; i < 26; ++i)
        cnt += (Sum(i, l, r) > 0);
      cout << cnt << "\n";
    }
  }

  return 0;
}