Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int tree[1000001];
string s;
void build(int node, int start, int end) {
  if (start == end) {
    int r;
    r = s[start - 1] - 'a';
    tree[node] = 1 << r;
  } else {
    int mid;
    mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
    tree[node] = tree[2 * node] | tree[2 * node + 1];
  }
}
void update(int node, int start, int end, int idx, char val) {
  if (start == end and start == idx) {
    s[idx - 1] = val;
    int r;
    r = val - 'a';
    tree[node] = 1 << r;
  } else {
    int mid = (start + end) / 2;
    if (idx > mid) {
      update(2 * node + 1, mid + 1, end, idx, val);
    } else {
      update(2 * node, start, mid, idx, val);
    }
    tree[node] = tree[2 * node] | tree[2 * node + 1];
  }
}
int query(int node, int start, int end, int l, int r) {
  if (l > end or r < start) {
    return 0;
  } else if (l <= start and r >= end) {
    return tree[node];
  } else {
    int mid;
    mid = (start + end) / 2;
    int p1 = query(2 * node, start, mid, l, r);
    int p2 = query(2 * node + 1, mid + 1, end, l, r);
    return p1 | p2;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  cin >> s;
  int q;
  cin >> q;
  build(1, 1, n);
  for (int i = 0; i < q; i++) {
    int r;
    cin >> r;
    if (r == 1) {
      int w;
      char c;
      cin >> w >> c;
      update(1, 1, n, w, c);
    } else {
      int l, r;
      cin >> l >> r;
      int ans = query(1, 1, n, l, r);
      int cc = 0;
      while (ans > 0) {
        int er;
        er = ans & 1;
        cc += er;
        ans = ans >> 1;
      }
      cout << cc << "\n";
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int tree[3000000];
string s;
void build(int node, int start, int end) {
  if (start == end) {
    int r;
    r = s[start - 1] - 'a';
    tree[node] = 1 << r;
  } else {
    int mid;
    mid = (start + end) / 2;
    build(2 * node, start, mid);
    build(2 * node + 1, mid + 1, end);
    tree[node] = tree[2 * node] | tree[2 * node + 1];
  }
}
void update(int node, int start, int end, int idx, char val) {
  if (start == end and start == idx) {
    s[idx - 1] = val;
    int r;
    r = val - 'a';
    tree[node] = 1 << r;
  } else {
    int mid = (start + end) / 2;
    if (idx > mid) {
      update(2 * node + 1, mid + 1, end, idx, val);
    } else {
      update(2 * node, start, mid, idx, val);
    }
    tree[node] = tree[2 * node] | tree[2 * node + 1];
  }
}
int query(int node, int start, int end, int l, int r) {
  if (l > end or r < start) {
    return 0;
  } else if (l <= start and r >= end) {
    return tree[node];
  } else {
    int mid;
    mid = (start + end) / 2;
    int p1 = query(2 * node, start, mid, l, r);
    int p2 = query(2 * node + 1, mid + 1, end, l, r);
    return p1 | p2;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  cin >> s;
  int q;
  cin >> q;
  build(1, 1, n);
  for (int i = 0; i < q; i++) {
    int r;
    cin >> r;
    if (r == 1) {
      int w;
      char c;
      cin >> w >> c;
      update(1, 1, n, w, c);
    } else {
      int l, r;
      cin >> l >> r;
      int ans = query(1, 1, n, l, r);
      int cc = 0;
      while (ans > 0) {
        int er;
        er = ans & 1;
        cc += er;
        ans = ans >> 1;
      }
      cout << cc << "\n";
    }
  }
  return 0;
}