prithivMLmods/Coder-Stat huggingface dataset:
Problem p02763 (C++)

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

using namespace std;

int N, Q;
string s;
set<int> v[26];

int main() {
  cin >> N;
  cin >> s;
  cin >> Q;
  for (int i = 0; i < N; i++)
    v[s[i] - 'a'].insert(i);
  for (int i = 0; i < 26; i++)
    v[i].insert(N + 1);
  vector<int> ans;
  for (int i = 0; i < Q; i++) {
    int C = 0;
    int a;
    cin >> a;
    if (a == 1) {
      int b;
      char c;
      cin >> b >> c;
      v[s[b - 1] - 'a'].erase(b - 1);
      s[b - 1] = c;
      v[s[b - 1] - 'a'].insert(b - 1);
    } else {
      int b, c;
      cin >> b >> c;
      for (int j = 0; j < 26; j++) {
        if ((int)c > *lower_bound(v[j].begin(), v[j].end(), b - 1)) {
          C++;
        }
      }
      ans.push_back(C);
    }
  }
  for (auto &e : ans)
    cout << e << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int N, Q;
string s;
set<int> v[26];

int main() {
  cin >> N;
  cin >> s;
  cin >> Q;
  for (int i = 0; i < N; i++)
    v[s[i] - 'a'].insert(i);
  for (int i = 0; i < 26; i++)
    v[i].insert(N + 1);
  vector<int> ans;
  for (int i = 0; i < Q; i++) {
    int C = 0;
    int a;
    cin >> a;
    if (a == 1) {
      int b;
      char c;
      cin >> b >> c;
      v[s[b - 1] - 'a'].erase(b - 1);
      s[b - 1] = c;
      v[s[b - 1] - 'a'].insert(b - 1);
    } else {
      int b, c;
      cin >> b >> c;
      for (int j = 0; j < 26; j++) {
        if (c > *(v[j].lower_bound(b - 1))) {
          C++;
        }
      }
      ans.push_back(C);
    }
  }
  for (auto &e : ans)
    cout << e << endl;
  return 0;
}
Problem p02763 (C++)

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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

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;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;

int N, Q;
string S;
int type[20001], idx[20001], l[20001], r[20001];
char c[20001];
set<int> st[26];

void input() {
  cin >> N >> S >> Q;
  for (int q = 0; q < Q; ++q) {
    cin >> type[q];
    if (type[q] == 1) {
      cin >> idx[q] >> c[q];
    } else {
      cin >> l[q] >> r[q];
    }
  }
}

void solve() {
  for (int i = 0; i < 26; ++i) {
    st[i].clear();
    st[i].insert(N);
  }
  for (int i = 0; i < N; ++i)
    st[(int)(S[i] - 'a')].insert(i);
  for (int q = 0; q < Q; +q) {
    if (type[q] == 1) {
      st[(int)(S[idx[q] - 1] - 'a')].erase(idx[q] - 1);
      S[idx[q] - 1] = c[q];
      st[(int)(S[idx[q] - 1] - 'a')].insert(idx[q] - 1);
    } else {
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        auto itr = st[i].lower_bound(l[q] - 1);
        if (*itr <= r[q] - 1)
          ans++;
      }
      cout << ans << endl;
    }
  }
  return;
}

int main() {
  input();
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

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;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
// const ll mod = 1000000007;

int N, Q;
string S;
int type[20001], idx[20001], l[20001], r[20001];
char c[20001];
set<int> st[26];

void input() {
  cin >> N >> S >> Q;
  for (int q = 0; q < Q; ++q) {
    cin >> type[q];
    if (type[q] == 1) {
      cin >> idx[q] >> c[q];
    } else {
      cin >> l[q] >> r[q];
    }
  }
}

void solve() {
  for (int i = 0; i < 26; ++i) {
    st[i].clear();
    st[i].insert(N);
  }
  for (int i = 0; i < N; ++i)
    st[(int)(S[i] - 'a')].insert(i);
  for (int q = 0; q < Q; ++q) {
    if (type[q] == 1) {
      st[(int)(S[idx[q] - 1] - 'a')].erase(idx[q] - 1);
      S[idx[q] - 1] = c[q];
      st[(int)(S[idx[q] - 1] - 'a')].insert(idx[q] - 1);
    } else {
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        auto itr = st[i].lower_bound(l[q] - 1);
        if (*itr <= r[q] - 1)
          ans++;
      }
      cout << ans << endl;
    }
  }
  return;
}

int main() {
  input();
  solve();
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
int N;
string S;
int Q;
int qtype[20202];
int i[20202], l[20202], r[20202];
char c[20202];
set<int> char_pos[30];
vector<int> ans;

int main() {
  cin >> N;
  cin >> S;
  cin >> Q;

  for (int i = 0; i < N; i++) {
    char si = S[i];
    char_pos[si - 'a'].insert(i);
  }

  rep(j, 0, Q) {
    cin >> qtype[j];
    if (qtype[j] == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      char si = S[i];
      char_pos[si - 'a'].erase(i);
      S[i] = c;
      char_pos[c - 'a'].insert(i);
    } else {
      ll l, r;
      cin >> l >> r;
      l--;
      int cnt = 0;
      for (int k = 0; k < 26; k++) {
        auto cp = char_pos[k];
        auto lb = cp.lower_bound(l);
        if (lb != cp.end() && *lb < r) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, begin, n) for (int i = begin; i < n; i++)
#define repe(i, begin, n) for (int i = begin; i <= n; i++)
#define repr(i, begin, n) for (int i = begin; i > begin - n; i--)
#define repre(i, begin, end) for (int i = begin; i >= end; i--)

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

// -------------------------------------------------------
int N;
string S;
int Q;
int qtype[20202];
int i[20202], l[20202], r[20202];
char c[20202];
set<int> char_pos[30];
vector<int> ans;

int main() {
  cin >> N;
  cin >> S;
  cin >> Q;

  for (int i = 0; i < N; i++) {
    char si = S[i];
    char_pos[si - 'a'].insert(i);
  }

  rep(j, 0, Q) {
    cin >> qtype[j];
    if (qtype[j] == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      char si = S[i];
      char_pos[si - 'a'].erase(i);
      S[i] = c;
      char_pos[c - 'a'].insert(i);
    } else {
      ll l, r;
      cin >> l >> r;
      l--;
      int cnt = 0;
      for (int k = 0; k < 26; k++) {
        auto lb = char_pos[k].lower_bound(l);
        if (lb != char_pos[k].end() && *lb < r) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define X first
#define Y second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ini(x, y) memset(x, y, sizeof(x))
#define endl '\n'
#define fastio                                                                 \
  cin.sync_with_stdio(false);                                                  \
  cin.tie(nullptr)
using namespace std;

using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7;
const int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

const int SZ = 1 << 3;
bool tree[26][SZ << 1];

void update(int k, int x, int v) {
  for (tree[k][x += SZ] = v; x >>= 1;)
    tree[k][x] = tree[k][x << 1] | tree[k][x << 1 | 1];
}
bool query(int k, int L, int R, int idx = 1, int s = 0, int e = SZ - 1) {
  if (e < L || R < s)
    return 0;
  if (L <= s && e <= R)
    return tree[k][idx];
  int m = s + e >> 1;
  return query(k, L, R, idx << 1, s, m) |
         query(k, L, R, idx << 1 | 1, m + 1, e);
}
int main() {
  fastio;
  int N;
  cin >> N;

  string S;
  cin >> S;

  for (int i = 0; i < N; ++i) {
    update(S[i] - 97, i, 1);
  }

  int Q;
  cin >> Q;

  for (int q; Q--;) {
    cin >> q;
    if (q == 1) {
      int a;
      char b;
      cin >> a >> b;
      update(S[a - 1] - 97, a - 1, 0);
      update(b - 97, a - 1, 1);
      S[a - 1] = b;
    } else {
      int a, b, cnt = 0;
      cin >> a >> b;
      for (int i = 0; i < 26; ++i) {
        cnt += query(i, a - 1, b - 1);
      }
      cout << cnt << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define X first
#define Y second
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define ini(x, y) memset(x, y, sizeof(x))
#define endl '\n'
#define fastio                                                                 \
  cin.sync_with_stdio(false);                                                  \
  cin.tie(nullptr)
using namespace std;

using ll = long long;
using pii = pair<int, int>;
const int MOD = 1e9 + 7;
const int dx[] = {-1, 0, 1, 0, -1, 1, 1, -1};
const int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};

const int SZ = 1 << 19;
bool tree[26][SZ << 1];

void update(int k, int x, int v) {
  for (tree[k][x += SZ] = v; x >>= 1;)
    tree[k][x] = tree[k][x << 1] | tree[k][x << 1 | 1];
}
bool query(int k, int L, int R, int idx = 1, int s = 0, int e = SZ - 1) {
  if (e < L || R < s)
    return 0;
  if (L <= s && e <= R)
    return tree[k][idx];
  int m = s + e >> 1;
  return query(k, L, R, idx << 1, s, m) |
         query(k, L, R, idx << 1 | 1, m + 1, e);
}
int main() {
  fastio;
  int N;
  cin >> N;

  string S;
  cin >> S;

  for (int i = 0; i < N; ++i) {
    update(S[i] - 97, i, 1);
  }

  int Q;
  cin >> Q;

  for (int q; Q--;) {
    cin >> q;
    if (q == 1) {
      int a;
      char b;
      cin >> a >> b;
      update(S[a - 1] - 97, a - 1, 0);
      update(b - 97, a - 1, 1);
      S[a - 1] = b;
    } else {
      int a, b, cnt = 0;
      cin >> a >> b;
      for (int i = 0; i < 26; ++i) {
        cnt += query(i, a - 1, b - 1);
      }
      cout << cnt << endl;
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

struct segtree {
  int N;
  vector<int> dat;
  segtree(int N_) : dat(N_ * 2) {
    int x = 1;
    while (N_ > x) {
      x *= 2;
    }
    N = x;
  }
  void update(int i, int x) {
    i += N - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = dat[i * 2 + 1] | dat[i * 2 + 2]; // 演算
    }
  }
  int query(int a, int b) { return query_sub(a, b, 0, 0, N); }
  int query_sub(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0; // モノイド
    else if (a <= l && r <= b)
      return dat[k];
    else {
      int vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return vl | vr; // 演算
    }
  }
};

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  segtree tr(n);
  REP(i, n) { tr.update(i, 1 << (s[i] - 'a')); }
  int q;
  cin >> q;
  while (q--) {
    int a;
    cin >> a;
    if (a == 1) {
      int b;
      char c;
      cin >> b >> c;
      tr.update(b - 1, 1 << (c - 'a'));
    } else {
      int l, r;
      cin >> l >> r;
      int k = tr.query(l - 1, r);
      int ans = 0;
      REP(i, 26) ans += (k >> i) & 1;
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
typedef vector<ll> VI;
typedef vector<VI> VVI;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define REP(i, n) for (int i = 0; i < n; i++)
#define ALL(v) v.begin(), v.end()

struct segtree {
  int N;
  vector<int> dat;
  segtree(int N_) : dat(N_ * 4) {
    int x = 1;
    while (N_ > x) {
      x *= 2;
    }
    N = x;
  }
  void update(int i, int x) {
    i += N - 1;
    dat[i] = x;
    while (i > 0) {
      i = (i - 1) / 2;
      dat[i] = dat[i * 2 + 1] | dat[i * 2 + 2]; // 演算
    }
  }
  int query(int a, int b) { return query_sub(a, b, 0, 0, N); }
  int query_sub(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l)
      return 0; // モノイド
    else if (a <= l && r <= b)
      return dat[k];
    else {
      int vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
      int vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
      return vl | vr; // 演算
    }
  }
};

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  segtree tr(n);
  REP(i, n) { tr.update(i, 1 << (s[i] - 'a')); }
  int q;
  cin >> q;
  while (q--) {
    int a;
    cin >> a;
    if (a == 1) {
      int b;
      char c;
      cin >> b >> c;
      tr.update(b - 1, 1 << (c - 'a'));
    } else {
      int l, r;
      cin >> l >> r;
      int k = tr.query(l - 1, r);
      int ans = 0;
      REP(i, 26) ans += (k >> i) & 1;
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, q;
const int maxn = 5e5 + 5;
int lt[maxn];
set<int> a[26];

bool check(int k, int l, int r) {
  int b;
  if (a[k].lower_bound(l) == a[k].end())
    return false;
  b = *a[k].lower_bound(l);
  //	cout << "csy" << ' ' << k << ' ' << b << endl;
  if (b <= r)
    return true;
  else
    return false;
}

int calc(int l, int r) {
  int ans = 0;
  for (int i = 0; i < 26; i++) {
    if (check(i, l, r))
      ans++;
  }
  return ans;
}

int main() {
  cin >> n;
  char p;
  for (int i = 1; i <= n; i++) {
    p = getchar();
    if (p == '\n') {
      i--;
      continue;
    }
    a[(int)p - 97].insert(i);
    lt[i] = (int)p - 97;
  }
  cin >> q;
  for (int i = 1, t, nx, ny; i <= q; i++) {
    cin >> t;
    if (t == 1) {
      cin >> nx >> p;
      ny = (int)p - 97;
      if (lt[nx] == ny)
        continue;
      else {
        a[lt[nx]].erase(a[lt[nx]].find(nx));
        a[ny].insert(nx);
      }
    } else {
      cin >> nx >> ny;
      cout << calc(nx, ny) << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, q;
const int maxn = 5e5 + 5;
int lt[maxn];
set<int> a[26];

bool check(int k, int l, int r) {
  int b;
  if (a[k].lower_bound(l) == a[k].end())
    return false;
  b = *a[k].lower_bound(l);
  //	cout << "csy" << ' ' << k << ' ' << b << endl;
  if (b <= r)
    return true;
  else
    return false;
}

int calc(int l, int r) {
  int ans = 0;
  for (int i = 0; i < 26; i++) {
    if (check(i, l, r))
      ans++;
  }
  return ans;
}

int main() {
  cin >> n;
  char p;
  for (int i = 1; i <= n; i++) {
    p = getchar();
    if (p == '\n') {
      i--;
      continue;
    }
    a[(int)p - 97].insert(i);
    lt[i] = (int)p - 97;
  }
  cin >> q;
  for (int i = 1, t, nx, ny; i <= q; i++) {
    cin >> t;
    if (t == 1) {
      cin >> nx >> p;
      ny = (int)p - 97;
      if (lt[nx] == ny)
        continue;
      else {
        a[lt[nx]].erase(a[lt[nx]].find(nx));
        a[ny].insert(nx);
        lt[nx] = ny;
      }
    } else {
      cin >> nx >> ny;
      cout << calc(nx, ny) << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long LL;
const int N = 1e6 + 50;
int node[N][28], num[N], u, v, type, ql, qr, p, n, q;
int alpha[30];
char s[N], ch;

void update(int o, int L, int R) {
  int M = L + (R - L) / 2;
  if (L == R) {
    node[o][u]--;
    node[o][v]++;
  } else {
    // int cnt = 0;
    if (p <= M)
      update(o * 2, L, M);
    else
      update(o * 2 + 1, M + 1, R);
    for (int i = 0; i < 26; i++) {
      node[o][i] = node[o * 2][i] + node[o * 2 + 1][i];
    }
  }
}

void query(int o, int L, int R) {
  int M = L + (R - L) / 2;
  if (ql <= L && R <= qr) {
    for (int i = 0; i < 26; i++)
      alpha[i] += node[o][i];
    return;
  }
  if (ql <= M)
    query(o * 2, L, M);
  if (M < qr)
    query(o * 2 + 1, M + 1, R);
}

int main() {
  cin >> n;
  scanf("%s", s);
  for (int i = 1; i <= n; i++) {
    num[i] = s[i - 1] - 'a';
    u = 27;
    v = num[i];
    p = i;
    update(1, 1, n);
  }
  cin >> q;
  while (q--) {
    cin >> type;
    if (type == 2) {
      scanf("%d%d", &ql, &qr);
      memset(alpha, 0, sizeof(alpha));
      query(1, 1, n);
      int ret = 0;
      for (int i = 0; i < 26; i++) {
        if (alpha[i] > 0)
          ret++;
      }
      cout << ret << endl;
    } else {
      cin >> ql >> ch;
      u = num[ql];
      v = ch - 'a';
      num[ql] = v;
      p = ql;
      update(1, 1, n);
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 50;
int node[N][28], num[N], u, v, type, ql, qr, p, n, q;
int alpha[30];
char s[N], ch;

void update(int o, int L, int R) {
  int M = L + (R - L) / 2;
  if (L == R) {
    node[o][u]--;
    node[o][v]++;
  } else {
    // int cnt = 0;
    if (p <= M)
      update(o * 2, L, M);
    else
      update(o * 2 + 1, M + 1, R);
    for (int i = 0; i < 26; i++) {
      node[o][i] = node[o * 2][i] + node[o * 2 + 1][i];
    }
  }
}

void query(int o, int L, int R) {
  int M = L + (R - L) / 2;
  if (ql <= L && R <= qr) {
    for (int i = 0; i < 26; i++)
      alpha[i] += node[o][i];
    return;
  }
  if (ql <= M)
    query(o * 2, L, M);
  if (M < qr)
    query(o * 2 + 1, M + 1, R);
}

int main() {
  cin >> n;
  scanf("%s", s);
  for (int i = 1; i <= n; i++) {
    num[i] = s[i - 1] - 'a';
    u = 27;
    v = num[i];
    p = i;
    update(1, 1, n);
  }
  cin >> q;
  while (q--) {
    cin >> type;
    if (type == 2) {
      scanf("%d%d", &ql, &qr);
      memset(alpha, 0, sizeof(alpha));
      query(1, 1, n);
      int ret = 0;
      for (int i = 0; i < 26; i++) {
        if (alpha[i] > 0)
          ret++;
      }
      cout << ret << endl;
    } else {
      cin >> ql >> ch;
      u = num[ql];
      v = ch - 'a';
      num[ql] = v;
      p = ql;
      update(1, 1, n);
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

signed main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<set<int>> v(26);
  for (int i = 0; i < n; ++i)
    v[s[i] - 'a'].insert(i);
  for (int i = 0; i < q; ++i) {
    int type;
    cin >> type;
    if (type == 1) {
      int idx;
      char c;
      cin >> idx >> c;
      --idx;
      v[s[i] - 'a'].erase(idx);
      v[c - 'a'].insert(idx);
      s[idx] = c;
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      int ans = 0;
      for (int j = 0; j < 26; ++j)
        ans += (v[j].lower_bound(l) != v[j].lower_bound(r));
      cout << ans << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

signed main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<set<int>> v(26);
  for (int i = 0; i < n; ++i)
    v[s[i] - 'a'].insert(i);
  for (int i = 0; i < q; ++i) {
    int type;
    cin >> type;
    if (type == 1) {
      int idx;
      char c;
      cin >> idx >> c;
      --idx;
      v[s[idx] - 'a'].erase(idx);
      v[c - 'a'].insert(idx);
      s[idx] = c;
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      int ans = 0;
      for (int j = 0; j < 26; ++j)
        ans += (v[j].lower_bound(l) != v[j].lower_bound(r));
      cout << ans << endl;
    }
  }
}
Problem p02763 (C++)

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

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

/*
18 26
*/
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;

  vector<set<int>> a(26);
  for (int i = 0; i < n; i++) {
    a[s[i] - 'a'].insert(i);
  }

  for (int i = 0; i < q; i++) {
    int type;
    cin >> type;
    if (type == 1) {
      int index;
      char new_c;
      cin >> index >> new_c;
      index--;
      a[s[index] - 'a'].erase(index);
      s[index] = new_c;
      a[s[index] - 'a'].insert(index);
    } else {
      int l, r;
      cin >> l >> r;
      l--, r--;
      int cnt = 0;
      for (int j = 0; j < 26; j++) {
        auto lower_it = lower_bound(a[j].begin(), a[j].end(), l);
        auto upper_it = upper_bound(a[j].begin(), a[j].end(), r);
        if (lower_it != upper_it)
          cnt++;
      }
      cout << cnt << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#include <math.h>

using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
typedef long long ll;
using Graph = vector<vector<int>>;
typedef long long ll;
typedef pair<int, int> P;

const int MOD = 1000000007;
const int INF_32 = 1LL << 30;
const int64_t INF_64 = 1LL << 60;

/*
18 26
*/
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;

  vector<set<int>> a(26);
  for (int i = 0; i < n; i++) {
    a[s[i] - 'a'].insert(i);
  }

  for (int i = 0; i < q; i++) {
    int type;
    cin >> type;
    if (type == 1) {
      int index;
      char new_c;
      cin >> index >> new_c;
      index--;
      a[s[index] - 'a'].erase(index);
      s[index] = new_c;
      a[s[index] - 'a'].insert(index);
    } else {
      int l, r;
      cin >> l >> r;
      l--, r--;
      int cnt = 0;
      for (int j = 0; j < 26; j++) {
        auto lower_it = a[j].lower_bound(l);
        auto upper_it = a[j].upper_bound(r);
        if (lower_it != upper_it)
          cnt++;
      }
      cout << cnt << endl;
    }
  }
}
Problem p02763 (C++)

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

using namespace std;

int n;
string s;
int q;
set<int> occ[26];

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  cin >> n >> s >> q;
  for (int i = 0; i < n; i++)
    occ[(int)s[i] - (int)'a'].insert(i + 1);

  for (int _ = 0; _ < q; _++) {
    int type;
    cin >> type;

    if (type == 1) {
      int x;
      char y;
      cin >> x >> y;
      if (s[x - 1] != y) {
        occ[(int)s[x - 1] - (int)'a'].erase(x);
        occ[(int)y - (int)'a'].insert(x);
        s[x - 1] = y;
      }
    } else {
      int x, y;
      cin >> x >> y;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        auto l = lower_bound(occ[i].begin(), occ[i].end(), x);
        if (l != occ[i].end() && *l <= y)
          ans++;
      }
      cout << ans << "\n";
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int n;
string s;
int q;
set<int> occ[26];

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  cin >> n >> s >> q;
  for (int i = 0; i < n; i++)
    occ[(int)s[i] - (int)'a'].insert(i + 1);

  for (int _ = 0; _ < q; _++) {
    int type;
    cin >> type;

    if (type == 1) {
      int x;
      char y;
      cin >> x >> y;
      if (s[x - 1] != y) {
        occ[(int)s[x - 1] - (int)'a'].erase(x);
        occ[(int)y - (int)'a'].insert(x);
        s[x - 1] = y;
      }
    } else {
      int x, y;
      cin >> x >> y;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        auto l = occ[i].lower_bound(x);
        if (l != occ[i].end() && *l <= y)
          ans++;
      }
      cout << ans << "\n";
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(void) {
  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<set<int>> is(26);
  rep(i, s.size()) { is[s[i] - 'a'].insert(i); }
  rep(qi, q) {
    int t;
    cin >> t;
    if (t == 1) {
      int i;
      char c;
      cin >> i >> c;
      --i;
      is[s[i] - 'a'].erase(i);
      s[i] = c;
      is[s[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      --r;
      int ans = 0;
      rep(i, 26) {
        auto itr = lower_bound(is[i].begin(), is[i].end(), l);
        if (itr != is[i].end() && *itr <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(void) {
  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<set<int>> is(26);
  rep(i, s.size()) { is[s[i] - 'a'].insert(i); }
  rep(qi, q) {
    int t;
    cin >> t;
    if (t == 1) {
      int i;
      char c;
      cin >> i >> c;
      --i;
      is[s[i] - 'a'].erase(i);
      s[i] = c;
      is[s[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      --r;
      int ans = 0;
      rep(i, 26) {
        auto itr = is[i].lower_bound(l);
        if (itr != is[i].end() && *itr <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define maxs(a, b) a = max(a, b)
#define mins(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll linf = (1ll << 61);
const int inf = 1001001001;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<set<int>> is(26);
  rep(i, n) { is[s[i] - 'a'].insert(i); }
  int q;
  cin >> q;
  while (q--) {
    int f;
    cin >> f;
    if (f == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      is[s[i] - 'a'].erase(i);
      s[i] = c;
      is[s[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(i, 26) {
        auto itr = lower_bound(is[i].begin(), is[i].end(), l);
        if (itr != is[i].end() && *itr <= r)
          ans++;
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define maxs(a, b) a = max(a, b)
#define mins(a, b) a = min(a, b)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll linf = (1ll << 61);
const int inf = 1001001001;
const int mod = 1000000007;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<set<int>> is(26);
  rep(i, n) { is[s[i] - 'a'].insert(i); }
  int q;
  cin >> q;
  while (q--) {
    int f;
    cin >> f;
    if (f == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      is[s[i] - 'a'].erase(i);
      s[i] = c;
      is[s[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(i, 26) {
        auto itr = is[i].lower_bound(l);
        if (itr != is[i].end() && *itr <= r)
          ans++;
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define _USE_CMATH_DEFINES

const long INF = (1l << 30);
const long LINF = (1l << 60); // 1.15*10^18

int main() {
  long n, q;
  std::string s;
  std::set<long> pos[26];
  scanf("%ld", &n);
  std::cin >> s;
  scanf("%ld", &q);
  for (int i = 0; i < n; i++) {
    pos[s[i] - 'a'].insert(i);
  }
  for (int i = 0; i < q; i++) {
    long x;
    scanf("%ld", &x);
    if (x == 1) {
      long ind;
      char c;
      scanf("%ld %c", &ind, &c);
      ind--;
      // printf("%ld %c\n", ind, c);
      pos[s[ind] - 'a'].erase(pos[s[ind] - 'a'].find(ind));
      pos[c - 'a'].insert(ind);
      /*
      for(char c = 'a'; c <= 'z'; c++){
          printf("  %c : (", c);
          for(auto it = pos[c-'a'].begin(); it != pos[c-'a'].end(); it++){
              printf("%ld ", *it);
          }
          printf(")\n");
      }
      //*/
    } else {
      long l, r;
      scanf("%ld%ld", &l, &r);
      l--;
      r--;
      long ans = 0;
      for (int j = 0; j < 26; j++) {
        if (pos[j].lower_bound(l) != pos[j].lower_bound(r + 1)) {
          ans++;
        }
      }
      printf("%ld\n", ans);
    }
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdlib>
#include <deque>
#include <fstream>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#define _USE_CMATH_DEFINES

const long INF = (1l << 30);
const long LINF = (1l << 60); // 1.15*10^18

int main() {
  long n, q;
  std::string s;
  std::set<long> pos[26];
  scanf("%ld", &n);
  std::cin >> s;
  scanf("%ld", &q);
  for (int i = 0; i < n; i++) {
    pos[s[i] - 'a'].insert(i);
  }
  for (int i = 0; i < q; i++) {
    long x;
    scanf("%ld", &x);
    if (x == 1) {
      long ind;
      char c;
      scanf("%ld %c", &ind, &c);
      ind--;
      // printf("%ld %c\n", ind, c);
      pos[s[ind] - 'a'].erase(pos[s[ind] - 'a'].find(ind));
      s[ind] = c;
      pos[c - 'a'].insert(ind);
      /*
      for(char c = 'a'; c <= 'z'; c++){
          printf("  %c : (", c);
          for(auto it = pos[c-'a'].begin(); it != pos[c-'a'].end(); it++){
              printf("%ld ", *it);
          }
          printf(")\n");
      }
      //*/
    } else {
      long l, r;
      scanf("%ld%ld", &l, &r);
      l--;
      r--;
      long ans = 0;
      for (int j = 0; j < 26; j++) {
        if (pos[j].lower_bound(l) != pos[j].lower_bound(r + 1)) {
          ans++;
        }
      }
      printf("%ld\n", ans);
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  string t;
  cin >> t;
  char s[n];
  for (int i = 0; i < n; i++)
    s[i] = t[i];
  int q;
  cin >> q;
  set<int> m[26];
  for (int i = 0; i < n; i++) {
    m[s[i] - 'a'].insert(i);
  }
  for (int _ = 0; _ < q; _++) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      --i;
      char prev = s[i];
      m[prev - 'a'].erase(i);
      m[c - 'a'].insert(i);
      s[i] = c;
      continue;
    }
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    int cnt = 0;
    for (int i = 0; i < 26; i++) {
      char c = 'a' + i;
      auto it = lower_bound(m[c - 'a'].begin(), m[c - 'a'].end(), l);
      if (it != m[c - 'a'].end()) {
        if (*it <= r) {
          ++cnt;
        }
      }
    }
    cout << cnt << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  string t;
  cin >> t;
  char s[n];
  for (int i = 0; i < n; i++)
    s[i] = t[i];
  int q;
  cin >> q;
  set<int> m[26];
  for (int i = 0; i < n; i++) {
    m[s[i] - 'a'].insert(i);
  }
  for (int _ = 0; _ < q; _++) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      --i;
      char prev = s[i];
      m[prev - 'a'].erase(i);
      m[c - 'a'].insert(i);
      s[i] = c;
      continue;
    }
    int l, r;
    cin >> l >> r;
    --l;
    --r;
    int cnt = 0;
    for (int i = 0; i < 26; i++) {
      char c = 'a' + i;
      auto it = m[c - 'a'].lower_bound(l);
      if (it != m[c - 'a'].end()) {
        if (*it <= r) {
          ++cnt;
        }
      }
    }
    cout << cnt << endl;
  }
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int N = 5e5;

inline int tog_bit(int x, int mask) { return (1 << x) ^ mask; }

int n, q;
char s[N + 5];

int ST[N + 5];

void build(int node, int l, int r) {
  if (l == r) {
    ST[node] = tog_bit(s[l - 1] - 'a', 0);
    return;
  }
  int mid = (l + r) / 2;
  build(node * 2, l, mid);
  build(node * 2 + 1, mid + 1, r);
  ST[node] = ST[node * 2] | ST[node * 2 + 1];
  return;
}

void update(int node, int l, int r, int id, char x) {
  if (l == r) {
    ST[node] = tog_bit(x - 'a', 0);
    return;
  }
  int mid = (l + r) / 2;
  if (id <= mid)
    update(node * 2, l, mid, id, x);
  else
    update(node * 2 + 1, mid + 1, r, id, x);
  ST[node] = ST[node * 2] | ST[node * 2 + 1];
  return;
}

int query(int node, int l, int r, int ql, int qr) {
  if (l >= ql && r <= qr)
    return ST[node];
  if (l > qr || r < ql)
    return 0;
  int mid = (l + r) / 2;
  return query(node * 2, l, mid, ql, qr) |
         query(node * 2 + 1, mid + 1, r, ql, qr);
}

int get_cnt(int x) {
  int ret = 0;
  for (int i = 0; i < 26; i++) {
    if ((1 << i) & x)
      ret++;
  }
  return ret;
}

int main() {
  scanf("%d", &n);
  scanf("%s", s);
  build(1, 1, n);
  scanf("%d", &q);
  while (q--) {
    int com;
    scanf("%d", &com);
    if (com == 1) {
      int id;
      char ub;
      scanf("%d %c", &id, &ub);
      update(1, 1, n, id, ub);
    } else {
      int l, r;
      scanf("%d %d", &l, &r);
      int ans = query(1, 1, n, l, r);
      printf("%d\n", get_cnt(ans));
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const int N = 5e5;

inline int tog_bit(int x, int mask) { return (1 << x) ^ mask; }

int n, q;
char s[N + 5];

int ST[4 * N + 5];

void build(int node, int l, int r) {
  if (l == r) {
    ST[node] = tog_bit(s[l - 1] - 'a', 0);
    return;
  }
  int mid = (l + r) / 2;
  build(node * 2, l, mid);
  build(node * 2 + 1, mid + 1, r);
  ST[node] = ST[node * 2] | ST[node * 2 + 1];
  return;
}

void update(int node, int l, int r, int id, char x) {
  if (l == r) {
    ST[node] = tog_bit(x - 'a', 0);
    return;
  }
  int mid = (l + r) / 2;
  if (id <= mid)
    update(node * 2, l, mid, id, x);
  else
    update(node * 2 + 1, mid + 1, r, id, x);
  ST[node] = ST[node * 2] | ST[node * 2 + 1];
  return;
}

int query(int node, int l, int r, int ql, int qr) {
  if (l >= ql && r <= qr)
    return ST[node];
  if (l > qr || r < ql)
    return 0;
  int mid = (l + r) / 2;
  return query(node * 2, l, mid, ql, qr) |
         query(node * 2 + 1, mid + 1, r, ql, qr);
}

int get_cnt(int x) {
  int ret = 0;
  for (int i = 0; i < 26; i++) {
    if ((1 << i) & x)
      ret++;
  }
  return ret;
}

int main() {
  scanf("%d", &n);
  scanf("%s", s);
  build(1, 1, n);
  scanf("%d", &q);
  while (q--) {
    int com;
    scanf("%d", &com);
    if (com == 1) {
      int id;
      char ub;
      scanf("%d %c", &id, &ub);
      update(1, 1, n, id, ub);
    } else {
      int l, r;
      scanf("%d %d", &l, &r);
      int ans = query(1, 1, n, l, r);
      printf("%d\n", get_cnt(ans));
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
const int N = 1e5 + 20, C = 26;
char a[N];
int n, q, type, pos, l, r;
char c;
set<int> s[C];
int main() {
  scanf("%d %s%d", &n, a + 1, &q);
  for (int i = 1; i <= n; i++)
    s[a[i] - 'a'].insert(i);

  while (q--) {
    scanf("%d", &type);
    if (type == 1) {
      scanf("%d %c", &pos, &c);
      s[a[pos] - 'a'].erase(pos);
      s[c - 'a'].insert(pos);
      a[pos] = c;
    } else {
      scanf("%d%d", &l, &r);

      int ans = 0;
      for (int i = 0; i < C; i++) {
        auto it = s[i].lower_bound(l);
        if (it == s[i].end() or *it > r)
          continue;
        ans++;
      }
      printf("%d\n", ans);
    }
  }
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
const int N = 5e5 + 20, C = 26;
char a[N];
int n, q, type, pos, l, r;
char c;
set<int> s[C];
int main() {
  scanf("%d %s%d", &n, a + 1, &q);
  for (int i = 1; i <= n; i++)
    s[a[i] - 'a'].insert(i);

  while (q--) {
    scanf("%d", &type);
    if (type == 1) {
      scanf("%d %c", &pos, &c);
      s[a[pos] - 'a'].erase(pos);
      s[c - 'a'].insert(pos);
      a[pos] = c;
    } else {
      scanf("%d%d", &l, &r);

      int ans = 0;
      for (int i = 0; i < C; i++) {
        auto it = s[i].lower_bound(l);
        if (it == s[i].end() or *it > r)
          continue;
        ans++;
      }
      printf("%d\n", ans);
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, Q, qi;
  string S;
  char c;
  cin >> N;
  cin >> S;
  cin >> Q;
  vector<set<int>> is(26);
  for (int i = 0; i < N; i++) {
    is[S[i] - 'a'].insert(i); // Sのi番目の文字の欄に何番目に出たかを記録
  }
  int l, r;
  for (int i = 0; i < Q; i++) {
    cin >> qi;
    if (qi == 1) {
      int ind;
      char c;
      cin >> ind >> c;
      ind--;
      is[S[ind] - 'a'].erase(ind); // S[i]がind番目に出るという情報を削除
      S[i] = c;                    // Sの情報を書き換え
      is[S[ind] - 'a'].insert(
          ind); // c(=S[i]-'a')がind番目に出るという情報を追加
    } else {
      cin >> l >> r;
      int ans = 0;
      l--;
      for (int i = 0; i < 26; i++) {
        // i番目の文字がｌ何番目以降（i番目含む）に初めて出る場所を調べる
        auto itr = is[i].lower_bound(l);
        // それが指定範囲内にあれば
        if (itr != is[i].end() && *itr < r) {
          ans++;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;

int main() {
  int N, Q, qi;
  string S;
  char c;
  cin >> N;
  cin >> S;
  cin >> Q;
  vector<set<int>> is(26);
  for (int i = 0; i < N; i++) {
    is[S[i] - 'a'].insert(i); // Sのi番目の文字の欄に何番目に出たかを記録
  }
  int l, r;
  for (int i = 0; i < Q; i++) {
    cin >> qi;
    if (qi == 1) {
      int ind;
      char c;
      cin >> ind >> c;
      ind--;
      is[S[ind] - 'a'].erase(ind); // S[i]がind番目に出るという情報を削除
      S[ind] = c;                  // Sの情報を書き換え
      is[S[ind] - 'a'].insert(
          ind); // c(=S[i]-'a')がind番目に出るという情報を追加
    } else {
      cin >> l >> r;
      int ans = 0;
      l--;
      for (int i = 0; i < 26; i++) {
        // i番目の文字がｌ何番目以降（i番目含む）に初めて出る場所を調べる
        auto itr = is[i].lower_bound(l);
        // それが指定範囲内にあれば
        if (itr != is[i].end() && *itr < r) {
          ans++;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;
  vector<set<int>> count(26);
  rep(i, n) { count[s[i] - 'a'].insert(i); }
  rep(qi, q) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      count[s[i] - 'a'].erase(i);
      s[i] = c;
      count[c - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(i, 26) {
        auto it = lower_bound(count[i].begin(), count[i].end(), l);
        if (it != count[i].end() && *it <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, N) for (ll(i) = 0; (i) < (N); (i)++)
const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;
  vector<set<int>> count(26);
  rep(i, n) { count[s[i] - 'a'].insert(i); }
  rep(qi, q) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      count[s[i] - 'a'].erase(i);
      s[i] = c;
      count[c - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(i, 26) {
        auto it = count[i].lower_bound(l);
        if (it != count[i].end() && *it <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok ? "Yes" : "No");
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef set<int> si;
typedef map<string, int> msi;
typedef greater<int> gt;
typedef priority_queue<int, vector<int>, gt> minq;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
// clang++ -std=c++11 -stdlib=libc++

int main() {
  int n, q;
  string s;
  cin >> n;
  cin >> s;
  cin >> q;
  vector<set<int>> sets(26);
  rep(i, n) { sets[s[i] - 'a'].insert(i); }
  rep(_, q) {
    int a;
    cin >> a;
    if (a == 1) {
      int i;
      char c;
      cin >> i >> c;
      --i;
      sets[s[i] - 'a'].erase(i);
      s[i] = c;
      sets[s[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      --r;
      int num = 0;
      rep(i, 26) {
        set<int> si = sets[i];
        auto it = si.lower_bound(l);
        if (it != si.end() && *it <= r)
          ++num;
      }
      cout << num << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok ? "Yes" : "No");
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef set<int> si;
typedef map<string, int> msi;
typedef greater<int> gt;
typedef priority_queue<int, vector<int>, gt> minq;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
// clang++ -std=c++11 -stdlib=libc++

int main() {
  int n, q;
  string s;
  cin >> n;
  cin >> s;
  cin >> q;
  vector<set<int>> sets(26);
  rep(i, n) { sets[s[i] - 'a'].insert(i); }
  rep(_, q) {
    int a;
    cin >> a;
    if (a == 1) {
      int i;
      char c;
      cin >> i >> c;
      --i;
      sets[s[i] - 'a'].erase(i);
      s[i] = c;
      sets[s[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      --r;
      int num = 0;
      rep(i, 26) {
        auto it = sets[i].lower_bound(l);
        if (it != sets[i].end() && *it <= r)
          ++num;
      }
      cout << num << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cout << ' ';
    cout << elem;
  }
  cout << endl;
}

// 解説参考.
int main() {
  int n;
  string s;
  int q;
  cin >> n >> s >> q;

  set<int> st[26]; // aからzそれぞれの位置を記録.
  REP(i, n) st[s[i] - 'a'].insert(i);
  REP(i, 26) st[i].insert(n); // 番兵.

  REP(i, q) {
    int t;
    cin >> t;

    if (t == 1) {
      int j;
      char c;
      cin >> j >> c;
      j--;

      REP(k, 26) {
        if (st[k].find(j) == st[k].end()) {
        } else
          st[k].erase(j);
      }
      st[c - 'a'].insert(j);
    } else {
      int l, r;
      cin >> l >> r;
      l--, r--;

      int ans = 0;
      REP(j, 26) {
        int p = *lower_bound(ALL(st[j]), l); // l以上の値を習得.
        if (p <= r)
          ans++; // 上記の値がr以下だったらok.
      }
      cout << ans << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define debug(x) cerr << #x << ": " << (x) << endl
#define INF (int)1e9
#define EPS (double)1e-9
#define MOD ((int)1e9 + 7)
using namespace std;
typedef long long llong;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> pii;
template <class Type> void line(const Type &a) {
  int cnt = 0;
  for (const auto &elem : a) {
    if (cnt++)
      cout << ' ';
    cout << elem;
  }
  cout << endl;
}

// 解説参考.
int main() {
  int n;
  string s;
  int q;
  cin >> n >> s >> q;

  set<int> st[26]; // aからzそれぞれの位置を記録.
  REP(i, n) st[s[i] - 'a'].insert(i);
  REP(i, 26) st[i].insert(n); // 番兵.

  REP(i, q) {
    int t;
    cin >> t;

    if (t == 1) {
      int j;
      char c;
      cin >> j >> c;
      j--;

      REP(k, 26) {
        if (st[k].find(j) == st[k].end()) {
        } else
          st[k].erase(j);
      }
      st[c - 'a'].insert(j);
    } else {
      int l, r;
      cin >> l >> r;
      l--, r--;

      int ans = 0;
      REP(j, 26) {
        int p = *st[j].lower_bound(l); // l以上の値を習得.
        if (p <= r)
          ans++; // 上記の値がr以下だったらok.
      }
      cout << ans << endl;
    }
  }
}