Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
 * じょえチャンネル
 * 高評価・チャンネル登録よろしくおねがいします！
 * https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ
 */

#include <bits/stdc++.h>

#define f(i, n) for (int i = 0; i < (n); i++)

#define inf (int)(3e18)
#define int long long
#define mod (int)(1000000007)

#define intt long long
#define P pair<int, int>

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)

using namespace std;

// Library
// モッドパウ
int modpow(int x, int y, int m = mod) {
  int res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
      res %= m;
    }
    x = x * x % mod;
    y /= 2;
  }
  return res;
}

int mypow(int x, int y) {
  int res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
    }
    x = x * x;
    y /= 2;
  }
  return res;
}
// is the number (x) a prime number?
bool prime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (!(x % i)) {
      return false;
    }
  }
  return true;
}

// saidai-kouyakusuu
inline int gcd(int x, int y) {
  if (!y) {
    return x;
  }
  return gcd(y, x % y);
}

// number of keta
int keta(int x) {
  int ans = 0;
  while (x) {
    x /= 10;
    ans++;
  }
  return ans;
}

// sum of keta
int ketasum(int x) {
  int ans = 0;
  while (x) {
    ans += x % 10;
    x /= 10;
  }
  return ans;
}

inline int lcm(int x, int y) {
  int ans = x / gcd(x, y) * y;
  return ans;
}
int twobeki(int x) {
  int ans = 0;
  while (1) {
    if (!(x & 1)) {
      ans++;
      x /= 2;
    } else {
      break;
    }
  }
  return ans;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

#define fin(i) scanf("%lld", &i)
#define fout(i) printf("%lld\n", i)
#define fendl printf("\n")
// Library-End

int n, m, q;
string s;
set<int> basho[50];
signed main() {
  cin >> n;
  cin >> s;
  cin >> q;
  rep(i, n) {
    basho[s[i] - 'a'].insert(i);
    //        cout<<s[i]-'a'<<' '<<i<<endl;
  }
  rep(i, q) {
    int w;
    fin(w);
    if (w == 1) {
      int f;
      char c;
      scanf("%lld %c", &f, &c);
      f--;
      if (s[f] != c) {
        basho[s[f] - 'a'].erase(f);
        s[f] = c;
        basho[c - 'a'].insert(f);
      }
    } else {
      int l, r;
      fin(l);
      fin(r);
      l--;
      r--;
      int ans = 0;
      rep(j, 30) {
        auto ba = lower_bound(basho[j].begin(), basho[j].end(), l);
        if (ba != basho[j].end()) {
          if (*ba <= r)
            ans++;
        }
      }
      fout(ans);
    }
  }
}


Fixed submission:
/*
 * じょえチャンネル
 * 高評価・チャンネル登録よろしくおねがいします！
 * https://www.youtube.com/channel/UCRXsI3FL_kvaVL9zoolBfbQ
 */

#include <bits/stdc++.h>

#define f(i, n) for (int i = 0; i < (n); i++)

#define inf (int)(3e18)
#define int long long
#define mod (int)(1000000007)

#define intt long long
#define P pair<int, int>

#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)

using namespace std;

// Library
// モッドパウ
int modpow(int x, int y, int m = mod) {
  int res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
      res %= m;
    }
    x = x * x % mod;
    y /= 2;
  }
  return res;
}

int mypow(int x, int y) {
  int res = 1;
  while (y) {
    if (y % 2) {
      res *= x;
    }
    x = x * x;
    y /= 2;
  }
  return res;
}
// is the number (x) a prime number?
bool prime(int x) {
  for (int i = 2; i * i <= x; i++) {
    if (!(x % i)) {
      return false;
    }
  }
  return true;
}

// saidai-kouyakusuu
inline int gcd(int x, int y) {
  if (!y) {
    return x;
  }
  return gcd(y, x % y);
}

// number of keta
int keta(int x) {
  int ans = 0;
  while (x) {
    x /= 10;
    ans++;
  }
  return ans;
}

// sum of keta
int ketasum(int x) {
  int ans = 0;
  while (x) {
    ans += x % 10;
    x /= 10;
  }
  return ans;
}

inline int lcm(int x, int y) {
  int ans = x / gcd(x, y) * y;
  return ans;
}
int twobeki(int x) {
  int ans = 0;
  while (1) {
    if (!(x & 1)) {
      ans++;
      x /= 2;
    } else {
      break;
    }
  }
  return ans;
}

template <class T, class U> inline bool chmax(T &lhs, const U &rhs) {
  if (lhs < rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &lhs, const U &rhs) {
  if (lhs > rhs) {
    lhs = rhs;
    return 1;
  }
  return 0;
}
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

#define fin(i) scanf("%lld", &i)
#define fout(i) printf("%lld\n", i)
#define fendl printf("\n")
// Library-End

int n, m, q;
string s;
set<int> basho[50];
signed main() {
  cin >> n;
  cin >> s;
  cin >> q;
  rep(i, n) {
    basho[s[i] - 'a'].insert(i);
    //        cout<<s[i]-'a'<<' '<<i<<endl;
  }
  rep(i, q) {
    int w;
    fin(w);
    if (w == 1) {
      int f;
      char c;
      scanf("%lld %c", &f, &c);
      f--;
      if (s[f] != c) {
        basho[s[f] - 'a'].erase(f);
        s[f] = c;
        basho[c - 'a'].insert(f);
      }
    } else {
      int l, r;
      fin(l);
      fin(r);
      l--;
      r--;
      int ans = 0;
      rep(j, 30) {
        auto ba = basho[j].lower_bound(l);
        if (ba != basho[j].end()) {
          if (*ba <= r)
            ans++;
        }
      }
      fout(ans);
    }
  }
}