Problem p02763 (C++)

Buggy submission (status: Runtime Error):
/**
 *	created: 22.09.2020 01:48:16
 **/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
// #define endl '\n'
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define len(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
using ll = long long;
using P = pair<int, int>;
const long double pi = acos(-1.0);
const int MAX = 1000010;
const int INF = 1ll << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
inline void print(P a) { cout << a.first << " " << a.second << endl; }
template <typename T> T pow(T a, ll n) {
  T r(1);
  while (n) {
    if (n & 1)
      r *= a;
    a *= a;
    n >>= 1;
  }
  return r;
}
struct faster_io {
  faster_io() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
  }
} faster_io_;

// Segment Tree
// query : f[a,b), Ο(log(n))
template <typename X> struct SegTree {
  using F = function<X(X, X)>;
  int n;
  F f;
  const X ex;
  vector<X> dat;
  SegTree(int n_, F f_, X ex_) : n(), f(f_), ex(ex_), dat(n_ * 4, ex_) {
    int x = 1;
    while (n_ > x) {
      x *= 2;
    }
    n = x;
  }
  void set(int i, X x) {
    dat[i + n - 1] = x;
    return;
  }
  void build() {
    rrep(k, n - 1) dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
    return;
  }
  void update(int i, X x) {
    i += n - 1;
    dat[i] = x; // subject to change
    while (i) {
      i = (i - 1) / 2;
      dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
    return;
  }
  X query(int a, int b) { return query_sub(a, b, 0, 0, n); }

private:
  X query_sub(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return ex;
    else if (a <= l && r <= b)
      return dat[k];
    else {
      X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return f(vl, vr);
    }
  }
};

signed main() {
  int n, q;
  string s;
  cin >> n >> s >> q;

  using X = int;
  auto f = [](X x1, X x2) -> X { return x1 | x2; };
  X ex = 0;
  SegTree<X> seg(n, f, ex);

  auto toi = [](char c) {
    int d = c - 'a';
    int ret = 1ll << d;
    return ret;
  };

  rep(i, q) seg.set(i, toi(s[i]));
  seg.build();

  rep(i, q) {
    int t;
    cin >> t;
    if (t == 1) {
      int a;
      char c;
      cin >> a >> c;
      a--;
      seg.update(a, toi(c));
    }
    if (t == 2) {
      int a, b;
      cin >> a >> b;
      a--;
      cout << pcnt(seg.query(a, b)) << endl;
    }
  }

  return 0;
}

Fixed submission:
/**
 *	created: 22.09.2020 01:48:16
 **/
#include <bits/stdc++.h>
// #include <boost/multiprecision/cpp_int.hpp>
// using bint = boost::multiprecision::cpp_int;
using namespace std;
// #define endl '\n'
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define len(x) ll(x.size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define pcnt(bit) __builtin_popcountll(bit)
using ll = long long;
using P = pair<int, int>;
const long double pi = acos(-1.0);
const int MAX = 1000010;
const int INF = 1ll << 60;
const int MOD = 1000000007;
// const int MOD = 998244353;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
inline void print(P a) { cout << a.first << " " << a.second << endl; }
template <typename T> T pow(T a, ll n) {
  T r(1);
  while (n) {
    if (n & 1)
      r *= a;
    a *= a;
    n >>= 1;
  }
  return r;
}
struct faster_io {
  faster_io() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
  }
} faster_io_;

// Segment Tree
// query : f[a,b), Ο(log(n))
template <typename X> struct SegTree {
  using F = function<X(X, X)>;
  int n;
  F f;
  const X ex;
  vector<X> dat;
  SegTree(int n_, F f_, X ex_) : n(), f(f_), ex(ex_), dat(n_ * 4, ex_) {
    int x = 1;
    while (n_ > x) {
      x *= 2;
    }
    n = x;
  }
  void set(int i, X x) {
    dat[i + n - 1] = x;
    return;
  }
  void build() {
    rrep(k, n - 1) dat[k] = f(dat[2 * k + 1], dat[2 * k + 2]);
    return;
  }
  void update(int i, X x) {
    i += n - 1;
    dat[i] = x; // subject to change
    while (i) {
      i = (i - 1) / 2;
      dat[i] = f(dat[i * 2 + 1], dat[i * 2 + 2]);
    }
    return;
  }
  X query(int a, int b) { return query_sub(a, b, 0, 0, n); }

private:
  X query_sub(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return ex;
    else if (a <= l && r <= b)
      return dat[k];
    else {
      X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return f(vl, vr);
    }
  }
};

signed main() {
  int n, q;
  string s;
  cin >> n >> s >> q;

  using X = int;
  auto f = [](X x1, X x2) -> X { return x1 | x2; };
  X ex = 0;
  SegTree<X> seg(n, f, ex);

  auto toi = [](char c) {
    int d = c - 'a';
    int ret = 1ll << d;
    return ret;
  };

  rep(i, n) seg.set(i, toi(s[i]));
  seg.build();

  rep(i, q) {
    int t;
    cin >> t;
    if (t == 1) {
      int a;
      char c;
      cin >> a >> c;
      a--;
      seg.update(a, toi(c));
    }
    if (t == 2) {
      int a, b;
      cin >> a >> b;
      a--;
      cout << pcnt(seg.query(a, b)) << endl;
    }
  }

  return 0;
}