Problem p02763 (C++)

Buggy submission (status: Runtime Error):
// Catchharsh
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define F first
#define S second
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
#define see(x) cout << #x << " = " << x << endl
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
ll power(ll base, ll exponent) {
  ll ans = 1;
  // base%=MOD;
  // exponent%=(MOD-1);
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base);
    base = (1LL * base * base);
    exponent >>= 1;
  }
  return ans;
}
const ll N = 2 * 1e5;
struct tt {
  ll ar[26];
};
tt vii;
// int ar[100005];
string s;
tt tree[4 * N];

tt merge(tt a, tt b) {
  tt final;
  for (ll i = 0; i < 26; i++)
    final.ar[i] = a.ar[i] + b.ar[i];
  return final;
}

void build(ll node, ll start, ll end) {
  if (start == end) {
    tree[node].ar[s[start] - 'a']++;
    return;
  } else {
    ll mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);

    tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
  }
}
tt query(ll l, ll r, ll start, ll end, ll node) {
  if (end < l || start > r) // checking whether range is out of L and R
  {
    return vii;
  } else if (l <= start && end <= r) // if the range is completely under L and R
  {
    return tree[node];
  } else // if there is partial overlap between range and L and R
  {
    ll mid = (start + end) / 2;
    tt p1 =
        query(l, r, start, mid, 2 * node); // Checking for answer in left part
    tt p2 = query(l, r, mid + 1, end,
                  2 * node + 1); // Checking for answer in right part
    return merge(p1, p2);
  }
}
void update(ll idx, char c, ll node, ll start, ll end) {
  ll mid = (start + end) / 2;
  if (start == end) {
    tree[node].ar[s[start] - 'a']--;
    s[start] = c;
    tree[node].ar[s[start] - 'a']++;
  } else {
    if (start <= idx && idx <= mid)
      update(idx, c, 2 * node, start, mid);
    else
      update(idx, c, 2 * node + 1, mid + 1, end);
    tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
  }
}
int32_t main() {
  IOS;
  ll n;
  cin >> n;
  cin >> s;
  build(1, 0, n - 1);
  ll i, j, k, q;
  cin >> q;
  while (q--) {
    ll x;
    cin >> x;
    if (x == 1) {
      ll id;
      char ch;
      cin >> id >> ch;
      update(id - 1, ch, 1, 0, n - 1);
    } else {
      ll ans = 0;
      ll l, r;
      cin >> l >> r;
      tt ap = query(l - 1, r - 1, 0, n - 1, 1);
      for (i = 0; i < 26; i++)
        if (ap.ar[i])
          ans++;
      cout << ans << endl;
    }
  }

  cerr << endl
       << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
}

Fixed submission:
// Catchharsh
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL)
#define F first
#define S second
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\n'
#define see(x) cout << #x << " = " << x << endl
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
ll power(ll base, ll exponent) {
  ll ans = 1;
  // base%=MOD;
  // exponent%=(MOD-1);
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base);
    base = (1LL * base * base);
    exponent >>= 1;
  }
  return ans;
}
const ll N = 5 * 1e5 + 5;
struct tt {
  ll ar[26];
};
tt vii;
// int ar[100005];
string s;
tt tree[4 * N];

tt merge(tt a, tt b) {
  tt final;
  for (ll i = 0; i < 26; i++)
    final.ar[i] = a.ar[i] + b.ar[i];
  return final;
}

void build(ll node, ll start, ll end) {
  if (start == end) {
    tree[node].ar[s[start] - 'a']++;
    return;
  } else {
    ll mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);

    tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
  }
}
tt query(ll l, ll r, ll start, ll end, ll node) {
  if (end < l || start > r) // checking whether range is out of L and R
  {
    return vii;
  } else if (l <= start && end <= r) // if the range is completely under L and R
  {
    return tree[node];
  } else // if there is partial overlap between range and L and R
  {
    ll mid = (start + end) / 2;
    tt p1 =
        query(l, r, start, mid, 2 * node); // Checking for answer in left part
    tt p2 = query(l, r, mid + 1, end,
                  2 * node + 1); // Checking for answer in right part
    return merge(p1, p2);
  }
}
void update(ll idx, char c, ll node, ll start, ll end) {
  ll mid = (start + end) / 2;
  if (start == end) {
    tree[node].ar[s[start] - 'a']--;
    s[start] = c;
    tree[node].ar[s[start] - 'a']++;
  } else {
    if (start <= idx && idx <= mid)
      update(idx, c, 2 * node, start, mid);
    else
      update(idx, c, 2 * node + 1, mid + 1, end);
    tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
  }
}
int32_t main() {
  IOS;
  ll n;
  cin >> n;
  cin >> s;
  build(1, 0, n - 1);
  ll i, j, k, q;
  cin >> q;
  while (q--) {
    ll x;
    cin >> x;
    if (x == 1) {
      ll id;
      char ch;
      cin >> id >> ch;
      update(id - 1, ch, 1, 0, n - 1);
    } else {
      ll ans = 0;
      ll l, r;
      cin >> l >> r;
      tt ap = query(l - 1, r - 1, 0, n - 1, 1);
      for (i = 0; i < 26; i++)
        if (ap.ar[i])
          ans++;
      cout << ans << endl;
    }
  }

  cerr << endl
       << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\n';
}