Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define M 1000000007
#define pi ld(acos(-1.0))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, n) for (int i = 1; i < (int)(n); i++)
#define repp(i, a, b) for (int i = a; i <= (int)(b); i++)
#define Repp(i, a, b) for (int i = a; i >= (int)(b); i--)
#define cs                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fi first
#define se second
#define ii pair<int, int>
#define bye(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    return 0;                                                                  \
  }
typedef long long ll;
typedef long double ld;
using namespace std;

int main() {
  cs int k, q, l, r, t, qq;
  string s;
  char c;
  cin >> k >> s >> q;
  vector<set<int>> v(26, set<int>{M});
  rep(i, k) { v[s[i] - 'a'].insert(i); }
  while (q--) {
    cin >> qq;
    qq--;
    if (!qq) {
      cin >> l >> c;
      l--;
      if (s[l] == c)
        continue;
      v[s[l] - 'a'].erase(l);
      v[c - 'a'].insert(l);
      s[l] = c;
    } else {
      cin >> l >> r;
      l--;
      r--;
      t = 0;
      rep(i, 26) {
        if (*lower_bound(v[i].begin(), v[i].end(), l) <= r)
          t++;
      }
      cout << t << '\n';
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define M 1000000007
#define pi ld(acos(-1.0))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define Rep(i, n) for (int i = 1; i < (int)(n); i++)
#define repp(i, a, b) for (int i = a; i <= (int)(b); i++)
#define Repp(i, a, b) for (int i = a; i >= (int)(b); i--)
#define cs                                                                     \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define fi first
#define se second
#define ii pair<int, int>
#define bye(x)                                                                 \
  {                                                                            \
    cout << x;                                                                 \
    return 0;                                                                  \
  }
typedef long long ll;
typedef long double ld;
using namespace std;

int main() {
  cs int k, q, l, r, t, qq;
  string s;
  char c;
  cin >> k >> s >> q;
  vector<set<int>> v(26, set<int>{M});
  rep(i, k) { v[s[i] - 'a'].insert(i); }
  while (q--) {
    cin >> qq;
    qq--;
    if (!qq) {
      cin >> l >> c;
      l--;
      if (s[l] == c)
        continue;
      v[s[l] - 'a'].erase(l);
      v[c - 'a'].insert(l);
      s[l] = c;
    } else {
      cin >> l >> r;
      l--;
      r--;
      t = 0;
      rep(i, 26) {
        auto it = v[i].lower_bound(l);
        if (it != v[i].end() && *it <= r)
          t++;
      }
      cout << t << '\n';
    }
  }
}