Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
using namespace std;
typedef long long ll;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define len(X) (X).size()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Tiii tuple<int, int, int>
#define Tlll tuple<ll, ll, ll>

int f, dat[26][1050000];

void init(int n_, int x) {
  f = 1;
  while (f < n_)
    f *= 2;
  f *= 2;
  for (int i = 0; i < 2 * f - 1; i++)
    dat[x][i] = 0;
}

void add(int k, int x, int v) {
  k += f - 1;
  dat[x][k] += v;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[x][k] = dat[x][k * 2 + 1] + dat[x][k * 2 + 2];
  }
}

int query(int a, int b, int k, int l, int r, int x) {
  if (r <= a || b <= l)
    return 0;

  if (a <= l && r <= b)
    return dat[x][k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2, x);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r, x);
    return vl + vr;
  }
}

int main() {
  int n;
  char s[500050];
  cin >> n >> s;

  for (int i = 0; i < 26; i++)
    init(n + 10, i);

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

  int q;
  cin >> q;
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int idx;
      char c[1];
      cin >> idx >> c[0];
      // cerr << c[0] << s[idx - 1] << endl;
      if (c[0] != s[idx - 1]) {
        add(idx, s[idx - 1] - 'a', -1);
        add(idx, c[0] - 'a', 1);
      }
      s[idx - 1] = c[0];

    } else {
      int l, r, ans = 0;
      cin >> l >> r;
      for (int i = 0; i < 26; i++) {
        ans += (query(l, r + 1, 0, 0, f, i) > 0);
        // cout << query(l, r + 1, 0, 0, f, i);
      }
      // cout << " ";
      cout << ans << endl;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <tuple>
using namespace std;
typedef long long ll;
#define _ << " " <<
#define all(X) (X).begin(), (X).end()
#define len(X) (X).size()
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define Tiii tuple<int, int, int>
#define Tlll tuple<ll, ll, ll>

int f, dat[26][1050000];

void init(int n_, int x) {
  f = 1;
  while (f < n_)
    f *= 2;
  for (int i = 0; i < 2 * f - 1; i++)
    dat[x][i] = 0;
}

void add(int k, int x, int v) {
  k += f - 1;
  dat[x][k] += v;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[x][k] = dat[x][k * 2 + 1] + dat[x][k * 2 + 2];
  }
}

int query(int a, int b, int k, int l, int r, int x) {
  if (r <= a || b <= l)
    return 0;

  if (a <= l && r <= b)
    return dat[x][k];
  else {
    int vl = query(a, b, k * 2 + 1, l, (l + r) / 2, x);
    int vr = query(a, b, k * 2 + 2, (l + r) / 2, r, x);
    return vl + vr;
  }
}

int main() {
  int n;
  char s[500050];
  cin >> n >> s;

  for (int i = 0; i < 26; i++)
    init(n + 10, i);

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

  int q;
  cin >> q;
  while (q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int idx;
      char c[1];
      cin >> idx >> c[0];
      // cerr << c[0] << s[idx - 1] << endl;
      if (c[0] != s[idx - 1]) {
        add(idx, s[idx - 1] - 'a', -1);
        add(idx, c[0] - 'a', 1);
      }
      s[idx - 1] = c[0];

    } else {
      int l, r, ans = 0;
      cin >> l >> r;
      for (int i = 0; i < 26; i++) {
        ans += (query(l, r + 1, 0, 0, f, i) > 0);
        // cout << query(l, r + 1, 0, 0, f, i);
      }
      // cout << " ";
      cout << ans << endl;
    }
  }
}