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

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

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

int main() {
  ll i, j;
  ll n;
  string s;
  cin >> n >> s;
  vector<set<ll>> vs(26);
  for (i = 0; i < n; ++i) {
    char tmp = s.at(i);
    vs.at(tmp - 'a').insert(i);
  }
  vector<char> ss(n);
  for (i = 0; i < n; ++i) {
    ss.at(i) = s.at(i);
  }
  ll q;
  cin >> q;
  for (i = 0; i < q; ++i) {
    ll com;
    cin >> com;
    if (com == 1) {
      ll ind;
      char c;
      cin >> ind >> c;
      --ind;
      /**
      char tmp = ss.at(ind);
      ss.at(ind) = c;
      vs.at(tmp - 'a').erase(ind);
      vs.at(c - 'a').insert(ind);
      **/
      rep(j, 26) { vs.at(j).erase(ind); }
      vs.at(c - 'a').insert(ind);
    } else {
      ll l, r;
      cin >> l >> r;
      ll ans = 0;
      rep(j, 26) {
        auto it1 = lower_bound(all(vs.at(j)), l - 1);
        auto it2 = lower_bound(all(vs.at(j)), r);
        if (it1 != it2)
          ++ans;
      }
      /**
      rep(j,26){
          auto it = vs.at(j).lower_bound(l-1);
          if(it != vs.at(j).end()){
              if(*it < r) ++ans;
          }
      }
      **/
      cout << ans << endl;
    }
  }
  return 0;
}

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

#define rep(i, n) for (i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
using ll = long long;

int main() {
  ll i, j;
  ll n;
  string s;
  cin >> n >> s;
  vector<set<ll>> vs(26);
  for (i = 0; i < n; ++i) {
    char tmp = s.at(i);
    vs.at(tmp - 'a').insert(i);
  }
  vector<char> ss(n);
  for (i = 0; i < n; ++i) {
    ss.at(i) = s.at(i);
  }
  ll q;
  cin >> q;
  for (i = 0; i < q; ++i) {
    ll com;
    cin >> com;
    if (com == 1) {
      ll ind;
      char c;
      cin >> ind >> c;
      --ind;
      /**
      char tmp = ss.at(ind);
      ss.at(ind) = c;
      vs.at(tmp - 'a').erase(ind);
      vs.at(c - 'a').insert(ind);
      **/
      rep(j, 26) { vs.at(j).erase(ind); }
      vs.at(c - 'a').insert(ind);
    } else {
      ll l, r;
      cin >> l >> r;
      ll ans = 0;
      rep(j, 26) {
        auto it1 = vs.at(j).lower_bound(l - 1);
        auto it2 = vs.at(j).lower_bound(r);
        if (it1 != it2)
          ++ans;
      }
      /**
      rep(j,26){
          auto it = vs.at(j).lower_bound(l-1);
          if(it != vs.at(j).end()){
              if(*it < r) ++ans;
          }
      }
      **/
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using Graph = vector<vector<int>>;
const int MOD = 1e9 + 7;

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

  int N, Q;
  string S;
  cin >> N >> S >> Q;
  vector<set<int>> pos(26);

  for (int i = 0; i < N; ++i)
    pos[S[i] - 'a'].insert(i);

  for (int i = 0; i < Q; ++i) {
    int n;
    cin >> n;
    if (n == 1) {
      int a;
      char c;
      cin >> a >> c;
      a--;
      pos[S[a] - 'a'].erase(a);
      pos[c - 'a'].insert(a);
      S[a] = c;
    } else {
      int a, b;
      cin >> a >> b;
      a--, b--;
      int cnt = 0;
      for (int j = 0; j < 26; ++j) {
        auto itr = lower_bound(pos[j].begin(), pos[j].end(), a);
        if (itr != pos[j].end())
          cnt += (*itr <= b);
      }
      cout << cnt << '\n';
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pli = pair<ll, int>;
using Graph = vector<vector<int>>;
const int MOD = 1e9 + 7;

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

  int N, Q;
  string S;
  cin >> N >> S >> Q;
  vector<set<int>> pos(26);

  for (int i = 0; i < N; ++i)
    pos[S[i] - 'a'].insert(i);

  for (int i = 0; i < Q; ++i) {
    int n;
    cin >> n;
    if (n == 1) {
      int a;
      char c;
      cin >> a >> c;
      a--;
      pos[S[a] - 'a'].erase(a);
      pos[c - 'a'].insert(a);
      S[a] = c;
    } else {
      int a, b;
      cin >> a >> b;
      a--, b--;
      int cnt = 0;
      for (int j = 0; j < 26; ++j) {
        auto itr = pos[j].lower_bound(a);
        if (itr != pos[j].end())
          cnt += (*itr <= b);
      }
      cout << cnt << '\n';
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define all(x) (x).begin(), (x).end()

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

  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<vector<int>> v(26);
  for (int i = 0; i < N; i++) {
    v[S[i] - 'a'].push_back(i);
  }
  int Q;
  cin >> Q;
  for (int k = 0; k < Q; k++) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      cin >> i;
      i--;
      char c;
      cin >> c;
      if (S[i] == c)
        continue;
      int j = S[i] - 'a';
      v[j].erase(lower_bound(all(v[j]), i));
      j = c - 'a';
      v[j].insert(lower_bound(all(v[j]), i), i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        if (v[i].empty())
          continue;
        int index = lower_bound(all(v[i]), l) - v[i].begin();
        if (index < v[i].size() && v[i][index] <= r)
          ans++;
      }
      cout << ans << "\n";
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define all(x) (x).begin(), (x).end()

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

  int N;
  cin >> N;
  string S;
  cin >> S;
  vector<vector<int>> v(26);
  for (int i = 0; i < N; i++) {
    v[S[i] - 'a'].push_back(i);
  }
  int Q;
  cin >> Q;
  for (int k = 0; k < Q; k++) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      cin >> i;
      i--;
      char c;
      cin >> c;
      if (S[i] == c)
        continue;
      int j = S[i] - 'a';
      v[j].erase(lower_bound(all(v[j]), i));
      S[i] = c;
      j = c - 'a';
      v[j].insert(lower_bound(all(v[j]), i), i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        if (v[i].empty())
          continue;
        int index = lower_bound(all(v[i]), l) - v[i].begin();
        if (index < v[i].size() && v[i][index] <= r)
          ans++;
      }
      cout << ans << "\n";
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long n;
vector<vector<long long>> BIT(30, vector<long long>(n + 10));
void add(long long c, long long i, long long x) {
  while (i <= n) {
    BIT[c][i] += x;
    i += i & -i;
  }
}
long long csum(long long c, long long i) {
  long long count = 0;
  while (i > 0) {
    count += BIT[c][i];
    i -= i & -i;
  }
  return count;
}
long long sum(long long c, long long l, long long r) {
  return csum(c, r) - csum(c, l - 1);
}
int main() {
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    long long c = int(s[i] - 'a');
    add(c, i + 1, 1);
  }
  long long q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b;
    cin >> b;
    if (b == 1) {
      long long I;
      char h;
      cin >> I >> h;
      long long c = int(s[I - 1] - 'a');
      add(c, I, -1);
      c = int(h - 'a');
      add(c, I, 1);
      s[I - 1] = h;
    } else {
      long long l, r;
      cin >> l >> r;
      long long ans = 0;
      for (int i = 0; i < 26; i++) {
        long long co = sum(i, l, r);
        if (co != 0) {
          ans++;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long n;
vector<vector<long long>> BIT(30, vector<long long>(500050));
void add(long long c, long long i, long long x) {
  while (i <= n) {
    BIT[c][i] += x;
    i += i & -i;
  }
}
long long csum(long long c, long long i) {
  long long count = 0;
  while (i > 0) {
    count += BIT[c][i];
    i -= i & -i;
  }
  return count;
}
long long sum(long long c, long long l, long long r) {
  return csum(c, r) - csum(c, l - 1);
}
int main() {
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    long long c = int(s[i] - 'a');
    add(c, i + 1, 1);
  }
  long long q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int b;
    cin >> b;
    if (b == 1) {
      long long I;
      char h;
      cin >> I >> h;
      long long c = int(s[I - 1] - 'a');
      add(c, I, -1);
      c = int(h - 'a');
      add(c, I, 1);
      s[I - 1] = h;
    } else {
      long long l, r;
      cin >> l >> r;
      long long ans = 0;
      for (int i = 0; i < 26; i++) {
        long long co = sum(i, l, r);
        if (co != 0) {
          ans++;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;

int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<set<int>> is(26);
  rep(i, n) is[s[i] - 'a'].insert(i);
  rep(qi, q) {
    int ty;
    cin >> ty;
    if (ty == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      is[s[i] - 'a'].erase(i);
      s[i] = c;
      is[s[i] - 'a'].insert(i);
    }
    if (ty == 2) {
      int l, r;
      cin >> l >> r;
      l--;
      int ans = 0;
      rep(i, 26) {
        auto iter = is[i].lower_bound(l);
        if (iter != is[i].end() && *iter < 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;
using ll = long long;
using P = pair<int, int>;

const int INF = 1001001001;

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

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

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


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

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  set<int> st[30];
  vector<char> s(n + 3);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    st[s[i] - 'a'].insert(i);
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      int x;
      char ch;
      cin >> x >> ch;
      st[s[i] - 'a'].erase(x);
      st[ch - 'a'].insert(x);
      s[x] = ch;
    } else {
      int l, r;
      cin >> l >> r;
      int cnt = 0;
      for (int i = 0; i < 26; i++) {
        if (st[i].lower_bound(l) != st[i].upper_bound(r)) {
          cnt++;
        }
      }
      cout << cnt << '\n';
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  set<int> st[30];
  vector<char> s(n + 3);
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    st[s[i] - 'a'].insert(i);
  }
  int q;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      int x;
      char ch;
      cin >> x >> ch;
      st[s[x] - 'a'].erase(x);
      st[ch - 'a'].insert(x);
      s[x] = ch;
    } else {
      int l, r;
      cin >> l >> r;
      int cnt = 0;
      for (int i = 0; i < 26; i++) {
        if (st[i].lower_bound(l) != st[i].upper_bound(r)) {
          cnt++;
        }
      }
      cout << cnt << '\n';
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
// 目標:セグメント木でACしたい
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int VAL = 'z' - 'a' + 1;
const int MAX_N = 100100;
set<int> st[VAL];
char S[MAX_N];
int N, Q;
vector<int> res;

int main() {
  scanf("%d", &N);
  scanf("%s", S);
  for (int i = 0; i < VAL; ++i) {
    st[i].clear();
    st[i].insert(N);
  }
  for (int i = 0; i < N; ++i) {
    st[S[i] - 'a'].insert(i);
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; ++i) {
    int a, l, r;
    scanf("%d %d", &a, &l);
    --l;
    if (a == 1) {
      char tmp[2];
      scanf("%s", tmp);
      st[S[l] - 'a'].erase(l);
      st[tmp[0] - 'a'].insert(l);
      S[l] = tmp[0];
    } else {
      scanf("%d", &r);
      --r;
      int ans = 0;
      for (int j = 0; j < VAL; ++j) {
        auto itr = st[j].lower_bound(l);
        if (*itr <= r)
          ++ans;
      }
      res.push_back(ans);
    }
  }
  for (auto &p : res)
    printf("%d\n", p);
  return 0;
}

Fixed submission:
// 目標:セグメント木でACしたい
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const int VAL = 'z' - 'a' + 1;
const int MAX_N = 500100;
set<int> st[VAL];
char S[MAX_N];
int N, Q;
vector<int> res;

int main() {
  scanf("%d", &N);
  scanf("%s", S);
  for (int i = 0; i < VAL; ++i) {
    st[i].clear();
    st[i].insert(N);
  }
  for (int i = 0; i < N; ++i) {
    st[S[i] - 'a'].insert(i);
  }
  scanf("%d", &Q);
  for (int i = 0; i < Q; ++i) {
    int a, l, r;
    scanf("%d %d", &a, &l);
    --l;
    if (a == 1) {
      char tmp[2];
      scanf("%s", tmp);
      st[S[l] - 'a'].erase(l);
      st[tmp[0] - 'a'].insert(l);
      S[l] = tmp[0];
    } else {
      scanf("%d", &r);
      --r;
      int ans = 0;
      for (int j = 0; j < VAL; ++j) {
        auto itr = st[j].lower_bound(l);
        if (*itr <= r)
          ++ans;
      }
      res.push_back(ans);
    }
  }
  for (auto &p : res)
    printf("%d\n", p);
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {

  int n, q;
  string s;
  vector<set<int>> A(26);

  cin >> n >> s >> q;

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

  for (int i = 0; i < q; i++) {
    int x;
    cin >> x;
    if (x == 1) {
      int y;
      char c;
      cin >> y >> c;
      y--;
      A[s[y] - 'a'].erase(y);
      A[c - 'a'].insert(y);
      s[y] = c;
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        auto it = lower_bound(A[i].begin(), A[i].end(), l);
        if (it != A[i].end() && *it <= r) {
          ans++;
        }
      }
      printf("%d\n", ans);
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;

int main() {

  int n, q;
  string s;
  vector<set<int>> A(26);

  cin >> n >> s >> q;

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

  for (int i = 0; i < q; i++) {
    int x;
    cin >> x;
    if (x == 1) {
      int y;
      char c;
      cin >> y >> c;
      y--;
      A[s[y] - 'a'].erase(y);
      A[c - 'a'].insert(y);
      s[y] = c;
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        auto it = A[i].lower_bound(l);
        if (it != A[i].end() && *it <= r) {
          ans++;
        }
      }
      printf("%d\n", ans);
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
// https://atcoder.jp/contests/abc157/submissions/10486180

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, Q;
  string S;
  cin >> N >> S >> Q;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    A[i] = 1 << (S[i] - 'a');
  }

  int x, l, r, i;
  char c;
  while (Q--) {
    cin >> x;
    if (x == 1) {
      cin >> i >> c;
      i--;
      A[i] = 1 << (c - 'a');
    } else {
      cin >> l >> r;
      int ans = 0;
      for (int i = l - 1; i < r; i++)
        ans |= A[i];
      cout << __builtin_popcount(ans) << '\n';
    }
  }
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
// https://atcoder.jp/contests/abc157/submissions/10486180

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, Q;
  string S;
  cin >> N >> S >> Q;
  vector<int> A(N);
  for (int i = 0; i < N; ++i) {
    A[i] = 1 << (S[i] - 'a');
  }

  int x, l, r, i;
  char c;
  while (Q--) {
    cin >> x;
    if (x == 1) {
      cin >> i >> c;
      i--;
      A[i] = 1 << (c - 'a');
    } else {
      cin >> l >> r;
      int ans = 0;
      for (int i = l - 1; i < r; i++)
        ans |= A[i];
      cout << __builtin_popcount(ans) << '\n';
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> p;
typedef long long ll;
typedef pair<ll, ll> pll;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const int MAXN = 100000;
const int MAXE = 100000;
const int MAXV = 10000;
const ll INF = 2e9;
const ll MOD = 1e9 + 7;
struct edge {
  int to;
  int cost;
};
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int Q;
  cin >> Q;
  vector<set<int>> st(26);
  for (int i = 0; i < N; ++i) {
    st[S[i] - 'a'].insert(i);
  }
  for (int i = 0; i < 26; ++i)
    st[i].insert(INF);
  while (Q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      st[S[i] - 'a'].erase(i);
      S[i] = c;
      st[S[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        int j = *lower_bound(st[i].begin(), st[i].end(), l);
        if (j <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> pii;
typedef pair<int, pii> p;
typedef long long ll;
typedef pair<ll, ll> pll;
int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
const int MAXN = 100000;
const int MAXE = 100000;
const int MAXV = 10000;
const ll INF = 2e9;
const ll MOD = 1e9 + 7;
struct edge {
  int to;
  int cost;
};
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int Q;
  cin >> Q;
  vector<set<int>> st(26);
  for (int i = 0; i < N; ++i) {
    st[S[i] - 'a'].insert(i);
  }
  for (int i = 0; i < 26; ++i)
    st[i].insert(INF);
  while (Q--) {
    int t;
    cin >> t;
    if (t == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      st[S[i] - 'a'].erase(i);
      S[i] = c;
      st[S[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        int j = *st[i].lower_bound(l);
        if (j <= 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;
int N, Q;
char S[500010];
set<int> s[26];
set<int>::iterator v[500010];
int main() {
  scanf("%d%s%d", &N, S, &Q);
  for (int i = 0; i < N; i++) {
    v[i] = s[S[i] - 'a'].insert(i).first;
  }
  for (; Q; Q--) {
    int a;
    scanf("%d", &a);
    if (a == 1) {
      int i;
      char c;
      scanf("%d %c", &i, &c);
      i--;
      s[S[i] - 'a'].erase(v[i]);
      S[i] = c;
      v[i] = s[S[i] - 'a'].insert(i).first;
    } else {
      int l, r;
      scanf("%d%d", &l, &r);
      l--, r--;
      int x = 0;
      for (int j = 0; j < 26; j++) {
        auto ll = lower_bound(s[j].begin(), s[j].end(), l);
        if (ll != s[j].end() && (*ll) <= r) {
          x++;
        }
      }
      printf("%d\n", x);
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, Q;
char S[500010];
set<int> s[26];
set<int>::iterator v[500010];
int main() {
  scanf("%d%s%d", &N, S, &Q);
  for (int i = 0; i < N; i++) {
    v[i] = s[S[i] - 'a'].insert(i).first;
  }
  for (; Q; Q--) {
    int a;
    scanf("%d", &a);
    if (a == 1) {
      int i;
      char c;
      scanf("%d %c", &i, &c);
      i--;
      s[S[i] - 'a'].erase(v[i]);
      S[i] = c;
      v[i] = s[S[i] - 'a'].insert(i).first;
    } else {
      int l, r;
      scanf("%d%d", &l, &r);
      l--, r--;
      int x = 0;
      for (int j = 0; j < 26; j++) {
        auto ll = s[j].lower_bound(l);
        if (ll != s[j].end() && (*ll) <= r) {
          x++;
        }
      }
      printf("%d\n", x);
    }
  }
}
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;
using ll = long long;

int main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<set<int>> se(26);
  rep(i, n) se[s[i] - 'a'].insert(i);
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    b--;
    if (a == 1) {
      char c;
      cin >> c;
      se[s[b] - 'a'].erase(b);
      s[b] = c;
      se[s[b] - 'a'].insert(b);
    } else {
      int c;
      cin >> c;
      c--;
      int ans = 0;
      rep(j, 26) {
        if (lower_bound(se[j].begin(), se[j].end(), b) == se[j].end())
          continue;
        if (*lower_bound(se[j].begin(), se[j].end(), b) <= c)
          ans++;
      }
      printf("%d\n", ans);
    }
  }
}

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

int main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<set<int>> se(26);
  rep(i, n) se[s[i] - 'a'].insert(i);
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    b--;
    if (a == 1) {
      char c;
      cin >> c;
      se[s[b] - 'a'].erase(b);
      s[b] = c;
      se[s[b] - 'a'].insert(b);
    } else {
      int c;
      cin >> c;
      c--;
      int ans = 0;
      rep(j, 26) {
        auto it = se[j].lower_bound(b);
        if (*it <= c && it != se[j].end())
          ans++;
      }
      printf("%d\n", ans);
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

using ll = long long;

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 int INF = 1001001001;
const long long INFL = LLONG_MAX / 3;
const double eps = (1e-9);

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

  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<set<int>> pos(26, set<int>());
  rep(i, n) {
    char c = s[i] - 'a';
    pos[c].insert(i);
  }

  ll q;
  cin >> q;
  rep(i, q) {
    int cmd;
    cin >> cmd;
    if (cmd == 1) {
      int idx;
      cin >> idx;
      idx--;
      char c;
      cin >> c;
      pos[s[idx] - 'a'].erase(idx);
      pos[c - 'a'].insert(idx);
      s[idx] = c;
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (auto &v : pos) {
        auto itr = lower_bound(ALL(v), l);
        if (itr == v.end())
          continue;
        if (*itr <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
#define FORV(i, v) for (auto i = v.begin(); i != v.end(); i++)
using namespace std;

using ll = long long;

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 int INF = 1001001001;
const long long INFL = LLONG_MAX / 3;
const double eps = (1e-9);

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

  ll n;
  cin >> n;
  string s;
  cin >> s;
  vector<set<int>> pos(26, set<int>());
  rep(i, n) {
    char c = s[i] - 'a';
    pos[c].insert(i);
  }

  ll q;
  cin >> q;
  rep(i, q) {
    int cmd;
    cin >> cmd;
    if (cmd == 1) {
      int idx;
      cin >> idx;
      idx--;
      char c;
      cin >> c;
      pos[s[idx] - 'a'].erase(idx);
      pos[c - 'a'].insert(idx);
      s[idx] = c;
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (auto &v : pos) {
        auto itr = v.lower_bound(l);
        if (itr == v.end())
          continue;
        if (*itr <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define pb push_back
#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per1(i, n) for (ll i = n; i >= 1; i--)
const int N = 1e5;
ll a[N][27];
void update(ll x, ll id, ll val) {
  for (; x < N; x += (x & -x))
    a[x][id] += val;
}
ll query(ll x, ll id) {
  ll ans = 0;
  for (; x; x -= x & (-x))
    ans += a[x][id];
  return ans;
}

void solve() {
  ll i, j, n, m, k, x, y, z;
  cin >> n;
  string s;
  char c;
  cin >> s;
  rep(i, n) { update(i + 1, s[i] - 'a', 1); }
  ll q;
  cin >> q;
  while (q--) {
    cin >> x >> y;
    if (x == 1) {
      cin >> c;
      if (s[y - 1] != c) {
        update(y, s[y - 1] - 'a', -1);
        s[y - 1] = c;
        update(y, s[y - 1] - 'a', 1);
      }

    } else {
      cin >> z;
      ll ans = 0;

      rep(i, 26) {
        if (query(z, i) - query(y - 1, i))
          ans++;
      }
      cout << ans << endl;
    }
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //    cin >> t;
  while (t--) {
    solve();
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define pb push_back
#define fi first
#define se second
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per1(i, n) for (ll i = n; i >= 1; i--)
const int N = 1e6;
ll a[N][27];
void update(ll x, ll id, ll val) {
  for (; x < N; x += (x & -x))
    a[x][id] += val;
}
ll query(ll x, ll id) {
  ll ans = 0;
  for (; x; x -= x & (-x))
    ans += a[x][id];
  return ans;
}

void solve() {
  ll i, j, n, m, k, x, y, z;
  cin >> n;
  string s;
  char c;
  cin >> s;
  rep(i, n) { update(i + 1, s[i] - 'a', 1); }
  ll q;
  cin >> q;
  while (q--) {
    cin >> x >> y;
    if (x == 1) {
      cin >> c;
      if (s[y - 1] != c) {
        update(y, s[y - 1] - 'a', -1);
        s[y - 1] = c;
        update(y, s[y - 1] - 'a', 1);
      }

    } else {
      cin >> z;
      ll ans = 0;

      rep(i, 26) {
        if (query(z, i) - query(y - 1, i))
          ans++;
      }
      cout << ans << endl;
    }
  }
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  //    cin >> t;
  while (t--) {
    solve();
    cout << endl;
  }
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n;
  string s;
  vector<set<ll>> a(26);
  cin >> n >> s;
  ll m;
  cin >> m;
  for (ll i = 0; i < n; i++) {
    ll now = s[i] - 'a';
    a[now].insert(i);
    //		cout <<now<< " "<<i<<endl;
  }
  for (ll i = 0; i < m; i++) {
    ll now;
    cin >> now;
    //		cout << now<<" ";
    if (now == 2) {
      ll cnt = 0;
      ll l, r;
      cin >> l >> r;
      l--;
      r--;
      for (ll j = 0; j < 26; j++) {
        auto itr = lower_bound(a[j].begin(), a[j].end(), l);
        //	cout << *itr<< "itr"<<j<<"j"<<endl;
        if (itr != a[j].end() && *itr <= r) {

          cnt++;
        }
      }
      cout << cnt << endl;
    }
    if (now == 1) {
      ll l;
      char t;
      cin >> l >> t;
      l--;
      ll nex = t - 'a';
      ll be = s[l] - 'a';
      if (nex == be) {
        continue;
      }
      s[l] = t;
      a[be].erase(l);
      a[nex].insert(l);
    }
  }
  // your code goes here
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL << 62
#define inf 1000000007

int main() {
  ll n;
  string s;
  vector<set<ll>> a(26);
  cin >> n >> s;
  ll m;
  cin >> m;
  for (ll i = 0; i < n; i++) {
    ll now = s[i] - 'a';
    a[now].insert(i);
    //		cout <<now<< " "<<i<<endl;
  }
  for (ll i = 0; i < m; i++) {
    ll now;
    cin >> now;
    //		cout << now<<" ";
    if (now == 2) {
      ll cnt = 0;
      ll l, r;
      cin >> l >> r;
      l--;
      r--;
      for (ll j = 0; j < 26; j++) {
        auto itr = a[j].lower_bound(l);
        //	cout << *itr<< "itr"<<j<<"j"<<endl;
        if (itr != a[j].end() && *itr <= r) {

          cnt++;
        }
      }
      cout << cnt << endl;
    }
    if (now == 1) {
      ll l;
      char t;
      cin >> l >> t;
      l--;
      ll nex = t - 'a';
      ll be = s[l] - 'a';
      if (nex == be) {
        continue;
      }
      s[l] = t;
      a[be].erase(l);
      a[nex].insert(l);
    }
  }
  // your code goes here
  return 0;
}
Problem p02763 (C++)

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

using namespace std;

const int NEUTRO = 0;
struct segmentTree {
  const int NEUTRO = 0;
  int n;
  vector<int> st;
  void init(int _) {
    n = 5 * _;
    st.resize(n + 1000);

    for (int i = 0; i < n; ++i)
      st[i] = 0;
  }

  void update(int pos, int value) {
    for (st[pos += n] = value; pos > 1; pos >>= 1)
      st[pos >> 1] = st[pos] + st[pos ^ 1];
  }
  // interval[l,r)
  int query(int l, int r) {
    int ans = NEUTRO;
    r++;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        ans += st[l++];
      if (r & 1)
        ans += st[--r];
    }
    return ans;
  }
};

segmentTree st[30];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  string s;
  cin >> n >> s;

  for (int i = 0; i < 30; ++i)
    st[i].init(n);

  for (int i = 0; i < (int)s.size(); ++i) {
    st[s[i] - 'a'].update(i, 1);
  }

  cin >> q;
  while (q--) {
    int c;
    cin >> c;
    if (c == 1) {
      int p;
      char x;
      cin >> p >> x;
      p--;
      char cur = s[p];
      st[cur - 'a'].update(p, 0);
      s[p] = x;
      st[x - 'a'].update(p, 1);
    } else {
      int ans = 0;
      int l, r;
      cin >> l >> r;
      l--, r--;
      for (int i = 0; i < 30; ++i)
        ans += st[i].query(l, r) != 0;
      cout << ans << "\n";
    }
  }
  return 0;
}


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

using namespace std;

const int NEUTRO = 0;
struct segmentTree {
  const int NEUTRO = 0;
  int n;
  vector<int> st;
  void init(int _) {
    n = _;
    st.resize(5 * n);
  }

  void update(int pos, int value) {
    for (st[pos += n] = value; pos > 1; pos >>= 1)
      st[pos >> 1] = st[pos] + st[pos ^ 1];
  }
  // interval[l,r)
  int query(int l, int r) {
    int ans = NEUTRO;
    r++;
    for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        ans += st[l++];
      if (r & 1)
        ans += st[--r];
    }
    return ans;
  }
};

segmentTree st[30];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  string s;
  cin >> n >> s;

  for (int i = 0; i < 30; ++i)
    st[i].init(n);

  for (int i = 0; i < (int)s.size(); ++i) {
    st[s[i] - 'a'].update(i, 1);
  }

  cin >> q;
  while (q--) {
    int c;
    cin >> c;
    if (c == 1) {
      int p;
      char x;
      cin >> p >> x;
      p--;
      char cur = s[p];
      st[cur - 'a'].update(p, 0);
      s[p] = x;
      st[x - 'a'].update(p, 1);
    } else {
      int ans = 0;
      int l, r;
      cin >> l >> r;
      l--, r--;
      for (int i = 0; i < 30; ++i)
        ans += st[i].query(l, r) != 0;
      cout << ans << "\n";
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()

signed main() {
  int n, q;
  string s;
  cin >> n >> s >> q;

  set<int> pos[26];
  rep(i, s.size()) { pos[s[i] - 'a'].insert(i + 1); }

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

    if (t == 1) {
      int in;
      char c;
      cin >> in >> c;
      pos[s[in - 1] - 'a'].erase(in);
      pos[c - 'a'].insert(in);
      s[in - 1] = c;
    } else {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      rep(j, 26) {
        if (auto it = lower_bound(all(pos[j]), l);
            it != pos[j].end() && *it <= r)
          ++ans;
      }

      cout << ans << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(x) (x).begin(), (x).end()

signed main() {
  int n, q;
  string s;
  cin >> n >> s >> q;

  set<int> pos[26];
  rep(i, s.size()) { pos[s[i] - 'a'].insert(i + 1); }

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

    if (t == 1) {
      int in;
      char c;
      cin >> in >> c;
      pos[s[in - 1] - 'a'].erase(in);
      pos[c - 'a'].insert(in);
      s[in - 1] = c;
    } else {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      rep(j, 26) {
        if (auto it = pos[j].lower_bound(l); it != pos[j].end() && *it <= r)
          ++ans;
      }

      cout << ans << endl;
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

const ll MOD_CONST = 1000000007;
const ll BIG_NUM = 1000000000000000000;
const int BIG_INT = 1000000000;

int 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 a;
    cin >> a;
    if (a == 1) {
      int id;
      char c;

      cin >> id >> c;
      id--;
      int num = s[id] - 'a';
      s[id] = c;
      c -= 'a';

      v[num].erase(id);
      v[c].insert(id);

    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int cnt = 0;
      for (int j = 0; j < 26; j++) {

        auto it = lower_bound(v[j].begin(), v[j].end(), l);

        if (it != v[j].end() && *it <= r) {

          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }
}


Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

const ll MOD_CONST = 1000000007;
const ll BIG_NUM = 1000000000000000000;
const int BIG_INT = 1000000000;

int 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 a;
    cin >> a;
    if (a == 1) {
      int id;
      char c;

      cin >> id >> c;
      id--;
      int num = s[id] - 'a';
      s[id] = c;
      c -= 'a';

      v[num].erase(id);
      v[c].insert(id);

    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int cnt = 0;
      for (int j = 0; j < 26; j++) {

        auto it = v[j].lower_bound(l);

        if (it != v[j].end() && *it <= r) {

          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }
}
Problem p02763 (C++)

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

set<int> tp[26];

void rem(int a, int pos) {
  assert(tp[a].find(pos) != tp[a].end());
  tp[a].erase(pos);
}

void add(int a, int pos) { tp[a].insert(pos); }

int cdis(int l, int r) {
  int c = 0;
  for (int i = 0; i < 26; i++) {
    set<int>::iterator e = tp[i].lower_bound(l);
    if (e != tp[i].end() && *e <= r)
      c++;
  }
  return c;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, q;

  string s;
  cin >> n >> s >> q;

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

  for (int T = 0; T < q; T++) {

    int t;
    cin >> t;

    if (t == 1) {
      int i;
      string t;
      cin >> i >> t;
      i--;
      rem(s[i] - 'a', i);
      add(t[0] - 'a', i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      cout << cdis(l, r) << endl;
    }
  }
}


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

set<int> tp[26];

void rem(int a, int pos) {
  assert(tp[a].find(pos) != tp[a].end());
  tp[a].erase(pos);
}

void add(int a, int pos) { tp[a].insert(pos); }

int cdis(int l, int r) {
  int c = 0;
  for (int i = 0; i < 26; i++) {
    set<int>::iterator e = tp[i].lower_bound(l);
    if (e != tp[i].end() && *e <= r)
      c++;
  }
  return c;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n, q;

  string s;
  cin >> n >> s >> q;

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

  for (int T = 0; T < q; T++) {

    int t;
    cin >> t;

    if (t == 1) {
      int i;
      string t;
      cin >> i >> t;
      i--;
      rem(s[i] - 'a', i);
      add(t[0] - 'a', i);
      s[i] = t[0];
      // cout << s << endl;
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      cout << cdis(l, r) << endl;
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> ii;

const ll siz = 1e5 + 10;
const ll SIZ = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll maxx = 2e9;
const ll MAXX = 1e18;

int n, q, t;
string s;
int tree[26][siz];

void updatePos(int letter, int pos, int val) {
  for (int i = pos; i < siz; i += i & -i)
    tree[letter][i] += val;
}

int getSum(int letter, int pos) {
  int sum = 0;
  for (int i = pos; i > 0; i -= i & -i)
    sum += tree[letter][i];
  return sum;
}

int getSumRange(int letter, int start, int finish) {
  return getSum(letter, finish) - getSum(letter, start - 1);
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //    freopen ("IN_4.INP", "r", stdin);
  //    freopen ("OUT_4.OUT", "w", stdout);
  cin >> n >> s >> q;
  for (int i = 1; i <= n; i++)
    updatePos(s[i - 1] - 'a', i, 1);
  while (q--) {
    cin >> t;
    if (t == 1) {
      int p;
      char c;
      cin >> p >> c;
      updatePos(s[p - 1] - 'a', p, -1);
      s[p - 1] = c;
      updatePos(c - 'a', p, 1);
    } else {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; i++)
        ans += (getSumRange(i, l, r) > 0);
      cout << ans << "\n";
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define ll long long
#define fi first
#define se second
using namespace std;
typedef pair<int, int> ii;

const ll siz = 5e5 + 10;
const ll SIZ = 1e6 + 10;
const ll mod = 1e9 + 7;
const ll maxx = 2e9;
const ll MAXX = 1e18;

int n, q, t;
string s;
int tree[26][siz];

void updatePos(int letter, int pos, int val) {
  for (int i = pos; i < siz; i += i & -i)
    tree[letter][i] += val;
}

int getSum(int letter, int pos) {
  int sum = 0;
  for (int i = pos; i > 0; i -= i & -i)
    sum += tree[letter][i];
  return sum;
}

int getSumRange(int letter, int start, int finish) {
  return getSum(letter, finish) - getSum(letter, start - 1);
}

int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  //    freopen ("IN_4.INP", "r", stdin);
  //    freopen ("OUT_4.OUT", "w", stdout);
  cin >> n >> s >> q;
  for (int i = 1; i <= n; i++)
    updatePos(s[i - 1] - 'a', i, 1);
  while (q--) {
    cin >> t;
    if (t == 1) {
      int p;
      char c;
      cin >> p >> c;
      updatePos(s[p - 1] - 'a', p, -1);
      s[p - 1] = c;
      updatePos(c - 'a', p, 1);
    } else {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; i++)
        ans += (getSumRange(i, l, r) > 0);
      cout << ans << "\n";
    }
  }
  return 0;
}
Problem p02763 (C++)

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

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

  int N, Q;
  string S;
  cin >> N >> S >> Q;

  vector<set<int>> vsi(26);
  for (int i = 0; i < S.size(); ++i) {
    vsi[S[i] - 'a'].insert(i);
  }
  for (int i = 0; i < Q; ++i) {
    int t;
    cin >> t;
    if (t == 1) {
      int p;
      char c;
      cin >> p >> c;
      p--;
      char sc = S[p];
      vsi[sc - 'a'].erase(p);
      S[p] = c;
      vsi[c - 'a'].insert(p);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (char c = 'a'; c <= 'z'; ++c) {
        auto it = lower_bound(vsi[c - 'a'].begin(), vsi[c - 'a'].end(), l);
        if (it != vsi[c - 'a'].end() && *it <= r)
          ++ans;
      }
      cout << ans << endl;
    }
  }
}


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

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

  int N, Q;
  string S;
  cin >> N >> S >> Q;

  vector<set<int>> vsi(26);
  for (int i = 0; i < S.size(); ++i) {
    vsi[S[i] - 'a'].insert(i);
  }
  for (int i = 0; i < Q; ++i) {
    int t;
    cin >> t;
    if (t == 1) {
      int p;
      char c;
      cin >> p >> c;
      p--;
      char sc = S[p];
      vsi[sc - 'a'].erase(p);
      S[p] = c;
      vsi[c - 'a'].insert(p);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (char c = 0; c < 26; ++c) {
        auto it = vsi[c].lower_bound(l);
        if (it != vsi[c].end() && *it <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }
}
Problem p02763 (C++)

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

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n, Q;
  string s;
  cin >> n >> s >> Q;
  vector<set<int>> st(26);
  for (int i = 0; i < n; ++i) {
    st.at(s.at(i) - 'a').insert(i);
  }
  for (int i = 0; i < Q; ++i) {
    int q;
    cin >> q;
    if (q == 1) {
      int j;
      char c;
      cin >> j >> c;
      --j;
      st.at(s.at(j) - 'a').erase(j);
      s.at(j) = c;
      st.at(s.at(j) - 'a').insert(j);
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      int ans = 0;
      for (int j = 0; j < 26; ++j) {
        if (!st.at(j).size())
          continue;
        auto itr = lower_bound(st.at(j).begin(), st.at(j).end(), l);
        if (itr != st.at(j).end() && *itr < r)
          ++ans;
      }
      cout << ans << "\n"s;
    }
  }
}


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

int main() {
  cin.tie(0), ios::sync_with_stdio(false);
  int n, Q;
  string s;
  cin >> n >> s >> Q;
  vector<set<int>> st(26);
  for (int i = 0; i < n; ++i) {
    st.at(s.at(i) - 'a').insert(i);
  }
  for (int i = 0; i < Q; ++i) {
    int q;
    cin >> q;
    if (q == 1) {
      int j;
      char c;
      cin >> j >> c;
      --j;
      st.at(s.at(j) - 'a').erase(j);
      s.at(j) = c;
      st.at(s.at(j) - 'a').insert(j);
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      int ans = 0;
      for (int j = 0; j < 26; ++j) {
        if (!st.at(j).size())
          continue;
        auto itr = st.at(j).lower_bound(l);
        if (itr != st.at(j).end() && *itr < r)
          ++ans;
      }
      cout << ans << "\n"s;
    }
  }
}