Problem p02763 (C++)

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

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int popcount(int n) {
  int res = 0;
  for (int i = 0; i < 32; ++i) {
    if (n & (1 << i))
      res++;
  }
  return res;
};

int bit_length(int n) {
  for (int i = 31; i >= 0; --i) {
    if (n & (1 << i))
      return i + 1;
  }
  return 1;
}

struct SegmentTree {
  int n;
  // cast
  int ide;
  int tree[1000000];
  SegmentTree(int k, int id) {
    ide = id;
    n = k;
    if (popcount(n) > 1) {
      n = 1 << bit_length(n);
    }
    for (int i = 0; i < 2 * n; ++i) {
      tree[i] = ide;
    }
  }
  // rewrite
  int func(int a, int b) { return a | b; };
  void update(int i, int x) {
    i += n - 1;
    tree[i] = x;
    while (i > 1) {
      i /= 2;
      tree[i] = func(tree[2 * i], tree[2 * i + 1]);
    };
  };
  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return ide;
    if (a <= l && r <= b)
      return tree[k];

    return func(query(a, b, k * 2, l, (l + r) / 2),
                query(a, b, k * 2 + 1, (l + r) / 2, r));
  };
};

int main() {
  int n;
  string s;
  cin >> n >> s;
  SegmentTree st(n, 0);
  for (int i = 1; i <= n; i++) {
    st.update(i, 1 << (int)(s[i - 1] - 'a'));
  }
  int q, type, l, r, res;
  char t;
  cin >> q;
  rep(i, q) {
    cin >> type >> l;
    if (type == 1) {
      cin >> t;
      st.update(l, 1 << (int)(t - 'a'));
    } else if (type == 2) {
      cin >> r;
      res = st.query(l, r + 1, 1, 1, st.n + 1);
      cout << popcount(res) << "\n";
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef vector<lint> vlint;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int popcount(int n) {
  int res = 0;
  for (int i = 0; i < 32; ++i) {
    if (n & (1 << i))
      res++;
  }
  return res;
};

int bit_length(int n) {
  for (int i = 31; i >= 0; --i) {
    if (n & (1 << i))
      return i + 1;
  }
  return 1;
}

struct SegmentTree {
  int n;
  // cast
  int ide;
  int tree[1600000];
  SegmentTree(int k, int id) {
    ide = id;
    n = k;
    if (popcount(n) > 1) {
      n = 1 << bit_length(n);
    }
    for (int i = 0; i < 2 * n; ++i) {
      tree[i] = ide;
    }
  }
  // rewrite
  int func(int a, int b) { return a | b; };
  void update(int i, int x) {
    i += n - 1;
    tree[i] = x;
    while (i > 1) {
      i /= 2;
      tree[i] = func(tree[2 * i], tree[2 * i + 1]);
    };
  };
  int query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return ide;
    if (a <= l && r <= b)
      return tree[k];

    return func(query(a, b, k * 2, l, (l + r) / 2),
                query(a, b, k * 2 + 1, (l + r) / 2, r));
  };
};

int main() {
  int n;
  string s;
  cin >> n >> s;
  SegmentTree st(n, 0);
  for (int i = 1; i <= n; i++) {
    st.update(i, 1 << (int)(s[i - 1] - 'a'));
  }
  int q, type, l, r, res;
  char t;
  cin >> q;
  rep(i, q) {
    cin >> type >> l;
    if (type == 1) {
      cin >> t;
      st.update(l, 1 << (int)(t - 'a'));
    } else if (type == 2) {
      cin >> r;
      res = st.query(l, r + 1, 1, 1, st.n + 1);
      cout << popcount(res) << "\n";
    }
  }
}