Problem p02763 (C++)

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

using namespace std;

using ll = long long;

template <typename T> using V = std::vector<T>;
using Vi = V<int>;
using VVi = V<V<int>>;
using Vl = V<ll>;
using VVl = V<V<ll>>;
using Vs = V<string>;

template <typename T1, typename T2> using P = std::pair<T1, T2>;
using Pii = P<int, int>;
using Pll = P<ll, ll>;
using Pdd = P<double, double>;

template <typename T1, typename T2> using M = std::map<T1, T2>;
using Mii = M<int, int>;
using Msi = M<string, int>;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP2(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define RREP(i, s, e) for (int i = (int)(s); i >= (int)(e); --i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(), ie = (c).end(); i != ie; ++i)
#define ALL(c) (c).begin(), (c).end()

const double PI = acos(-1);
const ll MOD = 1e9 + 7;

int n, Q;
string s;

int main() {
  cin >> n >> s >> Q;
  V<set<int>> v(26, set<int>());
  REP(i, n) { v[s[i] - 'a'].insert(i); }

  REP(q, Q) {
    int mode;
    cin >> mode;
    if (mode == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      if (s[i] == c)
        continue;
      v[s[i] - 'a'].erase(i);
      v[c - 'a'].insert(i);
      s[i] = c;
    } else if (mode == 2) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int cnt = 0;
      REP(i, 26) {
        set<int> u = v[i];
        auto iter = u.lower_bound(l);
        if (iter != u.end() && *iter <= r)
          cnt++;
      }
      printf("%d\n", cnt);
    }
  }

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

using ll = long long;

template <typename T> using V = std::vector<T>;
using Vi = V<int>;
using VVi = V<V<int>>;
using Vl = V<ll>;
using VVl = V<V<ll>>;
using Vs = V<string>;

template <typename T1, typename T2> using P = std::pair<T1, T2>;
using Pii = P<int, int>;
using Pll = P<ll, ll>;
using Pdd = P<double, double>;

template <typename T1, typename T2> using M = std::map<T1, T2>;
using Mii = M<int, int>;
using Msi = M<string, int>;

#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP2(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)
#define RREP(i, s, e) for (int i = (int)(s); i >= (int)(e); --i)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(), ie = (c).end(); i != ie; ++i)
#define ALL(c) (c).begin(), (c).end()

const double PI = acos(-1);
const ll MOD = 1e9 + 7;

int n, Q;
string s;

int main() {
  cin >> n >> s >> Q;
  V<set<int>> v(26, set<int>());
  REP(i, n) { v[s[i] - 'a'].insert(i); }

  REP(q, Q) {
    int mode;
    cin >> mode;
    if (mode == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      if (s[i] == c)
        continue;
      v[s[i] - 'a'].erase(i);
      v[c - 'a'].insert(i);
      s[i] = c;
    } else if (mode == 2) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int cnt = 0;
      REP(i, 26) {
        auto iter = v[i].lower_bound(l);
        if (iter != v[i].end() && *iter <= r)
          cnt++;
      }
      printf("%d\n", cnt);
    }
  }

  return 0;
}