Problem p02763 (C++)

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

int MAXN = 1e5 + 10, curr = 0;
int t[26][4 * 100000], a[100000];
;

void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[curr][v] = (a[tl] == curr);
  } else {
    int tm = (tl + tr) / 2;
    build(v * 2, tl, tm);
    build(v * 2 + 1, tm + 1, tr);
    t[curr][v] = t[curr][v * 2] + t[curr][v * 2 + 1];
  }
}

int sum(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[curr][v];
  }
  int tm = (tl + tr) / 2;
  return sum(v * 2, tl, tm, l, min(r, tm)) +
         sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}

void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[curr][v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[curr][v] = t[curr][v * 2] + t[curr][v * 2 + 1];
  }
}

int main() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  int len = str.length();

  for (int i = 0; i < len; ++i)
    a[i] = str[i] - 'a';
  for (int i = 0; i < 26; ++i) {
    curr = i;
    build(1, 0, len - 1);
  }

  int q;
  cin >> q;
  while (q--) {
    int k;
    cin >> k;

    if (k == 1) {
      int l;
      char r;
      cin >> l >> r;
      --l;
      curr = a[l];
      update(1, 0, len - 1, l, 0);
      a[l] = r - 'a';
      curr = a[l];
      update(1, 0, len - 1, l, 1);
    }

    else {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        curr = i;
        int val = sum(1, 0, len - 1, l - 1, r - 1);
        if (val > 0)
          ++ans;
      }
      cout << ans << '\n';
    }
  }
  return 0;
}


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

int MAXN = 1e9 + 7, curr = 0;
int t[26][4 * 500100], a[4 * 500100];
;

void build(int v, int tl, int tr) {
  if (tl == tr) {
    t[curr][v] = (a[tl] == curr);
  } else {
    int tm = (tl + tr) / 2;
    build(v * 2, tl, tm);
    build(v * 2 + 1, tm + 1, tr);
    t[curr][v] = t[curr][v * 2] + t[curr][v * 2 + 1];
  }
}

int sum(int v, int tl, int tr, int l, int r) {
  if (l > r)
    return 0;
  if (l == tl && r == tr) {
    return t[curr][v];
  }
  int tm = (tl + tr) / 2;
  return sum(v * 2, tl, tm, l, min(r, tm)) +
         sum(v * 2 + 1, tm + 1, tr, max(l, tm + 1), r);
}

void update(int v, int tl, int tr, int pos, int new_val) {
  if (tl == tr) {
    t[curr][v] = new_val;
  } else {
    int tm = (tl + tr) / 2;
    if (pos <= tm)
      update(v * 2, tl, tm, pos, new_val);
    else
      update(v * 2 + 1, tm + 1, tr, pos, new_val);
    t[curr][v] = t[curr][v * 2] + t[curr][v * 2 + 1];
  }
}

int main() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  int len = str.length();

  for (int i = 0; i < len; ++i)
    a[i] = str[i] - 'a';
  for (int i = 0; i < 26; ++i) {
    curr = i;
    build(1, 0, len - 1);
  }

  int q;
  cin >> q;
  while (q--) {
    int k;
    cin >> k;

    if (k == 1) {
      int l;
      char r;
      cin >> l >> r;
      --l;
      curr = a[l];
      update(1, 0, len - 1, l, 0);
      a[l] = r - 'a';
      curr = a[l];
      update(1, 0, len - 1, l, 1);
    }

    else {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        curr = i;
        int val = sum(1, 0, len - 1, l - 1, r - 1);
        if (val > 0)
          ++ans;
      }
      cout << ans << '\n';
    }
  }
  return 0;
}