Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
const int N = 5e5 + 54;
int n, q, type, x, k, s[N << 2][30], seg[N << 2], pre[N];
char S[N];
int ans, A;
void pushup(int x) {
  for (int i = 0; i < 26; ++i)
    s[x][i] = s[x << 1][i] + s[x << 1 | 1][i];
  seg[x] = seg[x << 1] | seg[x << 1 | 1];
}
void build(int l, int r, int x) {
  if (l == r) {
    s[x][S[l] - 97] = 1;
    seg[x] = 1 << (S[l] - 97);
    pre[x] = S[l] - 97;
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, x << 1);
  build(mid + 1, r, x << 1 | 1);
  pushup(x);
}
void modify(int l, int r, int x, int id, int k) {
  if (l == r) {
    int p = pre[x];
    --s[x][p];
    if (s[x][p] == 0)
      seg[x] -= (1 << p);
    ++s[x][k];
    if (s[x][k] == 1)
      seg[x] += (1 << k);
    pre[x] = k;
    return;
  }
  int mid = l + r >> 1;
  if (id <= mid)
    modify(l, mid, x << 1, id, k);
  else
    modify(mid + 1, r, x << 1 | 1, id, k);
  pushup(x);
}
int query(int l, int r, int x, int ql, int qr, int ret = 0) {
  if (ql <= l && r <= qr)
    return seg[x];
  int mid = l + r >> 1;
  if (ql <= mid)
    ret |= query(l, mid, x << 1, ql, qr);
  if (qr > mid)
    ret |= query(mid + 1, r, x << 1 | 1, ql, qr);
  return ret;
}
int main() {
  std::cin >> n;
  std::cin >> S + 1;
  build(1, n, 1);
  for (std::cin >> q; q; --q) {
    std::cin >> type;
    if (type & 1) {
      char y[3];
      std::cin >> x >> y;
      modify(1, n, 1, x, y[0] - 97);
    } else {
      int l, r;
      std::cin >> l >> r;
      ans = query(1, n, 1, l, r), A = 0;
      for (int i = 25; i >= 0; --i)
        if (ans & (1 << i))
          ++A;
      std::cout << A << '\n';
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
const int N = 5e5 + 54;
int n, q, type, x, k, s[N << 2][30], seg[N << 2], pre[N << 2];
char S[N];
int ans, A;
void pushup(int x) {
  for (int i = 0; i < 26; ++i)
    s[x][i] = s[x << 1][i] + s[x << 1 | 1][i];
  seg[x] = seg[x << 1] | seg[x << 1 | 1];
}
void build(int l, int r, int x) {
  if (l == r) {
    s[x][S[l] - 97] = 1;
    seg[x] = 1 << (S[l] - 97);
    pre[x] = S[l] - 97;
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, x << 1);
  build(mid + 1, r, x << 1 | 1);
  pushup(x);
}
void modify(int l, int r, int x, int id, int k) {
  if (l == r) {
    int p = pre[x];
    --s[x][p];
    if (s[x][p] == 0)
      seg[x] -= (1 << p);
    ++s[x][k];
    if (s[x][k] == 1)
      seg[x] += (1 << k);
    pre[x] = k;
    return;
  }
  int mid = l + r >> 1;
  if (id <= mid)
    modify(l, mid, x << 1, id, k);
  else
    modify(mid + 1, r, x << 1 | 1, id, k);
  pushup(x);
}
int query(int l, int r, int x, int ql, int qr, int ret = 0) {
  if (ql <= l && r <= qr)
    return seg[x];
  int mid = l + r >> 1;
  if (ql <= mid)
    ret |= query(l, mid, x << 1, ql, qr);
  if (qr > mid)
    ret |= query(mid + 1, r, x << 1 | 1, ql, qr);
  return ret;
}
int main() {
  std::cin >> n;
  std::cin >> S + 1;
  build(1, n, 1);
  for (std::cin >> q; q; --q) {
    std::cin >> type;
    if (type & 1) {
      char y[3];
      std::cin >> x >> y;
      modify(1, n, 1, x, y[0] - 97);
    } else {
      int l, r;
      std::cin >> l >> r;
      ans = query(1, n, 1, l, r), A = 0;
      for (int i = 25; i >= 0; --i)
        if (ans & (1 << i))
          ++A;
      std::cout << A << '\n';
    }
  }
}