Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

void textIO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

#define mx 500005
int tree[mx];
string str;

void build(int node, int s, int e) {
  if (s == e) {
    tree[node] = (1 << (str[s - 1] - 'a'));
    return;
  }
  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (s + e) / 2;
  build(left, s, mid);
  build(right, mid + 1, e);
  tree[node] = tree[left] | tree[right];
  return;
}

int query(int node, int s, int e, int l, int r) {
  if (l > e || r < s) {
    return 0;
  }
  if (l <= s && e <= r) {
    return tree[node];
  }
  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (s + e) / 2;
  return query(left, s, mid, l, r) | query(right, mid + 1, e, l, r);
}

void update(int node, int ind, char val, int s, int e) {
  if (s == ind && e == ind) {
    tree[node] = (1 << (val - 'a'));
    return;
  }
  if (ind < s || ind > e) {
    return;
  }
  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (s + e) / 2;
  update(left, ind, val, s, mid);
  update(right, ind, val, mid + 1, e);
  tree[node] = tree[left] | tree[right];
  return;
}

int main() {
  // textIO();
  fastIO();

  long long n, q;
  cin >> n;
  cin >> str;
  build(1, 1, n);
  cin >> q;
  while (q--) {
    long long qr, ind, l, r;
    char val;
    cin >> qr;
    if (qr == 1) {
      cin >> ind >> val;
      update(1, ind, val, 1, n);
    } else {
      cin >> l >> r;
      cout << __builtin_popcount(query(1, 1, n, l, r)) << '\n';
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

void textIO() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
}

void fastIO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}

#define mx 500005
int tree[4 * mx];
string str;

void build(int node, int s, int e) {
  if (s == e) {
    tree[node] = (1 << (str[s - 1] - 'a'));
    return;
  }
  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (s + e) / 2;
  build(left, s, mid);
  build(right, mid + 1, e);
  tree[node] = tree[left] | tree[right];
  return;
}

int query(int node, int s, int e, int l, int r) {
  if (l > e || r < s) {
    return 0;
  }
  if (l <= s && e <= r) {
    return tree[node];
  }
  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (s + e) / 2;
  return query(left, s, mid, l, r) | query(right, mid + 1, e, l, r);
}

void update(int node, int ind, char val, int s, int e) {
  if (s == ind && e == ind) {
    tree[node] = (1 << (val - 'a'));
    return;
  }
  if (ind < s || ind > e) {
    return;
  }
  int left = node * 2;
  int right = node * 2 + 1;
  int mid = (s + e) / 2;
  update(left, ind, val, s, mid);
  update(right, ind, val, mid + 1, e);
  tree[node] = tree[left] | tree[right];
  return;
}

int main() {
  // textIO();
  fastIO();

  long long n, q;
  cin >> n;
  cin >> str;
  build(1, 1, n);
  cin >> q;
  while (q--) {
    long long qr, ind, l, r;
    char val;
    cin >> qr;
    if (qr == 1) {
      cin >> ind >> val;
      update(1, ind, val, 1, n);
    } else {
      cin >> l >> r;
      cout << __builtin_popcount(query(1, 1, n, l, r)) << '\n';
    }
  }
  return 0;
}