Problem p02763 (C++)

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

using namespace std;

// segtree code from : https://codeforces.com/blog/entry/18051

const int N = 1e5 + 5; // limit for array size
const int AL = 26;     // alphabet size
int n;                 // array size
int t[AL][2 * N];

void build() { // build the tree
  for (int i = 0; i < AL; i++) {
    for (int j = n - 1; j > 0; --j)
      t[i][j] = t[i][j << 1] + t[i][j << 1 | 1];
  }
}

void modify(int p, int v) { // set value at position p
  int o_p = p;
  for (int i = 0; i < AL; i++) {
    p = o_p;
    if (i == v)
      continue;
    for (t[i][p += n] = 0; p > 1; p >>= 1)
      t[i][p >> 1] = t[i][p] + t[i][p ^ 1];
  }
  p = o_p;
  for (t[v][p += n] = 1; p > 1; p >>= 1)
    t[v][p >> 1] = t[v][p] + t[v][p ^ 1];
}

int query(int l, int r) { // sum on interval [l, r)
  int ans = 0;
  int o_l = l, o_r = r;
  for (int i = 0; i < AL; i++) {
    int res = 0;
    l = o_l;
    r = o_r;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        res += t[i][l++];
      if (r & 1)
        res += t[i][--r];
    }
    if (res > 0)
      ++ans;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < n; i++) {
    int v = s[i] - 'a';
    t[v][n + i] = 1;
  }

  build();

  int q;
  cin >> q;
  while (q--) {
    int x, l, r;
    cin >> x >> l;
    if (x == 1) {
      char c;
      cin >> c;
      int v = c - 'a';
      modify(l - 1, v);
    } else {
      cin >> r;
      cout << query(l - 1, r) << "\n";
    }
  }

  return 0;
}

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

using namespace std;

// segtree code from : https://codeforces.com/blog/entry/18051

const int N = 5e5 + 5; // limit for array size
const int AL = 26;     // alphabet size
int n;                 // array size
int t[AL][2 * N];

void build() { // build the tree
  for (int i = 0; i < AL; i++) {
    for (int j = n - 1; j > 0; --j)
      t[i][j] = t[i][j << 1] + t[i][j << 1 | 1];
  }
}

void modify(int p, int v) { // set value at position p
  int o_p = p;
  for (int i = 0; i < AL; i++) {
    p = o_p;
    if (i == v)
      continue;
    for (t[i][p += n] = 0; p > 1; p >>= 1)
      t[i][p >> 1] = t[i][p] + t[i][p ^ 1];
  }
  p = o_p;
  for (t[v][p += n] = 1; p > 1; p >>= 1)
    t[v][p >> 1] = t[v][p] + t[v][p ^ 1];
}

int query(int l, int r) { // sum on interval [l, r)
  int ans = 0;
  int o_l = l, o_r = r;
  for (int i = 0; i < AL; i++) {
    int res = 0;
    l = o_l;
    r = o_r;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        res += t[i][l++];
      if (r & 1)
        res += t[i][--r];
    }
    if (res > 0)
      ++ans;
  }
  return ans;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  cin >> n;
  string s;
  cin >> s;

  for (int i = 0; i < n; i++) {
    int v = s[i] - 'a';
    t[v][n + i] = 1;
  }

  build();

  int q;
  cin >> q;
  while (q--) {
    int x, l, r;
    cin >> x >> l;
    if (x == 1) {
      char c;
      cin >> c;
      int v = c - 'a';
      modify(l - 1, v);
    } else {
      cin >> r;
      cout << query(l - 1, r) << "\n";
    }
  }

  return 0;
}