Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

template <typename T> class BIT {
public:
  int n;
  vector<T> bit;

  BIT(int n) : n(n) {
    bit.resize(n);
    fill(bit.begin(), bit.end(), (T)0);
  }

  void add(int idx, T x) {
    for (int i = idx; i <= this->n; i += i & -i)
      bit[i] += x;
  }

  // bit[1]からbit[end]までの和 (閉区間)
  T sum(int end) {
    T ret = (T)0;
    for (int i = end; i >= 1; i -= i & -i)
      ret += bit[i];
    return ret;
  }

  T count_swap(vector<int> v) {
    T ret = (T)0;
    for (int i = 1; i <= n; ++i) {
      add(v[i], 1);
      ret += i - sum(v[i]);
    }
    return ret;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<BIT<int>> trees(26, BIT<int>(n + 1));
  for (int i = 0; i < n; ++i) {
    trees[s[i] - 'a'].add(i + 1, 1);
  }
  int q, query;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> query;
    if (query == 1) {
      int qi;
      char qc;
      cin >> qi >> qc;
      trees[s[qi - 1] - 'a'].add(qi, -1);
      s[qi - 1] = qc;
      trees[qc - 'a'].add(qi, 1);
    } else {
      int ql, qr, ans = 0;
      cin >> ql >> qr;
      for (int j = 0; j < 26; ++j) {
        if (trees[j].sum(qr) - trees[j].sum(ql - 1) > 0) {
          ++ans;
        }
      }
      cout << ans << endl;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

template <typename T> class BIT {
public:
  int n;
  vector<T> bit;

  BIT(int n) : n(n) {
    bit.resize(n);
    fill(bit.begin(), bit.end(), (T)0);
  }

  void add(int idx, T x) {
    for (int i = idx; i <= this->n; i += i & -i)
      bit[i] += x;
  }

  // bit[1]からbit[end]までの和 (閉区間)
  T sum(int end) {
    T ret = (T)0;
    for (int i = end; i >= 1; i -= i & -i)
      ret += bit[i];
    return ret;
  }

  T count_swap(vector<int> v) {
    T ret = (T)0;
    for (int i = 1; i <= n; ++i) {
      add(v[i], 1);
      ret += i - sum(v[i]);
    }
    return ret;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<BIT<int>> trees(26, BIT<int>(n + 10));
  for (int i = 0; i < n; ++i) {
    trees[s[i] - 'a'].add(i + 1, 1);
  }
  int q, query;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> query;
    if (query == 1) {
      int qi;
      char qc;
      cin >> qi >> qc;
      trees[s[qi - 1] - 'a'].add(qi, -1);
      s[qi - 1] = qc;
      trees[qc - 'a'].add(qi, 1);
    } else {
      int ql, qr, ans = 0;
      cin >> ql >> qr;
      for (int j = 0; j < 26; ++j) {
        if (trees[j].sum(qr) - trees[j].sum(ql - 1) > 0) {
          ++ans;
        }
      }
      cout << ans << endl;
    }
  }
}