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

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
ll INF = 1ll << 60;

int main() {
  int n, Q;
  string s;
  vector<set<int>> st(26);
  cin >> n;
  getchar();
  rep(i, 0, n) {
    char c = getchar();
    s.push_back(c);
    st[c - 'a'].insert(i);
  }
  getchar();

  cin >> Q;
  rep(tmp, 0, Q) {
    int q;
    cin >> q;
    q--;
    if (q) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(i, 0, 26) {
        auto itr = lower_bound(st[i].begin(), st[i].end(), l);
        if (itr != st[i].end() && *itr <= r)
          ans++;
      }
      cout << ans << "\n";
    } else {
      int i;
      char c;
      cin >> i >> c;
      i--;
      if (s[i] == c)
        continue;
      st[s[i] - 'a'].erase(i);
      s[i] = c;
      st[c - 'a'].insert(i);
    }
  }

  return 0;
}

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

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vb = vector<bool>;
using vvb = vector<vb>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, s, n) for (int i = (int)(s); i < (int)(n); i++)
ll INF = 1ll << 60;

int main() {
  int n, Q;
  string s;
  vector<set<int>> st(26);
  cin >> n;
  getchar();
  rep(i, 0, n) {
    char c = getchar();
    s.push_back(c);
    st[c - 'a'].insert(i);
  }
  getchar();

  cin >> Q;
  rep(tmp, 0, Q) {
    int q;
    cin >> q;
    q--;
    if (q) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(i, 0, 26) {
        auto itr = st[i].lower_bound(l);
        if (itr != st[i].end() && *itr <= r)
          ans++;
      }
      cout << ans << "\n";
    } else {
      int i;
      char c;
      cin >> i >> c;
      i--;
      if (s[i] == c)
        continue;
      st[s[i] - 'a'].erase(i);
      s[i] = c;
      st[c - 'a'].insert(i);
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, m;
int f[26][500001];
char a[500001];
int lowbit(int x) { return x & (-x); }
void build() {
  for (int i = 0; i < 26; i++)
    for (int j = 1; j <= n; j++)
      if (lowbit(j) <= n)
        f[i][j + lowbit(j)] += f[i][j];
}
void change(int x, int y, int z) {
  while (x <= n) {
    f[y][x] += z;
    x += lowbit(x);
  }
}
int find(int x, int y) {
  int tot = 0;
  while (y) {
    tot += f[x][y];
    y -= lowbit(y);
  }
  return tot;
}
int ask(int l, int r) {
  int ans = 0;
  for (int i = 0; i < 26; i++)
    if (find(i, r) - find(i, l - 1) > 0)
      ans++;
  return ans;
}
void A() {
  int x;
  char y;
  scanf("%d", &x);
  y = getchar();
  while (y > 'z' || y < 'a')
    y = getchar();
  if (a[x] != y) {
    change(x, a[x] - 'a', -1);
    a[x] = y;
    change(x, a[x] - 'a', 1);
  }
}
void B() {
  int l, r;
  scanf("%d%d", &l, &r);
  printf("%d\n", ask(l, r));
}
signed main() {
  int x;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    a[i] = getchar();
    while (a[i] > 'z' || a[i] < 'a')
      a[i] = getchar();
    f[a[i] - 'a'][i] = 1;
  }
  build();
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &x);
    if (x == 1)
      A();
    else
      B();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m;
int f[26][5000001];
char a[5000001];
int lowbit(int x) { return x & (-x); }
void build() {
  for (int i = 0; i < 26; i++)
    for (int j = 1; j <= n; j++)
      if (lowbit(j) <= n)
        f[i][j + lowbit(j)] += f[i][j];
}
void change(int x, int y, int z) {
  while (x <= n) {
    f[y][x] += z;
    x += lowbit(x);
  }
}
int find(int x, int y) {
  int tot = 0;
  while (y) {
    tot += f[x][y];
    y -= lowbit(y);
  }
  return tot;
}
int ask(int l, int r) {
  int ans = 0;
  for (int i = 0; i < 26; i++)
    if (find(i, r) - find(i, l - 1) > 0)
      ans++;
  return ans;
}
void A() {
  int x;
  char y;
  scanf("%d", &x);
  y = getchar();
  while (y > 'z' || y < 'a')
    y = getchar();
  if (a[x] != y) {
    change(x, a[x] - 'a', -1);
    a[x] = y;
    change(x, a[x] - 'a', 1);
  }
}
void B() {
  int l, r;
  scanf("%d%d", &l, &r);
  printf("%d\n", ask(l, r));
}
signed main() {
  int x;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    a[i] = getchar();
    while (a[i] > 'z' || a[i] < 'a')
      a[i] = getchar();
    f[a[i] - 'a'][i] = 1;
  }
  build();
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) {
    scanf("%d", &x);
    if (x == 1)
      A();
    else
      B();
  }
  return 0;
}
Problem p02763 (C++)

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

using namespace std;

#define pb push_back
#define NL endl
#define pi 2.0 * acos(0.0)
#define mod 1e9 + 7
#define FOR(i, j, n) for (ll i = j; i < n; i++)
#define RFOR(i, j, n) for (ll i = j; i >= n; i--)
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;

set<ll> cset[30];

int main() {
  fastIO;
  ll n, q, a, b, c, pos, ans = 0;
  string s;
  char d, curr;
  cin >> n >> s >> q;
  FOR(i, 0, n) { cset[s[i] - 'a'].insert(i + 1); }
  FOR(i, 0, 26) { cset[i].insert(1e18); }
  while (q--) {
    cin >> a;
    if (a == 1) {
      cin >> b >> d;
      curr = s[b - 1];
      cset[curr - 'a'].erase(b);
      cset[d - 'a'].insert(b);
      s[b - 1] = d;
    } else {
      cin >> b >> c;
      ans = 0;
      FOR(i, 0, 26) {
        if (cset[i].size()) {
          auto lower = lower_bound(cset[i].begin(), cset[i].end(), b);
          if (*lower == 1e18) {
          } else {
            ll val = *lower;
            if (val >= b && val <= c)
              ans++;
          }
        }
      }
      cout << ans << endl;
    }
  }
}


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

using namespace std;

#define pb push_back
#define NL endl
#define pi 2.0 * acos(0.0)
#define mod 1e9 + 7
#define FOR(i, j, n) for (ll i = j; i < n; i++)
#define RFOR(i, j, n) for (ll i = j; i >= n; i--)
#define fastIO                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);

typedef long long int ll;
typedef unsigned long long ull;
typedef long double ld;

set<ll> cset[30];

int main() {
  fastIO;
  ll n, q, a, b, c, pos, ans = 0;
  string s;
  char d, curr;
  cin >> n >> s >> q;
  FOR(i, 0, n) { cset[s[i] - 'a'].insert(i + 1); }
  FOR(i, 0, 26) { cset[i].insert(1e18); }
  while (q--) {
    cin >> a;
    if (a == 1) {
      cin >> b >> d;
      curr = s[b - 1];
      cset[curr - 'a'].erase(b);
      cset[d - 'a'].insert(b);
      s[b - 1] = d;
    } else {
      cin >> b >> c;
      ans = 0;
      FOR(i, 0, 26) {
        if (cset[i].size()) {
          auto lower = cset[i].lower_bound(b);
          if (*lower == 1e18) {
          } else {
            ll val = *lower;
            if (val >= b && val <= c)
              ans++;
          }
        }
      }
      cout << ans << endl;
    }
  }
}
Problem p02763 (C++)

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

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

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

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

Buggy submission (status: Runtime Error):
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

const int MAX = 1 << 17;
int N, dat[2 * MAX - 1];

// 初期化
void init(int n) {
  N = 1;
  while (N < n)
    N *= 2;
  for (int i = 0; i < 2 * N - 1; i++)
    dat[i] = 0;
}

// k番目(0-index)をaに変更
void update(int k, int a) {
  k += N - 1;
  dat[k] = 1 << a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = dat[k * 2 + 1] | dat[k * 2 + 2];
  }
}

//[a,b)なので0-indexならa--,b--;
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return vl | vr;
}

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

  rep(i, n) { update(i, s[i] - 'a'); }

  int c, a, b;
  char d;
  rep(i, q) {
    cin >> c;
    if (c == 1) {
      cin >> a >> d;
      a--;
      update(a, d - 'a');
    } else {
      cin >> a >> b;
      a--, b--;
      cout << __builtin_popcount(query(a, b + 1, 0, 0, N)) << endl;
    }
  }

  return 0;
}

Fixed submission:
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

const int MAX = 1 << 20;
int N, dat[2 * MAX - 1];

// 初期化
void init(int n) {
  N = 1;
  while (N < n)
    N *= 2;
  for (int i = 0; i < 2 * N - 1; i++)
    dat[i] = 0;
}

// k番目(0-index)をaに変更
void update(int k, int a) {
  k += N - 1;
  dat[k] = 1 << a;
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k] = dat[k * 2 + 1] | dat[k * 2 + 2];
  }
}

//[a,b)なので0-indexならa--,b--;
int query(int a, int b, int k, int l, int r) {
  if (r <= a || b <= l)
    return 0;
  if (a <= l && r <= b)
    return dat[k];
  int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
  int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
  return vl | vr;
}

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

  rep(i, n) { update(i, s[i] - 'a'); }

  int c, a, b;
  char d;
  rep(i, q) {
    cin >> c;
    if (c == 1) {
      cin >> a >> d;
      a--;
      update(a, d - 'a');
    } else {
      cin >> a >> b;
      a--, b--;
      cout << __builtin_popcount(query(a, b + 1, 0, 0, N)) << endl;
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 6;
int tree[26][N];
int n;
void add(int type, int id, int val) {
  while (id <= n) {
    tree[type][id] += val;
    id += (id & -id);
  }
}
int qu(int type, int till) {
  int ans = 0;
  while (till >= 1) {
    ans += tree[type][till];
    till -= (till & -till);
  }
  return ans;
}
int res(int l, int r, int type) { return qu(type, r) - qu(type, l - 1); }
int32_t main() {
  cin >> n;
  string s;
  cin >> s;
  for (int i = 1; i <= n; i++) {
    add(s[i - 1] - 'a', i, 1);
  }

  int q, ch, pos, l, r;
  char c;
  cin >> q;
  while (q--) {
    cin >> ch;
    if (ch == 1) {
      cin >> pos >> c;
      add(c - 'a', pos, 1);
      add(s[pos - 1] - 'a', pos, -1);
      s[pos - 1] = c;
    } else {
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        if (res(l, r, i) > 0)
          ans++;
      }
      cout << ans << "\n";
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 6;
int tree[26][N];
int n;
void add(int type, int id, int val) {
  while (id <= n) {
    tree[type][id] += val;
    id += (id & -id);
  }
}
int qu(int type, int till) {
  int ans = 0;
  while (till >= 1) {
    ans += tree[type][till];
    till -= (till & -till);
  }
  return ans;
}
int res(int l, int r, int type) { return qu(type, r) - qu(type, l - 1); }
int32_t main() {
  cin >> n;
  string s;
  cin >> s;
  for (int i = 1; i <= n; i++) {
    add(s[i - 1] - 'a', i, 1);
  }

  int q, ch, pos, l, r;
  char c;
  cin >> q;
  while (q--) {
    cin >> ch;
    if (ch == 1) {
      cin >> pos >> c;
      add(c - 'a', pos, 1);
      add(s[pos - 1] - 'a', pos, -1);
      s[pos - 1] = c;
    } else {
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        if (res(l, r, i) > 0)
          ans++;
      }
      cout << ans << "\n";
    }
  }
}
Problem p02763 (C++)

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

int main() {
  int n;
  string s;
  cin >> n >> s;
  // info[i] := 文字 ('a' + i) がどの位置にあるか
  // これを各文字について、二分木で持っておく
  vector<set<int>> info(26);
  for (int i = 0; i < n; ++i) {
    int id = s[i] - 'a';
    info[id].insert(i);
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int query;
    cin >> query;
    if (query == 1) {
      int pos;
      char c;
      cin >> pos >> c;
      --pos;
      if (s[pos] == c)
        continue;

      info[s[pos]].erase(pos);
      int id = c - 'a';
      info[id].insert(pos);
      s[pos] = c;
    }

    else { //  query == 2
      int left, right;
      cin >> left >> right;
      --left;

      int res = 0;
      for (int j = 0; j < 26; ++j) {
        auto it = info[j].lower_bound(left);
        if (it == info[j].end())
          continue; // 虚無を指す場合は continue

        int pos = *it;
        if (pos < right)
          ++res;
      }
      cout << res << "\n";
    }
  }
}

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

int main() {
  int n;
  string s;
  cin >> n >> s;
  // info[i] := 文字 ('a' + i) がどの位置にあるか
  // これを各文字について、二分木で持っておく
  vector<set<int>> info(26);
  for (int i = 0; i < n; ++i) {
    int id = s[i] - 'a';
    info[id].insert(i);
  }

  int q;
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int query;
    cin >> query;
    if (query == 1) {
      int pos;
      char c;
      cin >> pos >> c;
      --pos;
      if (s[pos] == c)
        continue;

      info[(s[pos] - 'a')].erase(pos);
      int id = c - 'a';
      info[id].insert(pos);
      s[pos] = c;
    }

    else { //  query == 2
      int left, right;
      cin >> left >> right;
      --left;

      int res = 0;
      for (int j = 0; j < 26; ++j) {
        auto it = info[j].lower_bound(left);
        if (it == info[j].end())
          continue; // 虚無を指す場合は continue

        int pos = *it;
        if (pos < right)
          ++res;
      }
      cout << res << "\n";
    }
  }
}
Problem p02763 (C++)

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

ll INF = 1e+18;
using G = vector<map<int, int>>;

int main() {
  int N;
  cin >> N;
  vector<char> ori(N);
  rep(i, 0, N) cin >> ori[i];
  map<char, set<int>> m;
  rep(i, 0, N) { m[ori[i]].insert(i + 1); }

  int Q;
  cin >> Q;
  rep(i, 0, Q) {
    int type;
    cin >> type;
    if (type == 1) {
      int index;
      char c;
      cin >> index >> c;
      m[c].insert(index);
      char rc = ori[index];
      m[rc].erase(index);
      ori[index] = c;
    } else {
      ll l, r;
      cin >> l >> r;
      if (l > r) {
        ll tmp = l;
        l = r;
        r = tmp;
      }
      ll ans = 0;
      rep(i, 0, 26) {
        if (m[(char)(97 + i)].size() == 0)
          continue;
        ll posi = *m[(char)(97 + i)].lower_bound(l);
        if (posi >= l && posi <= r)
          ++ans;
      }
      cout << ans << endl;
    }
  }
}

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

ll INF = 1e+18;
using G = vector<map<int, int>>;

int main() {
  int N;
  cin >> N;
  vector<char> ori(N);
  rep(i, 0, N) cin >> ori[i];
  map<char, set<int>> m;
  rep(i, 0, N) { m[ori[i]].insert(i + 1); }

  int Q;
  cin >> Q;
  rep(i, 0, Q) {
    int type;
    cin >> type;
    if (type == 1) {
      int index;
      char c;
      cin >> index >> c;
      m[c].insert(index);
      char rc = ori[index - 1];
      if (rc != c)
        m[rc].erase(index);
      ori[index - 1] = c;
    } else {
      ll l, r;
      cin >> l >> r;
      if (l > r) {
        ll tmp = l;
        l = r;
        r = tmp;
      }
      ll ans = 0;
      rep(i, 0, 26) {
        if (m[(char)(97 + i)].size() == 0)
          continue;
        ll posi = *m[(char)(97 + i)].lower_bound(l);
        if (posi >= l && posi <= r)
          ++ans;
      }
      cout << ans << endl;
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef long long int lli;
typedef pair<lli, lli> Pii;
const int INF = 10 ^ 9 + 11555;
int main() {
  // vector
  // vc.push_buck(x)
  // sort(vc.begin(),vc.end())
  int n, q;
  string s;
  cin >> n;
  cin >> s;
  cin >> q;
  vector<set<int>> is(26);
  rep(i, n) is[s[i] - 'a'].insert(i);
  int temp[3];
  char temp2;
  int ans;
  rep(i, q) {
    cin >> temp[0];
    if (temp[0] == 2) {
      cin >> temp[1] >> temp[2];
      ans = 0;
      rep(j, 26) {
        auto it = is[j].lower_bound(temp[1] - 1);
        if (it != is[j].end() && *it <= temp[2] - 1)
          ans++;
      }
      cout << ans << endl;
    } else {
      cin >> temp[1] >> temp2;
      temp[1]--;
      is[s[temp[1]] - 'a'].erase(temp[1]);
      s[i] = temp2;
      is[temp2 - 'a'].insert(temp[1]);
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <queue>
#include <vector>
#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
typedef long long int lli;
typedef pair<lli, lli> Pii;
const int INF = 10 ^ 9 + 11555;
int main() {
  // vector
  // vc.push_buck(x)
  // sort(vc.begin(),vc.end())
  int n, q;
  string s;
  cin >> n;
  cin >> s;
  cin >> q;
  vector<set<int>> is(26);
  rep(i, n) is[s[i] - 'a'].insert(i);
  int temp[3];
  char temp2;
  int ans;
  rep(i, q) {
    cin >> temp[0];
    if (temp[0] == 2) {
      cin >> temp[1] >> temp[2];
      ans = 0;
      rep(j, 26) {
        auto it = is[j].lower_bound(temp[1] - 1);
        if (it != is[j].end() && *it <= temp[2] - 1)
          ans++;
      }
      cout << ans << endl;
    } else {
      cin >> temp[1] >> temp2;
      temp[1]--;
      is[s[temp[1]] - 'a'].erase(temp[1]);
      s[temp[1]] = temp2;
      is[temp2 - 'a'].insert(temp[1]);
    }
  }
  return 0;
}
Problem p02763 (C++)

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

using namespace std;

struct BIT {
  vector<int> node;
  int sz;
  BIT(int sz) : sz(sz) { node.resize(sz, 0); }
  int sum(int i) {
    int res = 0;
    for (i--; i >= 0; i = (i & (i + 1)) - 1)
      res += node[i];
    return res;
  }
  void add(int i, int x) {
    for (; i < sz; i |= i + 1)
      node[i] += x;
  }
  int sum(int l, int r) { return sum(r) - sum(l); }
};

signed main() {
  int N;
  string S;
  cin >> N >> S;
  vector<BIT> bits;
  for (int i = 0; i < 26; i++)
    bits.push_back(BIT(N));
  for (int i = 0; i < N; i++) {
    bits[S[i] - 'a'].add(i, 1);
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    if (q == 1) {
      int index;
      char c;
      cin >> index >> c;
      index--;
      bits[S[index] - 'a'].add(index, -1);
      bits[c - 'a'].add(index, 1);
      S[index] = c;
    }
    if (q == 2) {
      int l, r;
      cin >> l >> r;
      l--, r--;
      int ans = 0;
      for (BIT it : bits) {
        ans += min(it.sum(l, r + 1), 1);
      }
      cout << ans << endl;
    }
  }
  return 0;
}


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

using namespace std;

struct BIT {
  vector<int> node;
  int sz;
  BIT(int sz) : sz(sz) { node.resize(sz, 0); }
  int sum(int i) {
    int res = 0;
    for (i--; i >= 0; i = (i & (i + 1)) - 1)
      res += node[i];
    return res;
  }
  void add(int i, int x) {
    for (; i < sz; i |= i + 1)
      node[i] += x;
  }
  int sum(int l, int r) { return sum(r) - sum(l); }
};

signed main() {
  int N;
  string S;
  cin >> N >> S;
  vector<BIT> bits;
  for (int i = 0; i < 26; i++)
    bits.push_back(BIT(N));
  for (int i = 0; i < N; i++) {
    bits[S[i] - 'a'].add(i, 1);
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    if (q == 1) {
      int index;
      char c;
      cin >> index >> c;
      index--;
      bits[S[index] - 'a'].add(index, -1);
      bits[c - 'a'].add(index, 1);
      S[index] = c;
    }
    if (q == 2) {
      int l, r;
      cin >> l >> r;
      l--, r--;
      int ans = 0;
      for (auto &it : bits) {
        ans += min(it.sum(l, r + 1), 1);
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

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

ll N, Q;
vector<char> vch;
set<ll> S[26];

signed main() {
  cin >> N;
  vch.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> vch[i];
    S[vch[i] - 'a'].insert(i);
  }
  /*
  for(int i=0;i<26;i++) {
      for(set<ll>::iterator it=S[i].begin();it!=S[i].end();it++) {
          cout << i << " " << *it << endl;
      }
  }
  */
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    if (q == 1) {
      int id;
      char c;
      cin >> id >> c;
      id--;
      S[vch[id] - 'a'].erase(id);
      vch[id] = c;
      S[vch[id] - 'a'].insert(id);
    } else {
      int l, r, ans = 0;
      cin >> l >> r;
      l--;
      r--;
      for (int i = 0; i < 26; i++) {
        if (S[i].empty() || S[i].lower_bound(l) == S[i].end())
          continue;
        for (set<ll>::iterator it = S[i].begin(); it != S[i].end(); it++) {
          // cout << i << " " << *it << " " << *S[i].lower_bound(l) << endl;
        }
        if (*S[i].lower_bound(l) <= r) {
          ans++;
          // cout << i << " " << l << " " << *S[i].lower_bound(l) << " "  << r
          // << endl;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
typedef long long ll;
using namespace std;

ll N, Q;
vector<char> vch;
set<ll> S[26];

signed main() {
  cin >> N;
  vch.resize(N);
  for (int i = 0; i < N; i++) {
    cin >> vch[i];
    S[vch[i] - 'a'].insert(i);
  }
  /*
  for(int i=0;i<26;i++) {
      for(set<ll>::iterator it=S[i].begin();it!=S[i].end();it++) {
          cout << i << " " << *it << endl;
      }
  }
  */
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int q;
    cin >> q;
    if (q == 1) {
      int id;
      char c;
      cin >> id >> c;
      id--;
      S[vch[id] - 'a'].erase(id);
      vch[id] = c;
      S[vch[id] - 'a'].insert(id);
    } else {
      int l, r, ans = 0;
      cin >> l >> r;
      l--;
      r--;
      for (int i = 0; i < 26; i++) {
        if (S[i].empty() || S[i].lower_bound(l) == S[i].end())
          continue;
        // for(set<ll>::iterator it=S[i].begin();it!=S[i].end();it++) {
        // cout << i << " " << *it << " " << *S[i].lower_bound(l) << endl;
        //}
        if (*S[i].lower_bound(l) <= r) {
          ans++;
          // cout << i << " " << l << " " << *S[i].lower_bound(l) << " "  << r
          // << endl;
        }
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

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

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const int inf = INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;
ll roundd(ll x, ll n) {
  if (x > n) {
    return x % n;
  } else if (x < 0) {
    return x % n + n;
  } else
    return x;
}
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  }

  else {
    return (gcd(b, a % b));
  }
}
int main() {
  ll n, q;
  cin >> n;
  string s;
  cin >> s;
  cin >> q;
  ll t = s.size();
  vector<set<ll>> vs(26);

  rep(i, 0, t) {
    ll tmp = s[i] - 'a';
    vs[tmp].insert(i);
  }

  rep(i, 0, q) {
    ll a;
    cin >> a;
    if (a == 1) {
      ll m;
      char c;
      cin >> m >> c;
      vs[s[m - 1] - 'a'].erase(m - 1);
      vs[c - 'a'].insert(m - 1);
      s[m - 1] = c;
    } else {
      ll l, r;
      cin >> l >> r;
      l--;
      r--;
      ll tmp = 0;
      rep(j, 0, 26) {
        auto it = lower_bound(vs[j].begin(), vs[j].end(), l);
        if (*it <= r && it != vs[j].end()) {
          tmp++;
        }
      }
      cout << tmp << endl;
    }
  }
}


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

using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef pair<ll, ll> P;
typedef vector<P> vp;
#define rep(i, a, b) for (ll i = (a); i < (b); ++i)
#define drep(i, a, b) for (ll i = (a); i >= (b); --i)
#define SIZE(a) ll((a).size())
#define out(a) cout << (a) << endl;
const int inf = INT_MAX;
const int MAX = 510000;
const ll MOD = 1000000007;
ll roundd(ll x, ll n) {
  if (x > n) {
    return x % n;
  } else if (x < 0) {
    return x % n + n;
  } else
    return x;
}
ll gcd(ll a, ll b) {
  if (a % b == 0) {
    return (b);
  }

  else {
    return (gcd(b, a % b));
  }
}
int main() {
  ll n, q;
  cin >> n;
  string s;
  cin >> s;
  cin >> q;
  ll t = s.size();
  vector<set<ll>> vs(26);

  rep(i, 0, t) {
    ll tmp = s[i] - 'a';
    vs[tmp].insert(i);
  }

  rep(i, 0, q) {
    ll a;
    cin >> a;
    if (a == 1) {
      ll m;
      char c;
      cin >> m >> c;
      vs[s[m - 1] - 'a'].erase(m - 1);
      vs[c - 'a'].insert(m - 1);
      s[m - 1] = c;
    } else {
      ll l, r;
      cin >> l >> r;
      l--;
      r--;
      ll tmp = 0;
      rep(j, 0, 26) {
        auto it = vs[j].lower_bound(l);
        if (it != vs[j].end() && *it <= r) {
          tmp++;
        }
      }
      cout << tmp << endl;
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int main() {
  int n, q;
  string s;
  cin >> n;
  cin >> s;
  cin >> q;
  set<int> data[26];
  rep(i, n) {
    int v = s[i] - 'a';
    data[v].insert(i);
  }

  rep(i, q) {
    int id;
    cin >> id;
    if (id == 1) {
      int iq;
      char cq;
      cin >> iq >> cq;
      iq--;
      int v = s[iq] - 'a';
      data[v].erase(iq);
      s[iq] = cq;
      v = cq - 'a';
      data[v].insert(iq);
    } else {
      int lq, rq;
      cin >> lq >> rq;
      lq--;
      rq--;
      int cnt = 0;
      rep(j, 26) {
        auto it = lower_bound(data[j].begin(), data[j].end(), lq);
        if (it != data[j].end() && *it <= rq) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int main() {
  int n, q;
  string s;
  cin >> n;
  cin >> s;
  cin >> q;
  set<int> data[26];
  rep(i, n) {
    int v = s[i] - 'a';
    data[v].insert(i);
  }

  rep(i, q) {
    int id;
    cin >> id;
    if (id == 1) {
      int iq;
      char cq;
      cin >> iq >> cq;
      iq--;
      int v = s[iq] - 'a';
      data[v].erase(iq);
      s[iq] = cq;
      v = cq - 'a';
      data[v].insert(iq);
    } else {
      int lq, rq;
      cin >> lq >> rq;
      lq--;
      rq--;
      int cnt = 0;
      rep(j, 26) {
        auto it = data[j].lower_bound(lq);
        if (it != data[j].end() && *it <= rq) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int main() {
  int n, q;
  string s;
  cin >> n;
  cin >> s;
  cin >> q;
  vector<int> data[26];
  rep(i, n) {
    int v = s[i] - 'a';
    data[v].push_back(i);
  }

  rep(i, q) {
    int id;
    cin >> id;
    if (id == 1) {
      int iq;
      char cq;
      cin >> iq >> cq;
      iq--;
      int v = s[iq] - 'a';
      auto it = lower_bound(data[v].begin(), data[v].end(), iq);
      data[v].erase(it);
      v = cq - 'a';
      it = upper_bound(data[v].begin(), data[v].end(), iq);
      data[v].insert(it, iq);
    } else {
      int lq, rq;
      cin >> lq >> rq;
      lq--;
      rq--;
      int cnt = 0;
      rep(j, 26) {
        auto it = lower_bound(data[j].begin(), data[j].end(), lq);
        if (it != data[j].end() && *it <= rq) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(), (x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int main() {
  int n, q;
  string s;
  cin >> n;
  cin >> s;
  cin >> q;
  vector<int> data[26];
  rep(i, n) {
    int v = s[i] - 'a';
    data[v].push_back(i);
  }

  rep(i, q) {
    int id;
    cin >> id;
    if (id == 1) {
      int iq;
      char cq;
      cin >> iq >> cq;
      iq--;
      int v = s[iq] - 'a';
      auto it = lower_bound(data[v].begin(), data[v].end(), iq);
      data[v].erase(it);
      s[iq] = cq;
      v = cq - 'a';
      it = upper_bound(data[v].begin(), data[v].end(), iq);
      data[v].insert(it, iq);
    } else {
      int lq, rq;
      cin >> lq >> rq;
      lq--;
      rq--;
      int cnt = 0;
      rep(j, 26) {
        auto it = lower_bound(data[j].begin(), data[j].end(), lq);
        if (it != data[j].end() && *it <= rq) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read() {
  ll a = 0, b = getchar(), c = 1;
  while (!isdigit(b))
    c = b == '-' ? -1 : 1, b = getchar();
  while (isdigit(b))
    a = a * 10 + b - '0', b = getchar();
  return a * c;
}
int n, m, a[500005], b[500005], sum[500005];
string s;
void build(int x, int l, int r) {
  if (l == r) {
    b[x] = a[l], sum[x] = 1 << b[x];
    return;
  }
  int mid = l + r >> 1;
  build(x * 2, l, mid);
  build(x * 2 + 1, mid + 1, r);
  sum[x] = sum[x * 2] | sum[x * 2 + 1];
}
void gao(int x, int l, int r, int l1, int w) {
  if (l == l1 and r == l1) {
    b[x] = w, sum[x] = 1 << w;
    return;
  }
  int mid = (l + r) >> 1;
  if (l1 <= mid)
    gao(x * 2, l, mid, l1, w);
  else
    gao(x * 2 + 1, mid + 1, r, l1, w);
  sum[x] = sum[x * 2] | sum[x * 2 + 1];
}
int query(int x, int l, int r, int l1, int r1) {
  if (l >= l1 and r <= r1)
    return sum[x];
  int res = 0, mid = (l + r) >> 1;
  if (l1 <= mid)
    res |= query(x << 1, l, mid, l1, r1);
  if (r1 > mid)
    res |= query(x << 1 | 1, mid + 1, r, l1, r1);
  return res;
}
int main() {
  n = read();
  cin >> s;
  for (int i = 0; i < s.size(); i++)
    a[i + 1] = s[i] - 'a' + 1;
  build(1, 1, n);
  m = read();
  while (m--) {
    int ab = read(), x = read(), y;
    if (ab == 1) {
      y = getchar();
      while (y == ' ')
        y = getchar();
      y -= 'a' - 1;
      gao(1, 1, n, x, y);
    } else {
      y = read();
      printf("%d\n", __builtin_popcount(query(1, 1, n, x, y)));
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll read() {
  ll a = 0, b = getchar(), c = 1;
  while (!isdigit(b))
    c = b == '-' ? -1 : 1, b = getchar();
  while (isdigit(b))
    a = a * 10 + b - '0', b = getchar();
  return a * c;
}
int n, m, a[1000005], b[1000005], sum[1000005];
string s;
void build(int x, int l, int r) {
  if (l == r) {
    b[x] = a[l], sum[x] = 1 << b[x];
    return;
  }
  int mid = l + r >> 1;
  build(x * 2, l, mid);
  build(x * 2 + 1, mid + 1, r);
  sum[x] = sum[x * 2] | sum[x * 2 + 1];
}
void gao(int x, int l, int r, int l1, int w) {
  if (l == l1 and r == l1) {
    b[x] = w, sum[x] = 1 << w;
    return;
  }
  int mid = (l + r) >> 1;
  if (l1 <= mid)
    gao(x * 2, l, mid, l1, w);
  else
    gao(x * 2 + 1, mid + 1, r, l1, w);
  sum[x] = sum[x * 2] | sum[x * 2 + 1];
}
int query(int x, int l, int r, int l1, int r1) {
  if (l >= l1 and r <= r1)
    return sum[x];
  int res = 0, mid = (l + r) >> 1;
  if (l1 <= mid)
    res |= query(x << 1, l, mid, l1, r1);
  if (r1 > mid)
    res |= query(x << 1 | 1, mid + 1, r, l1, r1);
  return res;
}
int main() {
  n = read();
  cin >> s;
  for (int i = 0; i < s.size(); i++)
    a[i + 1] = s[i] - 'a' + 1;
  build(1, 1, n);
  m = read();
  while (m--) {
    int ab = read(), x = read(), y;
    if (ab == 1) {
      y = getchar();
      while (y == ' ')
        y = getchar();
      y -= 'a' - 1;
      gao(1, 1, n, x, y);
    } else {
      y = read();
      printf("%d\n", __builtin_popcount(query(1, 1, n, x, y)));
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 7;
const int mod = 1e9 + 7;
typedef long long ll;
int n, m, k;

set<int> s[30];
set<int>::iterator it1, it2;
char ch[maxn];
int main() {
  scanf("%d", &n);
  scanf("%s", ch + 1);
  for (int i = 1; i <= n; i++)
    s[int(ch[i] - 'a')].insert(i);
  int q;
  scanf("%d", &q);
  while (q--) {
    int type, x, y;
    scanf("%d%d", &type, &x);
    if (type == 1) {
      char c[2];
      scanf("%s", c);
      s[int(ch[x] - 'a')].erase(x);
      s[int(c[0] - 'a')].insert(x);
      ch[x] = c[0];
    } else {
      scanf("%d", &y);
      // cout << q << " " << x << ' ' << y << '\n';
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        // cout<<"i = " << i << ' ' << ans << '\n';
        // for (auto val : s[i])cout << val << ' '; cout << "\n";
        if (s[i].count(x) > 0 || s[i].count(y) > 0)
          ans++;
        else {
          it1 = s[i].upper_bound(x);
          it2 = s[i].upper_bound(y);
          if (it1 != s[i].end() && *it1 <= y)
            ans++;
        }
      }
      printf("%d\n", ans);
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 7;
const int mod = 1e9 + 7;
typedef long long ll;
int n, m, k;

set<int> s[30];
set<int>::iterator it1, it2;
char ch[maxn];
int main() {
  scanf("%d", &n);
  scanf("%s", ch + 1);
  for (int i = 1; i <= n; i++)
    s[int(ch[i] - 'a')].insert(i);
  int q;
  scanf("%d", &q);
  while (q--) {
    int type, x, y;
    scanf("%d%d", &type, &x);
    if (type == 1) {
      char c[2];
      scanf("%s", c);
      s[int(ch[x] - 'a')].erase(x);
      s[int(c[0] - 'a')].insert(x);
      ch[x] = c[0];
    } else {
      scanf("%d", &y);
      // cout << q << " " << x << ' ' << y << '\n';
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        // cout<<"i = " << i << ' ' << ans << '\n';
        // for (auto val : s[i])cout << val << ' '; cout << "\n";
        if (s[i].count(x) > 0 || s[i].count(y) > 0)
          ans++;
        else {
          it1 = s[i].upper_bound(x);
          it2 = s[i].upper_bound(y);
          if (it1 != s[i].end() && *it1 <= y)
            ans++;
        }
      }
      printf("%d\n", ans);
    }
  }

  return 0;
}
Problem p02763 (C++)

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

template <typename T> struct BIT {
  int n;
  vector<T> f;
  BIT(int n = 200000) : n(n), f(n + 1) {}

  T getsum(int p) {
    T ret = 0;
    for (++p; p > 0; p -= p & -p) {
      ret += f[p];
    }
    return ret;
  }
  void add(int p, T x) {
    for (++p; p <= n; p += p & -p) {
      f[p] += x;
    }
  }
};

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int Q;
  cin >> Q;
  vector<BIT<long long>> tmp(26);
  for (int i = 0; i < N; i++) {
    tmp[s[i] - 'a'].add(i, 1);
  }
  for (int i = 0; i < Q; i++) {
    int com;
    cin >> com;
    if (com == 1) {
      int i;
      cin >> i;
      i--;
      char c;
      cin >> c;
      tmp[s[i] - 'a'].add(i, -1);
      s[i] = c;
      tmp[s[i] - 'a'].add(i, 1);
    }
    if (com == 2) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        ans += tmp[i].getsum(r) - tmp[i].getsum(l - 1) > 0;
      }
      cout << ans << endl;
    }
  }
}

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

template <typename T> struct BIT {
  int n;
  vector<T> f;
  BIT(int n = 500002) : n(n), f(n + 1) {}

  T getsum(int p) {
    T ret = 0;
    for (++p; p > 0; p -= p & -p) {
      ret += f[p];
    }
    return ret;
  }
  void add(int p, T x) {
    for (++p; p <= n; p += p & -p) {
      f[p] += x;
    }
  }
};

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int Q;
  cin >> Q;
  vector<BIT<long long>> tmp(26);
  for (int i = 0; i < N; i++) {
    tmp[s[i] - 'a'].add(i, 1);
  }
  for (int i = 0; i < Q; i++) {
    int com;
    cin >> com;
    if (com == 1) {
      int i;
      cin >> i;
      i--;
      char c;
      cin >> c;
      tmp[s[i] - 'a'].add(i, -1);
      s[i] = c;
      tmp[s[i] - 'a'].add(i, 1);
    }
    if (com == 2) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        ans += tmp[i].getsum(r) - tmp[i].getsum(l - 1) > 0;
      }
      cout << ans << endl;
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
class fenwick {
  vector<int> t;

public:
  fenwick() { t.resize(20001); }
  void add(int p, int v) {
    for (; p < (int)t.size(); p += p & -p) {
      t[p] += v;
    }
  }
  int query(int p) {
    int ret = 0;
    if (p < 0) {
      return 0;
    }
    for (; p; p -= p & -p) {
      ret += t[p];
    }
    return ret;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<fenwick> A;
  A.resize(26);
  for (int i = 0; i < n; ++i) {
    A[s[i] - 'a'].add(i + 1, 1);
  }
  while (q--) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      A[s[i - 1] - 'a'].add(i, -1);
      A[c - 'a'].add(i, 1);
      s[i - 1] = c;
    }
    if (type == 2) {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        if (A[i].query(r) - A[i].query(l - 1) > 0) {
          ++ans;
        }
      }
      cout << ans << '\n';
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
class fenwick {
  vector<int> t;

public:
  fenwick() { t.resize(500001); }
  void add(int p, int v) {
    for (; p < (int)t.size(); p += p & -p) {
      t[p] += v;
    }
  }
  int query(int p) {
    int ret = 0;
    if (p < 0) {
      return 0;
    }
    for (; p; p -= p & -p) {
      ret += t[p];
    }
    return ret;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, q;
  string s;
  cin >> n >> s >> q;
  vector<fenwick> A;
  A.resize(26);
  for (int i = 0; i < n; ++i) {
    A[s[i] - 'a'].add(i + 1, 1);
  }
  while (q--) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      A[s[i - 1] - 'a'].add(i, -1);
      A[c - 'a'].add(i, 1);
      s[i - 1] = c;
    }
    if (type == 2) {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        if (A[i].query(r) - A[i].query(l - 1) > 0) {
          ++ans;
        }
      }
      cout << ans << '\n';
    }
  }
  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;
typedef long long ll;
const int INT_INF = 2147483647;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<int, int> P;
#define SEG_LEN (1 << 18)

int seg[26][SEG_LEN * 2];

void add(int cha, int ind, int v) {
  ind += SEG_LEN;
  seg[cha][ind] += v;
  while (true) {
    ind /= 2;
    if (ind == 0)
      break;
    seg[cha][ind] = seg[cha][ind * 2] + seg[cha][ind * 2 + 1];
  }
}

int sum(int cha, int l, int r) {
  l += SEG_LEN;
  r += SEG_LEN;
  int ans = 0;
  while (l < r) {
    if (l % 2 == 1) {
      ans += seg[cha][l];
      l++;
    }
    l /= 2;
    if (r % 2 == 1) {
      ans += seg[cha][r - 1];
      r--;
    }
    r /= 2;
  }
  return ans;
}

int main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  rep(i, n) { add(s[i] - 'a', i + 1, 1); }
  rep(j, q) {
    int id;
    cin >> id;
    if (id == 1) {
      int i;
      char c;
      cin >> i >> c;
      add(s[i - 1] - 'a', i, -1);
      add(c - 'a', i, 1);
      s[i - 1] = c;
    } else {
      int ans = 0;
      int l, r;
      cin >> l >> r;
      rep(k, 26) {
        if (sum(k, l, r + 1))
          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;
const int INT_INF = 2147483647;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
typedef pair<int, int> P;
#define SEG_LEN (1 << 20)

int seg[26][SEG_LEN * 2];

void add(int cha, int ind, int v) {
  ind += SEG_LEN;
  seg[cha][ind] += v;
  while (true) {
    ind /= 2;
    if (ind == 0)
      break;
    seg[cha][ind] = seg[cha][ind * 2] + seg[cha][ind * 2 + 1];
  }
}

int sum(int cha, int l, int r) {
  l += SEG_LEN;
  r += SEG_LEN;
  int ans = 0;
  while (l < r) {
    if (l % 2 == 1) {
      ans += seg[cha][l];
      l++;
    }
    l /= 2;
    if (r % 2 == 1) {
      ans += seg[cha][r - 1];
      r--;
    }
    r /= 2;
  }
  return ans;
}

int main() {
  int n, q;
  string s;
  cin >> n >> s >> q;
  rep(i, n) { add(s[i] - 'a', i + 1, 1); }
  rep(j, q) {
    int id;
    cin >> id;
    if (id == 1) {
      int i;
      char c;
      cin >> i >> c;
      add(s[i - 1] - 'a', i, -1);
      add(c - 'a', i, 1);
      s[i - 1] = c;
    } else {
      int ans = 0;
      int l, r;
      cin >> l >> r;
      rep(k, 26) {
        if (sum(k, l, r + 1))
          ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main() {
  int N, Q;
  string S;
  cin >> N >> S >> Q;

  vector<set<int>> alpha(26, set<int>());
  rep(i, S.size()) { alpha[(int)(S[i] - 'a')].insert(i); }

  rep(i, Q) {
    int a;
    cin >> a;
    if (a == 1) {
      int b;
      char c;
      cin >> b >> c;
      b--;
      alpha[(int)(S[b] - 'a')].erase(b);
      alpha[(int)(c - 'a')].insert(b);
      S[i] = c;
    } else {
      int b, c;
      cin >> b >> c;
      b--;
      c--;
      int cnt = 0;
      rep(j, 26) {
        auto itr = alpha[j].lower_bound(b);
        if (itr == alpha[j].end())
          continue;
        if (*itr <= c)
          cnt++;
      }

      cout << cnt << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define repd(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) repd(i, 0, n)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main() {
  int N, Q;
  string S;
  cin >> N >> S >> Q;

  vector<set<int>> alpha(26, set<int>());
  rep(i, S.size()) { alpha[(int)(S[i] - 'a')].insert(i); }

  rep(i, Q) {
    int a;
    cin >> a;
    if (a == 1) {
      int b;
      char c;
      cin >> b >> c;
      b--;
      alpha[(int)(S[b] - 'a')].erase(b);
      alpha[(int)(c - 'a')].insert(b);
      S[b] = c;
    } else {
      int b, c;
      cin >> b >> c;
      b--;
      c--;
      int cnt = 0;
      rep(j, 26) {
        auto itr = alpha[j].lower_bound(b);
        if (itr == alpha[j].end())
          continue;
        if (*itr <= c)
          cnt++;
      }

      cout << cnt << endl;
    }
  }
  return 0;
}