Problem p02763 (C++)

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

#define ll long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define db double
#define pll pair<ll, ll>
#define pdb pair<db, db>
#define ALL(a) (a).begin(), (a).end()
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forn1(i, n) for (int i = 1; i < int(n); i++)
#define fornab(i, a, b) for (int i = a; i < b; i++)
#define clr(x) memset(x, 0, sizeof(x))
#define count_bit(x) __builtin_popcount(x)
#define endl "\n"
#define trace1(x) cout << #x << ": " << x << endl
#define trace2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define trace(v)                                                               \
  for (auto it = v.begin(); it != v.end(); it++)                               \
    cout << *it << " ";                                                        \
  cout << endl;
#define eps 1e-12
#define nedl endl
const ll size = 805;
const ll mod = 1e9 + 7;

ll power(ll n, ll k) {
  ll ans = 1;
  while (k) {
    if (k & 1)
      ans = (ans * n) % mod;
    k /= 2;
    n = (n * n) % mod;
  }
  return ans;
}

ll BIT[26][500002] = {0};

void update(ll letter, ll i, ll inc, ll n) {
  while (i <= n) {
    BIT[letter][i] += inc;
    i += (i & (-i));
  }
}

ll query(ll letter, ll i) {
  ll sum = 0;
  while (i > 0) {
    sum += BIT[letter][i];
    i -= (i & (-i));
  }
  return sum;
}

int main() {
  IOS;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll t = 1; // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    string s;
    cin >> s;

    forn(i, n) { update(s[i] - 'a', i + 1, 1, n); }

    ll q;
    cin >> q;
    // trace1(q);

    while (q--) {
      ll x1;
      cin >> x1;
      if (x1 == 1) {
        // trace1(x1);
        ll x2;
        char x3;
        cin >> x2 >> x3;
        int letter = s[x2 - 1] - 'a';
        s[x2 - 1] = x3;
        update(letter, x2, -1, n);
        update(x3 - 'a', x2, 1, n);
      } else {
        // trace1(x1);
        ll l, r;
        cin >> l >> r;
        ll c = 0;
        forn(i, 26) {
          ll gg = query(i, r) - query(i, l - 1);
          if (gg)
            c++;
        }
        cout << c << endl;
      }
    }
  }

  return 0;
}


Fixed submission:
// fahadmd16
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define pf push_front
#define eb emplace_back
#define mp make_pair
#define ff first
#define ss second
#define db double
#define pll pair<ll, ll>
#define pdb pair<db, db>
#define ALL(a) (a).begin(), (a).end()
#define IOS                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forn1(i, n) for (int i = 1; i < int(n); i++)
#define fornab(i, a, b) for (int i = a; i < b; i++)
#define clr(x) memset(x, 0, sizeof(x))
#define count_bit(x) __builtin_popcount(x)
#define endl "\n"
#define trace1(x) cout << #x << ": " << x << endl
#define trace2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \
  cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \
       << z << endl
#define trace4(a, b, c, d)                                                     \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \
       << #f << ": " << f << endl
#define trace(v)                                                               \
  for (auto it = v.begin(); it != v.end(); it++)                               \
    cout << *it << " ";                                                        \
  cout << endl;
#define eps 1e-12
#define nedl endl
const ll size = 805;
const ll mod = 1e9 + 7;

ll power(ll n, ll k) {
  ll ans = 1;
  while (k) {
    if (k & 1)
      ans = (ans * n) % mod;
    k /= 2;
    n = (n * n) % mod;
  }
  return ans;
}

ll BIT[26][500002] = {0};

void update(ll letter, ll i, ll inc, ll n) {
  while (i <= n) {
    BIT[letter][i] += inc;
    i += (i & (-i));
  }
}

ll query(ll letter, ll i) {
  ll sum = 0;
  while (i > 0) {
    sum += BIT[letter][i];
    i -= (i & (-i));
  }
  return sum;
}

int main() {
  IOS;
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
  // #endif
  ll t = 1; // cin>>t;
  while (t--) {
    ll n;
    cin >> n;
    string s;
    cin >> s;

    forn(i, n) { update(s[i] - 'a', i + 1, 1, n); }

    ll q;
    cin >> q;
    // trace1(q);

    while (q--) {
      ll x1;
      cin >> x1;
      if (x1 == 1) {
        // trace1(x1);
        ll x2;
        char x3;
        cin >> x2 >> x3;
        int letter = s[x2 - 1] - 'a';
        s[x2 - 1] = x3;
        update(letter, x2, -1, n);
        update(x3 - 'a', x2, 1, n);
      } else {
        // trace1(x1);
        ll l, r;
        cin >> l >> r;
        ll c = 0;
        forn(i, 26) {
          ll gg = query(i, r) - query(i, l - 1);
          if (gg)
            c++;
        }
        cout << c << endl;
      }
    }
  }

  return 0;
}