Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;
const double pi = acos(-1.0);
// memset ( a , 0 , n * sizeof(ll) ) ;

#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0); // don't use if small inpts
#define endl '\n'
#define sl(n) scanf("%lld", &n)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define f(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rf(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define ms(a, b) memset((a), (b), sizeof(a))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))
#define vec(g1)                                                                \
  int temp;                                                                    \
  cin >> temp;                                                                 \
  g1, push_back(temp);
#define abs(x) ((x < 0) ? (-(x)) : (x))
#define MAX 100005
#define inf LLONG_MAX
#define MIN INT_MIN

// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;

int mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll powmod(ll a, ll b) {
  ll res = 1;
  if (a >= mod)
    a %= mod;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a;
    if (res >= mod)
      res %= mod;
    a = a * a;
    if (a >= mod)
      a %= mod;
  }
  return res;
}
// ll fac[200005];
// ll inverse(ll n){return powmod(n, mod-2);}
// ll nCr(ll n, ll r){return (fac[n] * inverse(fac[r]) % mod * inverse(fac[n-r])
// % mod) % mod;}
////****************************************************************************************************************************************************************************************************************////
ll segtree[4 * MAX + 1];
char a[MAX];
void build(int node, int start, int end) {
  if (start == end) {

    segtree[node] = (1 << (a[start] - 'a'));
    return;
  }
  int mid = (start + end) / 2;
  build(2 * node, start, mid);
  build(2 * node + 1, mid + 1, end);
  segtree[node] = segtree[2 * node] | segtree[2 * node + 1];
}
void update(int node, int se, int en, int idx, char val) {
  if (se == en) {
    a[idx] = val;
    segtree[node] = (1 << (val - 'a'));
    return;
  }
  int mid = (se + en) / 2;
  if (se <= idx && idx <= mid)
    update(2 * node, se, mid, idx, val);
  else
    update(2 * node + 1, mid + 1, en, idx, val);

  segtree[node] = segtree[2 * node] | segtree[2 * node + 1];
}
ll query(int node, int se, int en, int qs, int qe) {
  if (qe < se || qs > en)
    return 0;
  if (qs <= se && qe >= en)
    return segtree[node];
  int mid = (se + en) / 2;

  ll left = query(2 * node, se, mid, qs, qe);
  ll right = query(2 * node + 1, mid + 1, en, qs, qe);
  return left | right;
}
int main() {
  int q;
  int n;
  cin >> n;
  f(i, 0, n) cin >> a[i];

  cin >> q;
  build(1, 0, n - 1);
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int pos;
      char val;
      cin >> pos >> val;
      update(1, 0, n - 1, pos - 1, val);
    } else if (t == 2) {
      ll l, r;
      cin >> l >> r;
      ll ans = query(1, 0, n - 1, l - 1, r - 1);
      ans = __builtin_popcount(ans);
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;
const double pi = acos(-1.0);
// memset ( a , 0 , n * sizeof(ll) ) ;

#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0); // don't use if small inpts
#define endl '\n'
#define sl(n) scanf("%lld", &n)
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define ll long long
#define ull unsigned long long
#define pii pair<int, int>
#define f(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rf(i, a, b) for (ll i = (ll)(a); i > (ll)(b); i--)
#define ms(a, b) memset((a), (b), sizeof(a))
#define max(a, b) ((a > b) ? (a) : (b))
#define min(a, b) ((a < b) ? (a) : (b))
#define vec(g1)                                                                \
  int temp;                                                                    \
  cin >> temp;                                                                 \
  g1, push_back(temp);
#define abs(x) ((x < 0) ? (-(x)) : (x))
#define MAX 500005
#define inf LLONG_MAX
#define MIN INT_MIN

// typedef
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef map<int, int> MPII;
typedef set<int> SETI;
typedef multiset<int> MSETI;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;

int mod = 1e9 + 7;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
ll powmod(ll a, ll b) {
  ll res = 1;
  if (a >= mod)
    a %= mod;
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a;
    if (res >= mod)
      res %= mod;
    a = a * a;
    if (a >= mod)
      a %= mod;
  }
  return res;
}
// ll fac[200005];
// ll inverse(ll n){return powmod(n, mod-2);}
// ll nCr(ll n, ll r){return (fac[n] * inverse(fac[r]) % mod * inverse(fac[n-r])
// % mod) % mod;}
////****************************************************************************************************************************************************************************************************************////
ll segtree[4 * MAX + 1];
char a[MAX];
void build(int node, int start, int end) {
  if (start == end) {

    segtree[node] = (1 << (a[start] - 'a'));
    return;
  }
  int mid = (start + end) / 2;
  build(2 * node, start, mid);
  build(2 * node + 1, mid + 1, end);
  segtree[node] = segtree[2 * node] | segtree[2 * node + 1];
}
void update(int node, int se, int en, int idx, char val) {
  if (se == en) {
    a[idx] = val;
    segtree[node] = (1 << (val - 'a'));
    return;
  }
  int mid = (se + en) / 2;
  if (se <= idx && idx <= mid)
    update(2 * node, se, mid, idx, val);
  else
    update(2 * node + 1, mid + 1, en, idx, val);

  segtree[node] = segtree[2 * node] | segtree[2 * node + 1];
}
ll query(int node, int se, int en, int qs, int qe) {
  if (qe < se || qs > en)
    return 0;
  if (qs <= se && qe >= en)
    return segtree[node];
  int mid = (se + en) / 2;

  ll left = query(2 * node, se, mid, qs, qe);
  ll right = query(2 * node + 1, mid + 1, en, qs, qe);
  return left | right;
}
int main() {
  int q;
  int n;
  cin >> n;
  f(i, 0, n) cin >> a[i];

  cin >> q;
  build(1, 0, n - 1);
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int pos;
      char val;
      cin >> pos >> val;
      update(1, 0, n - 1, pos - 1, val);
    } else if (t == 2) {
      ll l, r;
      cin >> l >> r;
      ll ans = query(1, 0, n - 1, l - 1, r - 1);
      ans = __builtin_popcount(ans);
      cout << ans << endl;
    }
  }
  return 0;
}