Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define assign(x, val) memset(x, val, sizeof(x))
#define motherfucker                                                           \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define for0(i, n) for (ll i = 0; i < n; ++i)
#define for1(i, n) for (ll i = 1; i <= n; ++i)
#define ford(i, n) for (ll i = n; i >= 1; --i)
#define forg(i, a, b) for (ll i = a; i < b; ++i)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpl;
long long mod = 1000000007;
const long double PI = (long double)(3.1415926535897932384626433832795);
inline void yes() { cout << "YES\n"; }
inline void no() { cout << "NO\n"; }
inline void notposs() { cout << -1 << endl; }
bool tests = 0;

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  set<int> se[26];
  for0(i, n) { se[s[i] - 'a'].insert(i); }
  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    --x;
    if (x) {
      int l, r;
      cin >> l >> r;
      --l;
      --r;
      int ans = 0;
      for (auto x : se) {
        if (x.lower_bound(l) != x.end() && *x.lower_bound(l) <= r) {
          ans++;
        }
      }
      cout << ans << endl;
    } else {
      int x;
      char c;
      cin >> x >> c;
      --x;
      se[s[x] - 'a'].erase(x);
      s[x] = c;
      se[c - 'a'].insert(x);
    }
  }
}

int main() {

  motherfucker ll t = 1;
  if (tests)
    cin >> t;
  for0(i, t) { solve(); }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define assign(x, val) memset(x, val, sizeof(x))
#define motherfucker                                                           \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define all(x) (x).begin(), (x).end()
#define for0(i, n) for (ll i = 0; i < n; ++i)
#define for1(i, n) for (ll i = 1; i <= n; ++i)
#define ford(i, n) for (ll i = n; i >= 1; --i)
#define forg(i, a, b) for (ll i = a; i < b; ++i)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vpi;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<pll> vpl;
long long mod = 1000000007;
const long double PI = (long double)(3.1415926535897932384626433832795);
inline void yes() { cout << "YES\n"; }
inline void no() { cout << "NO\n"; }
inline void notposs() { cout << -1 << endl; }
bool tests = 0;

void solve() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  set<int> se[26];
  for0(i, n) { se[s[i] - 'a'].insert(i); }
  int q;
  cin >> q;
  while (q--) {
    int x;
    cin >> x;
    --x;
    if (x) {
      int l, r;
      cin >> l >> r;
      --l;
      --r;
      int ans = 0;
      for (auto &x : se) {
        if (x.lower_bound(l) != x.end() && *x.lower_bound(l) <= r) {
          ans++;
        }
      }
      cout << ans << endl;
    } else {
      int x;
      char c;
      cin >> x >> c;
      --x;
      se[s[x] - 'a'].erase(x);
      s[x] = c;
      se[c - 'a'].insert(x);
    }
  }
}

int main() {

  motherfucker ll t = 1;
  if (tests)
    cin >> t;
  for0(i, t) { solve(); }
  return 0;
}