Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define exrep(i, a, b) for (long long i = a; i <= b; i++)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<P>> vvp;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<set<int>> st(26, set<int>({MAX}));

  rep(i, n) { st[s[i] - 'a'].insert(i); }

  while (q--) {
    int type;
    cin >> type;
    if (type == 1) {
      int idx;
      char nc;
      cin >> idx >> nc;
      idx--;
      int pc = s[idx];
      st[pc - 'a'].erase(idx);
      st[nc - 'a'].insert(idx);
      s[idx] = nc;
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      int cnt = 0;
      rep(i, 26) {
        if (st[i].empty())
          continue;
        int lidx = *lower_bound(all(st[i]), l);
        int ridx = *lower_bound(all(st[i]), r);
        if (ridx > lidx)
          cnt++;
      }
      cout << cnt << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define exrep(i, a, b) for (long long i = a; i <= b; i++)
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef vector<vector<P>> vvp;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<set<int>> st(26, set<int>({MAX}));

  rep(i, n) { st[s[i] - 'a'].insert(i); }

  while (q--) {
    int type;
    cin >> type;
    if (type == 1) {
      int idx;
      char nc;
      cin >> idx >> nc;
      idx--;
      int pc = s[idx];
      st[pc - 'a'].erase(idx);
      st[nc - 'a'].insert(idx);
      s[idx] = nc;
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      int cnt = 0;
      rep(i, 26) {
        if (st[i].empty())
          continue;
        int lidx = *st[i].lower_bound(l);
        int ridx = *st[i].lower_bound(r);
        if (ridx > lidx)
          cnt++;
      }
      cout << cnt << endl;
    }
  }

  return 0;
}