Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a) ? "Yes" : "No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
// SegmentTree
template <class T> class SegmentTree {
  vector<T> seg;

public:
  int size; // size of segmenttree
  T init;
  SegmentTree(int _legth, T _init = 0) {
    size = 1;
    init = _init;
    while (size <= _legth) {
      size <<= 1;
    }
    seg.assign(size << 1, init);
  }
  T get(int index) { return seg[size + index]; }
  void show() {
    rep(i, size * 2) { clog << seg[i] << " "; }
    clog << endl;
  }
  // change value
  void update(int index, T value) {
    seg[size + index] = value;
    int ter = index + size;
    while (ter != 1) {
      ter /= 2;
      seg[ter] = upquary(ter * 2);
    } // show();
  }

  T findquary(int left, int right, int index, int atStart, int atStop) {
    if ((left <= atStart) and (atStop <= right)) { // 対象範囲に完全に囲まれる
      return seg[index];
    }
    if ((atStop <= left) or (right < atStart)) { // 対象範囲に全く関わらない
      return init;
    }
    if (index >= size) { // 範囲外・子がいない
      return seg[index];
    }
    // 対象に一部が含まれる
    T aL = findquary(left, right, index * 2, atStart,
                     atStart + (atStop - atStart) / 2);
    T aR = findquary(left, right, index * 2 + 1,
                     atStart + (atStop - atStart) / 2, atStop);
    return max(aL, aR);
  }
  T upquary(int index) { // q1
    return max(seg[index], seg[index + 1]);
  }
  T find(int l, int r) { // q2
    T res = seg[size + l];
    res = findquary(l, r, 1, 0, size);
    return res;
  }
};
int main() {
  int n;
  string s;
  int ans = 0;
  cin >> n >> s;
  vector<SegmentTree<int>> dis('z' - 'a' + 1, SegmentTree<int>(s.size(), 0));
  rep(i, s.size()) {
    dis[s[i] - 'a'].update(i, 1);
    // dis[s[i]-'a'].show();
  }
  int q;
  char c;
  int l, r, id, at;
  cin >> q;
  rep(i, q) {
    cin >> id;
    if (id == 1) {
      cin >> at >> c;
      at--;
      dis[s[at]].update(at, 0);
      dis[c - 'a'].update(at, 1);
      s[at] = c;
      //	dis[c-'a'].show();

    } else if (id == 2) {
      // clog<<"2:: "<<endl;
      cin >> l >> r;
      l--;
      r--;
      ans = 0;
      rep(j, 'z' - 'a' + 1) {
        //	clog << (j + 'a') << " ::";
        //	clog << dis[j].find(l, r) << endl;
        ans += dis[j].find(l, r);
      }
      cout << ans << endl;
    }
  }
  //	cout<<ans<<endl;
}

Fixed submission:
#include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i, n) for (int i = 0, i##_len = (int)(n); i < i##_len; i++)
#define reps(i, n) for (int i = 1, i##_len = (int)(n); i <= i##_len; i++)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define repi(i, x)                                                             \
  for (auto i = (x).begin(), i##_fin = (x).end(); i != i##_fin; i++)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a) ? "Yes" : "No")
typedef vector<int> Vi;
typedef vector<Vi> VVi;
typedef pair<int, int> Pi;
typedef vector<Pi> VPi;
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long, long long> P;
typedef vector<P> VP;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long INFLL = 1LL << 60;
const int INF = 1 << 30;
const double PI = acos(-1);
// SegmentTree
template <class T> class SegmentTree {
  vector<T> seg;

public:
  int size; // size of segmenttree
  T init;
  SegmentTree(int _legth, T _init = 0) {
    size = 1;
    init = _init;
    while (size <= _legth) {
      size <<= 1;
    }
    seg.assign(size << 1, init);
  }
  T get(int index) { return seg[size + index]; }
  void show() {
    rep(i, size * 2) { clog << seg[i] << " "; }
    clog << endl;
  }
  // change value
  void update(int index, T value) {
    seg[size + index] = value;
    int ter = index + size;
    while (ter != 1) {
      ter /= 2;
      seg[ter] = upquary(ter * 2);
    } // show();
  }

  T findquary(int left, int right, int index, int atStart, int atStop) {
    if ((left <= atStart) and (atStop <= right)) { // 対象範囲に完全に囲まれる
      return seg[index];
    }
    if ((atStop <= left) or (right < atStart)) { // 対象範囲に全く関わらない
      return init;
    }
    if (index >= size) { // 範囲外・子がいない
      return seg[index];
    }
    // 対象に一部が含まれる
    T aL = findquary(left, right, index * 2, atStart,
                     atStart + (atStop - atStart) / 2);
    T aR = findquary(left, right, index * 2 + 1,
                     atStart + (atStop - atStart) / 2, atStop);
    return max(aL, aR);
  }
  T upquary(int index) { // q1
    return max(seg[index], seg[index + 1]);
  }
  T find(int l, int r) { // q2
    T res = seg[size + l];
    res = findquary(l, r, 1, 0, size);
    return res;
  }
};
int main() {
  int n;
  string s;
  int ans = 0;
  cin >> n >> s;
  vector<SegmentTree<int>> dis('z' - 'a' + 1, SegmentTree<int>(s.size(), 0));
  rep(i, s.size()) {
    dis[s[i] - 'a'].update(i, 1);
    // dis[s[i]-'a'].show();
  }
  int q;
  char c;
  int l, r, id, at;
  cin >> q;
  rep(i, q) {
    cin >> id;
    if (id == 1) {
      cin >> at >> c;
      at--;
      dis[s[at] - 'a'].update(at, 0);
      dis[c - 'a'].update(at, 1);
      s[at] = c;
      //	dis[c-'a'].show();

    } else if (id == 2) {
      // clog<<"2:: "<<endl;
      cin >> l >> r;
      l--;
      r--;
      ans = 0;
      rep(j, 'z' - 'a' + 1) {
        //	clog << (j + 'a') << " ::";
        //	clog << dis[j].find(l, r) << endl;
        ans += dis[j].find(l, r);
      }
      cout << ans << endl;
    }
  }
  //	cout<<ans<<endl;
}