Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);                                                             \
  std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  Init();
  ll n, q;
  string s;
  cin >> n >> s >> q;
  map<char, set<ll>> d;
  rep(i, n) d[s[i]].insert(i);
  rep(i, q) {
    ll cmd;
    cin >> cmd;
    if (cmd == 1) {
      ll idx;
      char c;
      cin >> idx >> c;
      idx--;
      if (s[idx] == c)
        continue;
      d[s[idx]].erase(idx);
      s[idx] = c;
      d[c].insert(idx);
    } else if (cmd == 2) {
      ll l, r, cnt = 0;
      cin >> l >> r;
      l--;
      r--;
      for (auto p : d) {
        auto it = p.second.lower_bound(l);
        if (it == p.second.end())
          continue;
        if (*it <= r)
          cnt++;
      }
      cout << cnt << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007
#define INFLL LLONG_MAX // 9223372036854775807
#define EPS 1e-9
#define rep(i, n) for (ll i = 0, i##_len = (ll)(n); i < i##_len; i++)
#define repf(i, n) for (ll i = 1, i##_len = (ll)(n + 1); i < i##_len; i++)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
#define Init()                                                                 \
  std::ios::sync_with_stdio(false);                                            \
  std::cin.tie(0);                                                             \
  std::cout << fixed << setprecision(15);
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
using namespace std;

int main() {
  Init();
  ll n, q;
  string s;
  cin >> n >> s >> q;
  map<char, set<ll>> d;
  rep(i, n) d[s[i]].insert(i);
  rep(i, q) {
    ll cmd;
    cin >> cmd;
    if (cmd == 1) {
      ll idx;
      char c;
      cin >> idx >> c;
      idx--;
      if (s[idx] == c)
        continue;
      d[s[idx]].erase(idx);
      s[idx] = c;
      d[c].insert(idx);
    } else if (cmd == 2) {
      ll l, r, cnt = 0;
      cin >> l >> r;
      l--;
      r--;
      for (char c = 'a'; c <= 'z'; c++) {
        auto it = d[c].lower_bound(l);
        if (it == d[c].end())
          continue;
        if (*it <= r)
          cnt++;
      }
      cout << cnt << endl;
    }
  }
}