Problem p02763 (C++)

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

template <typename T> class BinaryIndexedTree {
  vector<T> arr;
  T initial_value;
  long long size;
  function<T(T, T)> operation;

public:
  BinaryIndexedTree(const long long N, function<T(T, T)> func, T init) {
    set_size(N);
    set_initial_value(init);
    initialize_array();
    set_operation(func);
  }

  void set_size(const long long N) { this->size = N + 1; }

  void set_initial_value(const T init) { this->initial_value = init; }

  void initialize_array() { this->arr = vector<T>(size, initial_value); }

  void initialize_array(const T init) {
    set_initial_value(init);
    initialize_array();
  }

  void set_operation(const function<T(T, T)> func) { this->operation = func; }

  void update(long long idx, T value) {
    while (idx <= this->size) {
      this->arr[idx] = this->operation(this->arr[idx], value);
      idx += (idx & -idx);
    }
  }

  T query(long long idx) {
    T ret = this->initial_value;
    while (idx > 0) {
      ret = this->operation(ret, this->arr[idx]);
      idx -= (idx & -idx);
    }
    return ret;
  }
};

int main() {
  int N;
  string S;
  cin >> N >> S;
  S = " " + S;

  vector<BinaryIndexedTree<int>> bit(
      26, BinaryIndexedTree<int>(
              N + 1, [](int a, int b) { return a + b; }, 0));

  for (int i = 1; i <= N; i++) {
    bit[S[i] - 'a'].update(i, 1);
  }

  int Q;
  cin >> Q;
  for (int q = 0; q < Q; q++) {
    int t;
    cin >> t;
    if (t == 1) {
      int i;
      char c;
      cin >> i >> c;

      bit[S[i] - 'a'].update(i, -1);
      bit[c - 'a'].update(i, 1);
      S[i] = c;
    }
    if (t == 2) {
      int l, r;
      int ans = 0;
      cin >> l >> r;
      for (BinaryIndexedTree<int> b : bit) {
        if (b.query(r) - b.query(l - 1) > 0) {
          ans++;
        }
      }
      cout << ans << endl;
    }
  }
}


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

template <typename T> class BinaryIndexedTree {
  vector<T> arr;
  T initial_value;
  long long size;
  function<T(T, T)> operation;

public:
  BinaryIndexedTree(const long long N, function<T(T, T)> func, T init) {
    set_size(N);
    set_initial_value(init);
    initialize_array();
    set_operation(func);
  }

  void set_size(const long long N) { this->size = N + 1; }

  void set_initial_value(const T init) { this->initial_value = init; }

  void initialize_array() { this->arr = vector<T>(size, initial_value); }

  void initialize_array(const T init) {
    set_initial_value(init);
    initialize_array();
  }

  void set_operation(const function<T(T, T)> func) { this->operation = func; }

  void update(long long idx, T value) {
    while (idx <= this->size) {
      this->arr[idx] = this->operation(this->arr[idx], value);
      idx += (idx & -idx);
    }
  }

  T query(long long idx) {
    T ret = this->initial_value;
    while (idx > 0) {
      ret = this->operation(ret, this->arr[idx]);
      idx -= (idx & -idx);
    }
    return ret;
  }
};

int main() {
  int N;
  string S;
  cin >> N >> S;
  S = " " + S;

  vector<BinaryIndexedTree<int>> bit(
      26, BinaryIndexedTree<int>(
              N + 1, [](int a, int b) { return a + b; }, 0));

  for (int i = 1; i <= N; i++) {
    bit[S[i] - 'a'].update(i, 1);
  }

  int Q;
  cin >> Q;
  for (int q = 0; q < Q; q++) {
    int t;
    cin >> t;
    if (t == 1) {
      int i;
      char c;
      cin >> i >> c;

      bit[S[i] - 'a'].update(i, -1);
      bit[c - 'a'].update(i, 1);
      S[i] = c;
    }
    if (t == 2) {
      int l, r;
      int ans = 0;
      cin >> l >> r;
      for (BinaryIndexedTree<int> &b : bit) {
        if (b.query(r) - b.query(l - 1) > 0) {
          ans++;
        }
      }
      cout << ans << endl;
    }
  }
}