Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2)                                                        \
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                    \
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << endl
#define debug_4(x1, x2, x3, x4)                                                \
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                            \
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \
  CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))        \
  (__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index, num) for (int index = 0; index < (int)num; index++)
#define rep1(index, num) for (int index = 1; index <= (int)num; index++)
#define brep(index, num) for (int index = (int)num - 1; index >= 0; index--)
#define brep1(index, num) for (int index = (int)num; index > 0; index--)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = (ll)1e+18 + 1;
int INF = 1e+9 + 1;
template <typename Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;
  // 内部は1-indexedだけどmainでは0-indxedで動く
  int sz; // size 葉がsz個、葉以外がsz-1個
  vector<Monoid> seg;

  const F f;
  const Monoid M1;

  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) { seg[k + sz] = x; }

  void build() {
    for (int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); // 子を用いて親を更新
    }
  }

  void add(int k, const Monoid &x) {
    k += sz;
    seg[k] += x;
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  Monoid query(int a, int b) { // 半開区間
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        L = f(L, seg[a++]);
      if (b & 1)
        R = f(seg[--b], R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) const { return seg[k + sz]; }
};
int main() {
  int N, Q;
  string S;
  scan(N >> S >> Q);
  vector<SegmentTree<int>> segv;
  rep(i, 26) {
    SegmentTree<int> seg(
        N, [](int a, int b) { return a + b; }, 0);
    segv.push_back(seg);
  }
  rep(i, N) { segv[S[i] - 'a'].add(i, 1); }
  rep(i, Q) {
    int q;
    scan(q);
    if (q == 1) {
      int k;
      char c;
      scan(k >> c);
      k--;
      segv[S[k] - 'a'].add(k, -1);
      segv[c - 'a'].add(k, 1);
      S[k] = 'a' + c;
    } else {
      int ans = 0;
      int l, r;
      scan(l >> r);
      l--;
      rep(j, 26) {
        if (segv[j].query(l, r) > 0)
          ans++;
      }
      prin(ans);
    }
    /*rep(j,26){
            printf("%c ",'a'+j);
            rep(i,N){
                    printf("%d",segv[j].query(i,i+1));
            }kaigyo;
    }*/
  }
  return 0;
}


Fixed submission:
#define _DEBUG
#include "bits/stdc++.h"
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug_1(x1) cout << #x1 << ": " << x1 << endl
#define debug_2(x1, x2)                                                        \
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug_3(x1, x2, x3)                                                    \
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << endl
#define debug_4(x1, x2, x3, x4)                                                \
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug_5(x1, x2, x3, x4, x5)                                            \
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \
  CHOOSE((__VA_ARGS__, debug_5, debug_4, debug_3, debug_2, debug_1, ~))        \
  (__VA_ARGS__)
#else
#define debug(...)
#endif
#define rep(index, num) for (int index = 0; index < (int)num; index++)
#define rep1(index, num) for (int index = 1; index <= (int)num; index++)
#define brep(index, num) for (int index = (int)num - 1; index >= 0; index--)
#define brep1(index, num) for (int index = (int)num; index > 0; index--)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-7
#define mp(a1, a2) make_pair(a1, a2)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
typedef long long ll;
typedef long double ld;
using namespace std;
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl = (ll)1e+18 + 1;
int INF = 1e+9 + 1;
template <typename Monoid> struct SegmentTree {
  using F = function<Monoid(Monoid, Monoid)>;
  // 内部は1-indexedだけどmainでは0-indxedで動く
  int sz; // size 葉がsz個、葉以外がsz-1個
  vector<Monoid> seg;

  const F f;
  const Monoid M1;

  SegmentTree(int n, const F f, const Monoid &M1) : f(f), M1(M1) {
    sz = 1;
    while (sz < n)
      sz <<= 1;
    seg.assign(2 * sz, M1);
  }

  void set(int k, const Monoid &x) { seg[k + sz] = x; }

  void build() {
    for (int k = sz - 1; k > 0; k--) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  void update(int k, const Monoid &x) {
    k += sz;
    seg[k] = x;
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]); // 子を用いて親を更新
    }
  }

  void add(int k, const Monoid &x) {
    k += sz;
    seg[k] += x;
    while (k >>= 1) {
      seg[k] = f(seg[2 * k + 0], seg[2 * k + 1]);
    }
  }

  Monoid query(int a, int b) { // 半開区間
    Monoid L = M1, R = M1;
    for (a += sz, b += sz; a < b; a >>= 1, b >>= 1) {
      if (a & 1)
        L = f(L, seg[a++]);
      if (b & 1)
        R = f(seg[--b], R);
    }
    return f(L, R);
  }

  Monoid operator[](const int &k) const { return seg[k + sz]; }
};
int main() {
  int N, Q;
  string S;
  scan(N >> S >> Q);
  vector<SegmentTree<int>> segv;
  rep(i, 26) {
    SegmentTree<int> seg(
        N, [](int a, int b) { return a + b; }, 0);
    segv.push_back(seg);
  }
  rep(i, N) { segv[S[i] - 'a'].add(i, 1); }
  rep(i, Q) {
    int q;
    scan(q);
    if (q == 1) {
      int k;
      char c;
      scan(k >> c);
      k--;
      segv[S[k] - 'a'].add(k, -1);
      segv[c - 'a'].add(k, 1);
      S[k] = c;
    } else {
      int ans = 0;
      int l, r;
      scan(l >> r);
      l--;
      rep(j, 26) {
        if (segv[j].query(l, r) > 0)
          ans++;
      }
      prin(ans);
    }
    /*rep(j,26){
            printf("%c ",'a'+j);
            rep(i,N){
                    printf("%d",segv[j].query(i,i+1));
            }kaigyo;
    }*/
  }
  return 0;
}