Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
int MOD = 1000000007;
struct edge {
  int to, cost;
};

int modpow(int a, int x) {
  if (a < 0)
    a += MOD;
  int ans = 1;
  while (x > 0) {
    if (x & 1)
      ans = ans * a % MOD;
    a = a * a % MOD;
    x >>= 1;
  }
  return ans;
}

int gyaku(int n) { return modpow(n, MOD - 2); }

auto factor(int n) {
  map<int, int> res;
  for (int i = 2; i * i <= n; i++) {
    for (; n % i == 0; n /= i)
      res[i]++;
  }
  if (n > 1)
    res[n]++;
  return res;
}
////////////////////////////////////////////////////////////
signed main() {
  int n;
  cin >> n;
  vector<char> s(n);
  rep(i, n) cin >> s[i];
  int q;
  cin >> q;

  set<int> sc[30];
  rep(i, n) {
    int c = s[i] - 'a';
    sc[c].insert(i);
  }
  int a;
  rep(i, q) {
    cin >> a;
    if (a == 1) {
      int g;
      char c;
      cin >> g >> c;
      g--;
      int t = s[g] - 'a';
      s[g] = c;
      sc[t].erase(g);
      sc[c - 'a'].insert(g);
    }
    if (a == 2) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(j, 26) {
        if (!sc[j].empty()) {
          auto idx = lower_bound(sc[j].begin(), sc[j].end(), l);
          if (r >= *idx && idx != sc[j].end()) {
            ans++;
          }
        }
      }
      cout << ans << endl;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
int MOD = 1000000007;
struct edge {
  int to, cost;
};

int modpow(int a, int x) {
  if (a < 0)
    a += MOD;
  int ans = 1;
  while (x > 0) {
    if (x & 1)
      ans = ans * a % MOD;
    a = a * a % MOD;
    x >>= 1;
  }
  return ans;
}

int gyaku(int n) { return modpow(n, MOD - 2); }

auto factor(int n) {
  map<int, int> res;
  for (int i = 2; i * i <= n; i++) {
    for (; n % i == 0; n /= i)
      res[i]++;
  }
  if (n > 1)
    res[n]++;
  return res;
}
////////////////////////////////////////////////////////////
signed main() {
  int n;
  cin >> n;
  vector<char> s(n);
  rep(i, n) cin >> s[i];
  int q;
  cin >> q;

  set<int> sc[30];
  rep(i, n) {
    int c = s[i] - 'a';
    sc[c].insert(i);
  }
  int a;
  rep(i, q) {
    cin >> a;
    if (a == 1) {
      int g;
      char c;
      cin >> g >> c;
      g--;
      int t = s[g] - 'a';
      s[g] = c;
      sc[t].erase(g);
      sc[c - 'a'].insert(g);
    }
    if (a == 2) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(j, 26) {
        if (!sc[j].empty()) {
          auto idx = sc[j].lower_bound(l);
          if (r >= *idx && idx != sc[j].end()) {
            ans++;
          }
        }
      }
      cout << ans << endl;
    }
  }
}