Problem p02763 (C++)

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define VIN(v)                                                                 \
  for (auto &elem_ : (v))                                                      \
  cin >> elem_
#define VOUT(v, sep)                                                           \
  for (_loop_int idx = 0; idx < (_loop_int)v.size(); idx++) {                  \
    cout << v[idx];                                                            \
    if (idx < (_loop_int)v.size() - 1)                                         \
      cout << sep;                                                             \
  }                                                                            \
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG2(x, y) cout << #x << ": " << x << " " << #y << ": " << y << endl
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  REP(debugidx, v.size()) cout << " " << v[debugidx];                          \
  cout << endl
#define DEBUG_ARR(v, n)                                                        \
  cout << #v << ":";                                                           \
  REP(debugidx, n) cout << " " << v[debugidx];                                 \
  cout << endl

const ll MOD = 1000000007ll;
const int IINF = numeric_limits<int>::max() / 2 - 1;
const ll LINF = numeric_limits<ll>::max() / 2 - 1;

template <class T> inline bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  // ios::sync_with_stdio(false); cin.tie(0);
  int n;
  string s;
  cin >> n;
  cin >> s;
  // vector<vi> v(26);
  set<int> v[26];

  REP(i, n) {
    // v[s[i]-'a'].emplace_back(i+1);
    v[s[i] - 'a'].insert(i + 1);
  }

  int q;
  cin >> q;
  REP(lp, q) {
    int com, l, r;
    char a;
    cin >> com;
    if (com == 1) {
      // 変更
      cin >> l >> a;

      // 削除
      v[s[l - 1] - 'a'].erase(l);
      s[l - 1] = a;

      // 挿入
      v[s[l - 1] - 'a'].insert(l);
    } else {
      // 出力
      cin >> l >> r;
      int ans = 0;

      REP(i, 26) {
        auto it = lower_bound(ALL(v[i]), l);

        if (it != v[i].end() && *it <= r) {
          ans++;
        }
      }

      // cout<<ans<<endl;
      printf("%d\n", ans);
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

typedef int _loop_int;
#define REP(i, n) for (_loop_int i = 0; i < (_loop_int)(n); ++i)
#define FOR(i, a, b) for (_loop_int i = (_loop_int)(a); i < (_loop_int)(b); ++i)
#define FORR(i, a, b)                                                          \
  for (_loop_int i = (_loop_int)(b)-1; i >= (_loop_int)(a); --i)

#define VIN(v)                                                                 \
  for (auto &elem_ : (v))                                                      \
  cin >> elem_
#define VOUT(v, sep)                                                           \
  for (_loop_int idx = 0; idx < (_loop_int)v.size(); idx++) {                  \
    cout << v[idx];                                                            \
    if (idx < (_loop_int)v.size() - 1)                                         \
      cout << sep;                                                             \
  }                                                                            \
  cout << endl
#define ALL(a) (a).begin(), (a).end()

#define DEBUG(x) cout << #x << ": " << x << endl
#define DEBUG2(x, y) cout << #x << ": " << x << " " << #y << ": " << y << endl
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  REP(debugidx, v.size()) cout << " " << v[debugidx];                          \
  cout << endl
#define DEBUG_ARR(v, n)                                                        \
  cout << #v << ":";                                                           \
  REP(debugidx, n) cout << " " << v[debugidx];                                 \
  cout << endl

const ll MOD = 1000000007ll;
const int IINF = numeric_limits<int>::max() / 2 - 1;
const ll LINF = numeric_limits<ll>::max() / 2 - 1;

template <class T> inline bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  // ios::sync_with_stdio(false); cin.tie(0);
  int n;
  string s;
  cin >> n;
  cin >> s;
  // vector<vi> v(26);
  set<int> v[26];

  REP(i, n) {
    // v[s[i]-'a'].emplace_back(i+1);
    v[s[i] - 'a'].insert(i + 1);
  }

  int q;
  cin >> q;
  REP(lp, q) {
    int com, l, r;
    char a;
    cin >> com;
    if (com == 1) {
      // 変更
      cin >> l >> a;

      // 削除
      v[s[l - 1] - 'a'].erase(l);
      s[l - 1] = a;

      // 挿入
      v[s[l - 1] - 'a'].insert(l);
    } else {
      // 出力
      cin >> l >> r;
      int ans = 0;

      REP(i, 26) {
        auto it = v[i].lower_bound(l);

        if (it != v[i].end() && *it <= r) {
          ans++;
        }
      }

      // cout<<ans<<endl;
      printf("%d\n", ans);
    }
  }
  return 0;
}