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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#pragma warning(disable : 4996)

#define N_MAX 100001
#define M_MAX 100001
#define K_MAX 100001

int N, M, K;

struct NodeInfo {
  int index;
  int dec;
  int size;
  int rank;
  NodeInfo *p;
  NodeInfo() : index(-1), dec(1), size(1), rank(0), p(NULL) {}
};

NodeInfo infos[N_MAX];

int GetGroupIndex(NodeInfo *ni) {
  while (ni->p != NULL) {
    ni = ni->p;
  }
  return ni->index;
}
void Merge(NodeInfo *a, NodeInfo *b) {
  int ai = GetGroupIndex(a);
  int bi = GetGroupIndex(b);
  if (ai != bi) {
    if (infos[ai].rank > infos[bi].rank) {
      int tmp = ai;
      ai = bi;
      bi = tmp;
    }
    infos[bi].p = &infos[ai];
    infos[ai].size += infos[bi].size;
    if (infos[ai].rank < infos[bi].rank + 1) {
      infos[ai].rank = infos[bi].rank + 1;
    }
  }
  a->dec += 1;
  b->dec += 1;
}

int main(void) {
  scanf("%d %d %d", &N, &M, &K);
  for (int i = 0; i <= N; ++i) {
    infos[i].index = i;
  }
  // friend ship
  for (int i = 0; i < M; ++i) {
    int A, B;
    scanf("%d %d", &A, &B);
    Merge(&infos[A], &infos[B]);
  }
  // block ship
  for (int i = 0; i < K; ++i) {
    int C, D;
    scanf("%d %d", &C, &D);
    if (GetGroupIndex(&infos[C]) == GetGroupIndex(&infos[D])) {
      ++infos[C].dec;
      ++infos[D].dec;
    }
  }
  for (int i = 1; i <= N; ++i) {
    int gi = GetGroupIndex(&infos[i]);
    printf("%d ", infos[gi].size - infos[i].dec);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#pragma warning(disable : 4996)

#define N_MAX 100001
#define M_MAX 100001
#define K_MAX 100001

int N, M, K;

struct NodeInfo {
  int index;
  int dec;
  int size;
  int rank;
  NodeInfo *p;
  NodeInfo() : index(-1), dec(1), size(1), rank(0), p(NULL) {}
};

NodeInfo infos[N_MAX];

int GetGroupIndex(NodeInfo *ni) {
  while (ni->p != NULL) {
    ni = ni->p;
  }
  return ni->index;
}
void Merge(NodeInfo *a, NodeInfo *b) {
  int ai = GetGroupIndex(a);
  int bi = GetGroupIndex(b);
  if (ai != bi) {
    if (infos[ai].rank < infos[bi].rank) {
      int tmp = ai;
      ai = bi;
      bi = tmp;
    }
    infos[bi].p = &infos[ai];
    infos[ai].size += infos[bi].size;
    if (infos[ai].rank < infos[bi].rank + 1) {
      infos[ai].rank = infos[bi].rank + 1;
    }
  }
  a->dec += 1;
  b->dec += 1;
}

int main(void) {
  scanf("%d %d %d", &N, &M, &K);
  for (int i = 0; i <= N; ++i) {
    infos[i].index = i;
  }
  // friend ship
  for (int i = 0; i < M; ++i) {
    int A, B;
    scanf("%d %d", &A, &B);
    Merge(&infos[A], &infos[B]);
  }
  // block ship
  for (int i = 0; i < K; ++i) {
    int C, D;
    scanf("%d %d", &C, &D);
    if (GetGroupIndex(&infos[C]) == GetGroupIndex(&infos[D])) {
      ++infos[C].dec;
      ++infos[D].dec;
    }
  }
  for (int i = 1; i <= N; ++i) {
    int gi = GetGroupIndex(&infos[i]);
    printf("%d ", infos[gi].size - infos[i].dec);
  }
  return 0;
}
Problem p02762 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int par[100003], sz[100003];
int find(int v) {
  if (par[v] == v)
    return v;
  return par[v] = find(par[v]);
}
void union_set(int u, int v) {
  u = find(u);
  v = find(v);
  if (u != v) {
    if (sz[u] < sz[v])
      swap(u, v);
    par[v] = u;
    sz[u] += sz[v];
  }
}
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n + 1];
  int b[n + 1];
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  int u, v;
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    a[u]++;
    a[v]++;
    union_set(u, v);
  }
  u = find(u);
  for (int i = 0; i < k; i++) {
    cin >> u >> v;
    if (find(u) == find(v))
      b[u]++, b[v]++;
  }
  for (int i = 1; i <= n; i++) {
    int ans = sz[find(i)] - 1 - a[i] - b[i];
    cout << ans << " ";
  }
  cout << "\n";
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int par[100003], sz[100003];
int find(int v) {
  if (par[v] == v)
    return v;
  return par[v] = find(par[v]);
}
void union_set(int u, int v) {
  u = find(u);
  v = find(v);
  if (u != v) {
    if (sz[u] < sz[v])
      swap(u, v);
    par[v] = u;
    sz[u] += sz[v];
  }
}
int main() {
  int n, m, k;
  cin >> n >> m >> k;
  int a[n + 1];
  int b[n + 1];
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  int u, v;
  for (int i = 1; i <= n; i++) {
    par[i] = i;
    sz[i] = 1;
  }
  for (int i = 0; i < m; i++) {
    cin >> u >> v;
    a[u]++;
    a[v]++;
    union_set(u, v);
  }
  // u=find(u);
  for (int i = 0; i < k; i++) {
    cin >> u >> v;
    if (find(u) == find(v))
      b[u]++, b[v]++;
  }
  for (int i = 1; i <= n; i++) {
    int ans = sz[find(i)] - 1 - a[i] - b[i];
    cout << ans << " ";
  }
  cout << "\n";
  return 0;
}
Problem p02762 (C++)

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

using namespace std;

int n, m, k;
vector<int> friends[100000];
unordered_set<int> blocks[100000];
bool visited[100000];
unordered_set<int> comp;
int ans[100000];

void dfs(int v) {
  visited[v - 1] = 1;
  comp.insert(v);

  for (auto a : friends[v - 1]) {
    if (!visited[a - 1])
      dfs(a);
  }
}

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

  cin >> n >> m >> k;

  for (int i = 0; i < n; i++) {
    visited[i] = 0;
    ans[i] = 0;
  }

  for (int i = 0; i < m; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    friends[t1 - 1].push_back(t2);
    friends[t2 - 1].push_back(t1);
  }

  for (int i = 0; i < k; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    blocks[t1 - 1].insert(t2);
    blocks[t2 - 1].insert(t1);
  }

  for (int i = 0; i < n; i++) {
    comp.clear();
    if (!visited[i])
      dfs(i + 1);

    for (auto a : comp) {
      int b = 0;
      if (comp.size() <= blocks[a - 1].size()) {
        for (const auto &x : comp) {
          if (blocks[a - 1].count(x) != 0)
            b++;
        }
      } else {
        for (const auto &x : blocks[a - 1]) {
          if (comp.count(x) != 0)
            b++;
        }
      }
      ans[a - 1] = comp.size() - 1 - b - friends[a - 1].size();
    }
  }

  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";

  return 0;
}


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

using namespace std;

int n, m, k;
vector<int> friends[100000];
unordered_set<int> blocks[100000];
bool visited[100000];
unordered_set<int> comp;
int ans[100000];

void dfs(int v) {
  visited[v - 1] = 1;
  comp.insert(v);

  for (auto a : friends[v - 1]) {
    if (!visited[a - 1])
      dfs(a);
  }
}

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

  cin >> n >> m >> k;

  for (int i = 0; i < n; i++) {
    visited[i] = 0;
    ans[i] = 0;
  }

  for (int i = 0; i < m; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    friends[t1 - 1].push_back(t2);
    friends[t2 - 1].push_back(t1);
  }

  for (int i = 0; i < k; i++) {
    int t1, t2;
    cin >> t1 >> t2;
    blocks[t1 - 1].insert(t2);
    blocks[t2 - 1].insert(t1);
  }

  for (int i = 0; i < n; i++) {
    if (comp.size() != 0)
      comp.clear();
    if (!visited[i])
      dfs(i + 1);

    for (auto a : comp) {
      int b = 0;
      if (comp.size() <= blocks[a - 1].size()) {
        for (const auto &x : comp) {
          if (blocks[a - 1].count(x) != 0)
            b++;
        }
      } else {
        for (const auto &x : blocks[a - 1]) {
          if (comp.count(x) != 0)
            b++;
        }
      }
      ans[a - 1] = comp.size() - 1 - b - friends[a - 1].size();
    }
  }

  for (int i = 0; i < n; i++)
    cout << ans[i] << " ";

  return 0;
}
Problem p02762 (C++)

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

vector<int64_t> par(100000);
vector<int64_t> r(100000);

int find(int64_t n) {
  if (par.at(n) == n) {
    return n;
  } else {
    return par.at(n) = find(par.at(n));
  }
}
void unite(int64_t a, int64_t b) {
  int64_t x = find(a);
  int64_t y = find(b);
  if (x == y) {
    return;
  }
  if (r.at(x) > r.at(y)) {
    par.at(y) = x;
  } else {
    par.at(x) = y;
    if (r.at(x) == r.at(y)) {
      r.at(y)++;
    }
  }
}

int main() {
  int64_t N, M, K;
  cin >> N >> M >> K;
  vector<pair<int64_t, int64_t>> A(100000), C(100000);
  vector<int64_t> block(100000, 0), num(100000, 0);
  for (int64_t i = 0; i < N; i++) {
    r.at(i) = 0;
    par.at(i) = i;
  }
  for (int64_t i = 0; i < M; i++) {
    cin >> A.at(i).first >> A.at(i).second;
    unite(A.at(i).first - 1, A.at(i).second - 1);
    block.at(A.at(i).first - 1)++;
    block.at(A.at(i).second - 1)++;
  }
  for (int64_t i = 0; i < K; i++) {
    cin >> C.at(i).first >> C.at(i).second;
    if (find(C.at(i).first) == find(C.at(i).second)) {
      block.at(C.at(i).first - 1)++;
      block.at(C.at(i).second - 1)++;
    }
  }
  for (int64_t i = 0; i < N; i++) {
    num.at(find(i))++;
  }

  for (int64_t i = 0; i < N; i++) {
    cout << num.at(find(i)) - 1 - block.at(i) << " ";
  }
  cout << endl;
}


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

vector<int64_t> par(100000);
vector<int64_t> r(100000);

int find(int64_t n) {
  if (par.at(n) == n) {
    return n;
  } else {
    return par.at(n) = find(par.at(n));
  }
}
void unite(int64_t a, int64_t b) {
  int64_t x = find(a);
  int64_t y = find(b);
  if (x == y) {
    return;
  }
  if (r.at(x) > r.at(y)) {
    par.at(y) = x;
  } else {
    par.at(x) = y;
    if (r.at(x) == r.at(y)) {
      r.at(y)++;
    }
  }
}

int main() {
  int64_t N, M, K;
  cin >> N >> M >> K;
  vector<pair<int64_t, int64_t>> A(100000), C(100000);
  vector<int64_t> block(100000, 0), num(100000, 0);
  for (int64_t i = 0; i < N; i++) {
    r.at(i) = 0;
    par.at(i) = i;
  }
  for (int64_t i = 0; i < M; i++) {
    cin >> A.at(i).first >> A.at(i).second;
    unite(A.at(i).first - 1, A.at(i).second - 1);
    block.at(A.at(i).first - 1)++;
    block.at(A.at(i).second - 1)++;
  }
  for (int64_t i = 0; i < K; i++) {
    cin >> C.at(i).first >> C.at(i).second;
    if (find(C.at(i).first - 1) == find(C.at(i).second - 1)) {
      block.at(C.at(i).first - 1)++;
      block.at(C.at(i).second - 1)++;
    }
  }
  for (int64_t i = 0; i < N; i++) {
    num.at(find(i))++;
  }

  for (int64_t i = 0; i < N; i++) {
    cout << num.at(find(i)) - 1 - block.at(i) << " ";
  }
  cout << endl;
}
Problem p02762 (C++)

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

struct UnionFind {
  vector<int> d;
  UnionFind(int n) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int deg[10005];
vector<int> to[10005];

int main() {

  int n, m, k;
  cin >> n >> m >> k;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  rep(i, n) {
    int ans = uf.size(i) - 1 - deg[i];
    for (int u : to[i]) {
      if (uf.same(i, u))
        --ans;
    }
    printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
  }
  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;

struct UnionFind {
  vector<int> d;
  UnionFind(int n) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int deg[100005];
vector<int> to[100005];

int main() {

  int n, m, k;
  cin >> n >> m >> k;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  rep(i, n) {
    int ans = uf.size(i) - 1 - deg[i];
    for (int u : to[i]) {
      if (uf.same(i, u))
        --ans;
    }
    printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
  }
  return 0;
}
Problem p02762 (C++)

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

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int deg[100005];
vector<int> to[100005];

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  rep(i, n) {
    int ans = uf.size(i) - 1 - deg[i];
    for (int u : to[i]) {
      if (uf.same(i, u))
        --ans;
    }
    printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
  }
  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>;

struct UnionFind {
  vector<int> d;
  UnionFind(int n = 0) : d(n, -1) {}
  int find(int x) {
    if (d[x] < 0)
      return x;
    return d[x] = find(d[x]);
  }
  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    if (d[x] > d[y])
      swap(x, y);
    d[x] += d[y];
    d[y] = x;
    return true;
  }
  bool same(int x, int y) { return find(x) == find(y); }
  int size(int x) { return -d[find(x)]; }
};

int deg[100005];
vector<int> to[100005];

int main() {
  int n, m, k;
  cin >> n >> m >> k;
  UnionFind uf(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    deg[a]++;
    deg[b]++;
    uf.unite(a, b);
  }
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    to[a].push_back(b);
    to[b].push_back(a);
  }
  rep(i, n) {
    int ans = uf.size(i) - 1 - deg[i];
    for (int u : to[i]) {
      if (uf.same(i, u))
        --ans;
    }
    printf("%d%c", ans, i == n - 1 ? '\n' : ' ');
  }
  return 0;
}
Problem p02763 (C++)

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;
  vector<int> tp(q), p1(q), p2(q);
  vector<char> p3(q);
  rep(i, q) {
    cin >> tp.at(i) >> p1.at(i);
    if (tp.at(i) == 2)
      cin >> p2.at(i);
    else
      cin >> p3.at(i);
  }
  vector<set<int>> vs(26);
  rep(i, s.size()) { vs.at(s.at(i) - 'a').insert(i + 1); }

  rep(i, q) {
    if (tp.at(i) == 1) {
      rep(j, 26) { vs.at(j).erase(p1.at(i)); }
      vs.at(p3.at(i) - 'a').insert(p1.at(i));
    } else {
      int cnt = 0;
      rep(j, 26) {
        auto lt = lower_bound(vs.at(j).begin(), vs.at(j).end(), p1.at(i));
        if (lt != vs.at(j).end() && *lt <= p2.at(i)) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }

  // rep(i,26){
  //     if(vs.at(i).size() == 0) continue;
  //     char c = 'a' + i;
  //     cout << c << ":";
  //     for(auto x:vs.at(i)){
  //         cout << x << " ";
  //     }
  //     cout << endl;
  // }
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;
  vector<int> tp(q), p1(q), p2(q);
  vector<char> p3(q);
  rep(i, q) {
    cin >> tp.at(i) >> p1.at(i);
    if (tp.at(i) == 2)
      cin >> p2.at(i);
    else
      cin >> p3.at(i);
  }
  vector<set<int>> vs(26);
  rep(i, s.size()) { vs.at(s.at(i) - 'a').insert(i + 1); }

  rep(i, q) {
    if (tp.at(i) == 1) {
      rep(j, 26) { vs.at(j).erase(p1.at(i)); }
      vs.at(p3.at(i) - 'a').insert(p1.at(i));
    } else {
      int cnt = 0;
      rep(j, 26) {
        auto lt = vs.at(j).lower_bound(p1.at(i));
        if (lt != vs.at(j).end() && *lt <= p2.at(i)) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }

  // rep(i,26){
  //     if(vs.at(i).size() == 0) continue;
  //     char c = 'a' + i;
  //     cout << c << ":";
  //     for(auto x:vs.at(i)){
  //         cout << x << " ";
  //     }
  //     cout << endl;
  // }
  return 0;
}
Problem p02763 (C++)

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

int BIT[300010][30], n, q, t, l, r, pos, ans;
char ch;
string s;

void updateBIT(int pos, int f, int val) {

  while (pos <= n) {
    BIT[pos][f] += val;
    pos += (pos & (-pos));
  }

  return;
}

int queryBIT(int pos, int f) {

  int myAns = 0;
  while (pos > 0) {
    myAns += BIT[pos][f];
    pos -= (pos & (-pos));
  }

  return myAns;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> s >> q;
  for (int i = 1; i <= n; i++)
    updateBIT(i, s[i - 1] - 'a', 1);

  while (q--) {
    cin >> t;
    if (t == 1) {
      cin >> pos >> ch;
      updateBIT(pos, s[pos - 1] - 'a', -1);
      s[pos - 1] = ch;
      updateBIT(pos, s[pos - 1] - 'a', 1);
    } else {
      cin >> l >> r;
      ans = 0;
      for (int i = 0; i <= 'z' - 'a'; i++)
        ans += min(queryBIT(r, i) - queryBIT(l - 1, i), 1);
      cout << ans << "\n";
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int, int>
#define pll pair<long long, long long>
using namespace std;

int BIT[500010][30], n, q, t, l, r, pos, ans;
char ch;
string s;

void updateBIT(int pos, int f, int val) {

  while (pos <= n) {
    BIT[pos][f] += val;
    pos += (pos & (-pos));
  }

  return;
}

int queryBIT(int pos, int f) {

  int myAns = 0;
  while (pos > 0) {
    myAns += BIT[pos][f];
    pos -= (pos & (-pos));
  }

  return myAns;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> s >> q;
  for (int i = 1; i <= n; i++)
    updateBIT(i, s[i - 1] - 'a', 1);

  while (q--) {
    cin >> t;
    if (t == 1) {
      cin >> pos >> ch;
      updateBIT(pos, s[pos - 1] - 'a', -1);
      s[pos - 1] = ch;
      updateBIT(pos, s[pos - 1] - 'a', 1);
    } else {
      cin >> l >> r;
      ans = 0;
      for (int i = 0; i <= 'z' - 'a'; i++)
        ans += min(queryBIT(r, i) - queryBIT(l - 1, i), 1);
      cout << ans << "\n";
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define all(x) (x).begin(), (x).end()
ll inf = LLONG_MAX;
long double pi = M_PI;
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

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

  set<ll> st;
  map<char, set<ll>> place;
  for (char c = 'a'; c <= 'z'; c++)
    place[c] = st;
  for (ll i = 0; i < n; i++) {
    place[s[i]].insert(i);
  }

  /*for (ll i = 0; i < 26; i++) {
    for (ll j = 0; j < n - 1; j++) {
      cout << v[i][j] << " ";
    }
    cout << v[i][n - 1] << endl;
  }*/

  for (ll i = 0; i < q; i++) {
    ll x;
    cin >> x;

    if (x == 1) {
      ll y;
      char c;
      cin >> y >> c;

      if (s[y - 1] == c)
        continue;

      place[s[y - 1]].erase(y - 1);
      place[c].insert(y - 1);
      s[y - 1] = c;
    } else {
      ll l, r;
      cin >> l >> r;

      ll cnt = 0;
      for (char c = 'a'; c <= 'z'; c++) {
        auto itr = lower_bound(place[c].begin(), place[c].end(), l - 1);
        if (itr != place[c].end() && *itr < r) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define all(x) (x).begin(), (x).end()
ll inf = LLONG_MAX;
long double pi = M_PI;
void Yes() { cout << "Yes" << endl; }
void No() { cout << "No" << endl; }
void YES() { cout << "YES" << endl; }
void NO() { cout << "NO" << endl; }

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

  set<ll> st;
  map<char, set<ll>> place;
  for (char c = 'a'; c <= 'z'; c++)
    place[c] = st;
  for (ll i = 0; i < n; i++) {
    place[s[i]].insert(i);
  }

  /*for (ll i = 0; i < 26; i++) {
    for (ll j = 0; j < n - 1; j++) {
      cout << v[i][j] << " ";
    }
    cout << v[i][n - 1] << endl;
  }*/

  for (ll i = 0; i < q; i++) {
    ll x;
    cin >> x;

    if (x == 1) {
      ll y;
      char c;
      cin >> y >> c;

      if (s[y - 1] == c)
        continue;

      place[s[y - 1]].erase(y - 1);
      place[c].insert(y - 1);
      s[y - 1] = c;
    } else {
      ll l, r;
      cin >> l >> r;

      ll cnt = 0;
      for (char c = 'a'; c <= 'z'; c++) {
        auto itr = place[c].lower_bound(l - 1);
        if (itr != place[c].end() && *itr < r) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }
}
Problem p02763 (C++)

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

using namespace std;

int main() {
  int N, Q;
  string s;
  cin >> N >> s >> Q;
  vector<set<int>> st(26);
  for (int i = 0; i < N; ++i) {
    st[(int)(s[i] - 'a')].insert(i);
  }
  for (int i = 0; i < 26; ++i) {
    st[i].insert(N);
  }

  vector<int> ans;
  for (int q = 0; q < Q; ++q) {
    int t;
    cin >> t;
    if (t == 1) {
      int idx;
      char c;
      cin >> idx >> c;
      --idx;
      for (int i = 0; i < 26; ++i) {
        if (st[i].count(idx) > 0) {
          st[i].erase(idx);
        }
        if (i == (int)(c - 'a')) {
          st[i].insert(idx);
        }
      }
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      int cnt = 0;
      for (int i = 0; i < 26; ++i) {
        cnt += *lower_bound(st[i].begin(), st[i].end(), l) < r;
      }
      ans.emplace_back(cnt);
    }
  }
  for (int val : ans) {
    cout << val << endl;
  }

  return 0;
}


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

using namespace std;

int main() {
  int N, Q;
  string s;
  cin >> N >> s >> Q;
  vector<set<int>> st(26);
  for (int i = 0; i < N; ++i) {
    st[(int)(s[i] - 'a')].insert(i);
  }
  for (int i = 0; i < 26; ++i) {
    st[i].insert(N);
  }

  vector<int> ans;
  for (int q = 0; q < Q; ++q) {
    int t;
    cin >> t;
    if (t == 1) {
      int idx;
      char c;
      cin >> idx >> c;
      --idx;
      for (int i = 0; i < 26; ++i) {
        if (st[i].count(idx) > 0) {
          st[i].erase(idx);
        }
        if (i == (int)(c - 'a')) {
          st[i].insert(idx);
        }
      }
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      int cnt = 0;
      for (int i = 0; i < 26; ++i) {
        cnt += *st[i].lower_bound(l) < r;
      }
      ans.emplace_back(cnt);
    }
  }
  for (int val : ans) {
    cout << val << endl;
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
#define debug(v)                                                               \
  { cerr << "\033[1;36m[debug]\033[m " << #v << " : " << v << endl; }
#define all(x) (x).begin(), (x).end()

// using int64 = long long;
typedef long long int64;

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

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int type;
    cin >> type;
    switch (type) {
    case 1:
      int i;
      char c;
      cin >> i >> c;
      alpha[s[i - 1] - 'a'].erase(i - 1);
      alpha[c - 'a'].insert(i - 1);
      s[i - 1] = c;
      break;
    case 2:
      int l, r;
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        auto it = lower_bound(all(alpha[i]), l - 1);
        if (it != alpha[i].end() && *it < r)
          ans++;
      }
      cout << ans << endl;
      break;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <utility>
#include <vector>

using namespace std;
template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}
#define debug(v)                                                               \
  { cerr << "\033[1;36m[debug]\033[m " << #v << " : " << v << endl; }
#define all(x) (x).begin(), (x).end()

// using int64 = long long;
typedef long long int64;

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

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int type;
    cin >> type;
    switch (type) {
    case 1:
      int i;
      char c;
      cin >> i >> c;
      alpha[s[i - 1] - 'a'].erase(i - 1);
      alpha[c - 'a'].insert(i - 1);
      s[i - 1] = c;
      break;
    case 2:
      int l, r;
      cin >> l >> r;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        auto it = alpha[i].lower_bound(l - 1);
        if (it != alpha[i].end() && *it < r)
          ans++;
      }
      cout << ans << endl;
      break;
    }
  }
}
Problem p02763 (C++)

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

#define sz(x) (int)x.size()

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  string a;
  cin >> a;
  set<int> s[26];
  for (int i = 0; i < sz(a); i++)
    a[i] -= 'a', s[a[i]].insert(i);
  int q;
  cin >> q;
  for (int t, l; q--;) {
    cin >> t >> l, l--;
    if (t == 1) {
      char c;
      cin >> c;
      s[a[l]].erase(l);
      a[l] = c - 'a';
      s[a[l]].insert(l);
    } else {
      int r, ans = 0;
      cin >> r;
      for (int i = 0; i < 26; i++) {
        auto x = s[i].lower_bound(l);
        ans += (x != s[i].end() && *x < r);
      }
      cout << ans << "\n";
    }
  }
  return 0;
}


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

#define sz(x) (int)x.size()

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  string a;
  cin >> a;
  set<int> s[26];
  for (int i = 0; i < sz(a); i++)
    a[i] -= 'a', s[a[i]].insert(i);
  int q;
  cin >> q;
  for (int t, l; q--;) {
    cin >> t >> l, l--;
    if (t == 1) {
      char c;
      cin >> c;
      s[a[l]].erase(l);
      a[l] = c - 'a';
      s[a[l]].insert(l);
    } else {
      int r, ans = 0;
      cin >> r;
      for (int i = 0; i < 26; i++) {
        auto x = s[i].lower_bound(l);
        ans += (x != s[i].end() && *x < r);
      }
      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++)
#define PI 3.14159265359
#define INF 1000100100
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<ll, ll>
#define PP pair<P, P>
using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int Q;
  cin >> Q;
  vector<vector<int>> ap(26);
  rep(i, n) ap[s[i] - 'a'].push_back(i);
  rep(i, Q) {
    int a;
    cin >> a;
    if (a == 1) {
      int it;
      char next;
      cin >> it >> next;
      it--;
      auto whe = lower_bound(all(ap[s[it] - 'a']), it);
      ap[s[it] - 'a'].erase(whe);
      if (ap[next - 'a'].empty())
        ap[next - 'a'].push_back(it);
      else {
        whe = lower_bound(all(ap[next - 'a']), it);
        whe = ap[next - 'a'].insert(whe - 1, it);
      }
      s[it] = next;
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(j, 26) {
        if (ap[j].empty())
          continue;
        auto whe2 = lower_bound(all(ap[j]), l);
        if (ap[j][whe2 - ap[j].begin()] <= r &&
            whe2 - ap[j].begin() != ap[j].size())
          ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define PI 3.14159265359
#define INF 1000100100
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define P pair<ll, ll>
#define PP pair<P, P>
using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int Q;
  cin >> Q;
  vector<vector<int>> ap(26);
  rep(i, n) ap[s[i] - 'a'].push_back(i);
  rep(i, Q) {
    int a;
    cin >> a;
    if (a == 1) {
      int it;
      char next;
      cin >> it >> next;
      it--;
      auto whe = lower_bound(all(ap[s[it] - 'a']), it);
      ap[s[it] - 'a'].erase(whe);
      if (ap[next - 'a'].empty())
        ap[next - 'a'].push_back(it);
      else {
        whe = lower_bound(all(ap[next - 'a']), it);
        whe = ap[next - 'a'].insert(whe, it);
      }
      s[it] = next;
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(j, 26) {
        if (ap[j].empty())
          continue;
        auto whe2 = lower_bound(all(ap[j]), l);
        if (ap[j][whe2 - ap[j].begin()] <= r &&
            whe2 - ap[j].begin() != ap[j].size())
          ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \
  for (auto elementPrintVec : x) {                                             \
    cout << elementPrintVec << " ";                                            \
  }                                                                            \
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  set<int> S[26];
  /*
  rep(i, 26){
    rep(j, n){
      if(s[j]-'a'==i){
        S[i].insert(j);
      }
    }
  }*/
  rep(i, n) { S[s[i] - 'a'].insert(i); }
  int q;
  cin >> q;
  rep(i, q) {
    int t;
    cin >> t;
    if (t == 1) {
      int iq;
      char cq;
      cin >> iq >> cq;
      iq--;
      S[s[iq] - 'a'].erase(iq);
      s[iq] = cq;
      S[cq - 'a'].insert(iq);
    } else {
      int lq, rq;
      cin >> lq >> rq;
      int ans = 0;
      lq--;
      rq--;
      rep(j, 26) {
        auto it = lower_bound(S[j].begin(), S[j].end(), lq);
        if (it != S[j].end() && *it <= rq) {
          ans++;
        }
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define repo(i, n) for (int i = 1; i <= n; i++)
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x)                                                            \
  for (auto elementPrintVec : x) {                                             \
    cout << elementPrintVec << " ";                                            \
  }                                                                            \
  cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  set<int> S[26];
  /*
  rep(i, 26){
    rep(j, n){
      if(s[j]-'a'==i){
        S[i].insert(j);
      }
    }
  }*/
  rep(i, n) { S[s[i] - 'a'].insert(i); }
  int q;
  cin >> q;
  rep(i, q) {
    int t;
    cin >> t;
    if (t == 1) {
      int iq;
      char cq;
      cin >> iq >> cq;
      iq--;
      S[s[iq] - 'a'].erase(iq);
      s[iq] = cq;
      S[cq - 'a'].insert(iq);
    } else {
      int lq, rq;
      cin >> lq >> rq;
      int ans = 0;
      lq--;
      rq--;
      rep(j, 26) {
        // auto it=lower_bound(S[j].begin(), S[j].end(), lq);
        auto it = S[j].lower_bound(lq);
        if (it != S[j].end() && *it <= rq) {
          ans++;
        }
      }
      printf("%d\n", ans);
    }
  }
  return 0;
}
Problem p02763 (C++)

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

typedef long long lli;

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  lli N;
  cin >> N;

  vector<char> S(N);

  set<lli> positions[26];

  for (lli i = 0; i < N; ++i) {
    cin >> S[i];
    positions[S[i] - 'a'].insert(i);
  }

  lli Q;
  cin >> Q;

  while (Q-- > 0) {
    lli t;
    cin >> t;

    if (t == 1) {
      lli i;
      char c;

      cin >> i >> c;
      --i;

      char old = S[i];
      S[i] = c;

      positions[old - 'a'].erase(i);
      positions[c - 'a'].insert(i);

    } else {
      /* Count stuff */
      lli l, r;
      cin >> l >> r;

      --l;
      --r;

      int in = 0;
      for (int i = 0; i < 26; ++i) {

        for (lli it : positions[i]) {
          if (it >= l and it <= r) {
            ++in;

            break;
          }
        }
      }

      cout << in << endl;
    }
  }

  return 0;
}


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

typedef long long lli;

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  lli N;
  cin >> N;

  vector<char> S(N);

  set<lli> positions[26];

  for (lli i = 0; i < N; ++i) {
    cin >> S[i];
    positions[S[i] - 'a'].insert(i);
  }

  lli Q;
  cin >> Q;

  while (Q-- > 0) {
    lli t;
    cin >> t;

    if (t == 1) {
      lli i;
      char c;

      cin >> i >> c;
      --i;

      char old = S[i];
      S[i] = c;

      positions[old - 'a'].erase(i);
      positions[c - 'a'].insert(i);

    } else {
      /* Count stuff */
      lli l, r;
      cin >> l >> r;

      --l;
      --r;

      int in = 0;
      for (int i = 0; i < 26; ++i) {
        auto it = positions[i].lower_bound(l);
        if (it != positions[i].end() && *it <= r)
          ++in;
      }

      cout << in << endl;
    }
  }

  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, i, j;
  string S;
  cin >> N >> S;
  vector<set<int>> al(26);
  for (i = 0; i < 26; i++) {
    al[i] = {N};
  }
  for (i = 0; i < N; i++) {
    al[S[i] - 'a'].insert(i);
  }
  int Q;
  cin >> Q;
  for (i = 0; i < Q; i++) {
    int a;
    cin >> a;
    if (a == 1) {
      int k;
      char c;
      cin >> k >> c;
      k--;
      if (S[k] != c) {
        al[S[k] - 'a'].erase(k);
        S[k] = c;
        al[c - 'a'].insert(k);
      }
    } else {
      int l, r, ans = 0;
      cin >> l >> r;
      l--;
      r--;
      for (j = 0; j < 26; j++) {
        if (*lower_bound(al[j].begin(), al[j].end(), l) <= r) {
          ans++;
        }
      }
      cout << ans << "\n";
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, i, j;
  string S;
  cin >> N >> S;
  vector<set<int>> al(26);
  for (i = 0; i < 26; i++) {
    al[i] = {N};
  }
  for (i = 0; i < N; i++) {
    al[S[i] - 'a'].insert(i);
  }
  int Q;
  cin >> Q;
  for (i = 0; i < Q; i++) {
    int a;
    cin >> a;
    if (a == 1) {
      int k;
      char c;
      cin >> k >> c;
      k--;
      if (S[k] != c) {
        al[S[k] - 'a'].erase(k);
        S[k] = c;
        al[c - 'a'].insert(k);
      }
    } else {
      int l, r, ans = 0;
      cin >> l >> r;
      l--;
      r--;
      for (j = 0; j < 26; j++) {
        if (*al[j].lower_bound(l) <= r) {
          ans++;
        }
      }
      cout << ans << "\n";
    }
  }
}
Problem p02763 (C++)

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

using namespace std;
typedef long long ll;

int main() {
  int N;
  string S;
  int Q;
  cin >> N >> S >> Q;
  vector<set<int>> sets(26);
  for (int i = 0; i < N; ++i) {
    char ch = S[i];
    sets[ch - 'a'].insert(i);
  }

  for (int iter = 0; iter < Q; ++iter) {
    int cmd;
    cin >> cmd;
    if (cmd == 1) {
      int i;
      char ch;
      cin >> i >> ch;
      --i;
      char old_ch = S[i];
      sets[old_ch - 'a'].erase(i);
      S[i] = ch;
      sets[ch - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      --r;
      int ans = 0;
      for (char ch = 'a'; ch <= 'z'; ++ch) {
        const set<int> &s = sets[ch - 'a'];
        auto it = lower_bound(s.begin(), s.end(), l);
        if (it != s.end() and *it <= r)
          ++ans;
      }
      cout << ans << endl;
    }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N;
  string S;
  int Q;
  cin >> N >> S >> Q;
  vector<set<int>> sets(26);
  for (int i = 0; i < N; ++i) {
    char ch = S[i];
    sets[ch - 'a'].insert(i);
  }

  for (int iter = 0; iter < Q; ++iter) {
    int cmd;
    cin >> cmd;
    if (cmd == 1) {
      int i;
      char ch;
      cin >> i >> ch;
      --i;
      char old_ch = S[i];
      sets[old_ch - 'a'].erase(i);
      S[i] = ch;
      sets[ch - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      --r;
      int ans = 0;
      for (char ch = 'a'; ch <= 'z'; ++ch) {
        const set<int> &s = sets[ch - 'a'];
        // std::lower_bound is O(N)!!
        // auto it = lower_bound(s.begin(), s.end(), l);
        // std::set::lower_bound is O(log N)
        auto it = s.lower_bound(l);
        if (it != s.end() and *it <= r)
          ++ans;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

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

typedef long long ll;
// const int INF = 1e8;
const ll INF = 1LL << 60;
typedef pair<int, int> P;

ll N, Q;
string S;

int main() {
  cin >> N >> S >> Q;
  vector<set<int>> is(26);
  rep(i, N) is[S[i] - 'a'].insert(i);

  rep(k, Q) {
    ll q;
    cin >> q;
    if (q == 1) {
      ll i;
      char c;
      cin >> i >> c;
      i--;
      is[S[i] - 'a'].erase(i);
      S[i] = c;
      is[S[i] - 'a'].insert(i);
    } else {
      ll l, r;
      cin >> l >> r;
      l--;

      // 文字の種類数を求める
      int ans = 0;
      rep(j, 26) {
        auto it = lower_bound(is[j].begin(), is[j].end(), l);
        if (it != is[j].end() && *it < r)
          ans++;
      }
      cout << ans << endl;
    }
  }
}

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

typedef long long ll;
// const int INF = 1e8;
const ll INF = 1LL << 60;
typedef pair<int, int> P;

ll N, Q;
string S;

int main() {
  cin >> N >> S >> Q;
  vector<set<int>> is(26);
  rep(i, N) is[S[i] - 'a'].insert(i);

  rep(k, Q) {
    ll q;
    cin >> q;
    if (q == 1) {
      ll i;
      char c;
      cin >> i >> c;
      i--;
      is[S[i] - 'a'].erase(i);
      S[i] = c;
      is[S[i] - 'a'].insert(i);
    } else {
      ll l, r;
      cin >> l >> r;
      l--;

      // 文字の種類数を求める
      int ans = 0;
      rep(j, 26) {
        // auto it = lower_bound(is[j].begin(), is[j].end(), l);
        auto it = is[j].lower_bound(l);
        if (it != is[j].end() && *it < r)
          ans++;
      }
      cout << ans << endl;
    }
  }
}
Problem p02763 (C++)

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

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

int main() {
  int n = ri();
  std::string s;
  std::cin >> s;
  for (auto &i : s)
    i -= 'a';

  int q = ri();
  std::set<int> pos[26];
  for (int i = 0; i < n; i++)
    pos[s[i]].insert(i);
  for (int i = 0; i < q; i++) {
    if (ri() == 1) {
      int index = ri() - 1;
      std::string s;
      std::cin >> s;
      int c = s[0] - 'a';
      pos[s[index]].erase(index);
      pos[s[index] = c].insert(index);
    } else {
      int l = ri() - 1;
      int r = ri();
      int cnt = 0;
      for (int j = 0; j < 26; j++)
        if (pos[j].lower_bound(l) != pos[j].lower_bound(r))
          cnt++;
      printf("%d\n", cnt);
    }
  }
  return 0;
}


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

int ri() {
  int n;
  scanf("%d", &n);
  return n;
}

int main() {
  int n = ri();
  std::string s;
  std::cin >> s;
  for (auto &i : s)
    i -= 'a';

  int q = ri();
  std::set<int> pos[26];
  for (int i = 0; i < n; i++)
    pos[s[i]].insert(i);
  for (int i = 0; i < q; i++) {
    if (ri() == 1) {
      int index = ri() - 1;
      std::string tmp;
      std::cin >> tmp;
      int c = tmp[0] - 'a';
      pos[s[index]].erase(index);
      pos[s[index] = c].insert(index);
    } else {
      int l = ri() - 1;
      int r = ri();
      int cnt = 0;
      for (int j = 0; j < 26; j++)
        if (pos[j].lower_bound(l) != pos[j].lower_bound(r))
          cnt++;
      printf("%d\n", cnt);
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n, Q;
char s[N];
class BIT {
public:
  vector<int> sum;
  void init(int SIZE) { sum.resize(SIZE, 0); }
  void ins(const int &x, const int &v) {
    for (int i = x; i <= n; i += i & -i)
      sum[i] += v;
  }
  int ask(const int &x) const {
    int ans = 0;
    for (int i = x; i; i &= i - 1)
      ans += sum[i];
    return ans;
  }
} bit[26];
int main() {
  cin >> n;
  scanf("%s", s + 1);
  for (int i = 0; i < 26; i++)
    bit[i].init(n);
  for (int i = 1; i <= n; i++)
    bit[s[i] - 'a'].ins(i, 1);
  scanf("%d", &Q);
  while (Q--) {
    int op;
    scanf("%d", &op);
    if (op == 1) {
      int x;
      char c;
      scanf("%d %c", &x, &c);
      if (s[x] == c)
        continue;
      bit[s[x] - 'a'].ins(x, -1);
      bit[c - 'a'].ins(x, 1);
      s[x] = c;
    } else {
      int l, r;
      scanf("%d%d", &l, &r);
      int ans = 0;
      for (int i = 0; i < 26; i++)
        ans += bit[i].ask(r) - bit[i].ask(l - 1) > 0;
      printf("%d\n", ans);
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int n, Q;
char s[N];
class BIT {
public:
  vector<int> sum;
  void init(int SIZE) { sum.resize(SIZE, 0); }
  void ins(const int &x, const int &v) {
    for (int i = x; i <= n; i += i & -i)
      sum[i] += v;
  }
  int ask(const int &x) const {
    int ans = 0;
    for (int i = x; i; i &= i - 1)
      ans += sum[i];
    return ans;
  }
} bit[26];
int main() {
  cin >> n;
  scanf("%s", s + 1);
  for (int i = 0; i < 26; i++)
    bit[i].init(n + 1);
  for (int i = 1; i <= n; i++)
    bit[s[i] - 'a'].ins(i, 1);
  scanf("%d", &Q);
  while (Q--) {
    int op;
    scanf("%d", &op);
    if (op == 1) {
      int x;
      char c;
      scanf("%d %c", &x, &c);
      if (s[x] == c)
        continue;
      bit[s[x] - 'a'].ins(x, -1);
      bit[c - 'a'].ins(x, 1);
      s[x] = c;
    } else {
      int l, r;
      scanf("%d%d", &l, &r);
      int ans = 0;
      for (int i = 0; i < 26; i++)
        ans += bit[i].ask(r) - bit[i].ask(l - 1) > 0;
      printf("%d\n", ans);
    }
  }
}
Problem p02763 (C++)

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

#define MOD (1000000007LL)

using namespace std;

const int denom = 100;

int cntt[500000 / denom][26];
string S;

bool hoge(int n, int l, int r) {
  int L = (l + denom) / denom;
  int R = (r + 1) / denom;
  // cout << L << " " << R << endl;
  for (int I = L; I < R; I++) {
    if (cntt[I][n] > 0)
      return true;
  }
  for (int i = l; i <= min(r, L * denom); i++) {
    // cout << "i: " << i << ", S[i]: " << S[i] << endl;
    if (S[i] == n + 'a')
      return true;
  }
  for (int i = max(l, R * denom); i <= r; i++) {
    if (S[i] == n + 'a')
      return true;
  }
  // cout << "false" << endl;
  return false;
}

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

  for (int i = 0; i < N; i++) {
    cntt[i / denom][S[i] - 'a']++;
  }

  for (int q = 0; q < Q; q++) {
    int t;
    cin >> t;
    if (t == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      cntt[i / denom][S[i] - 'a']--;
      cntt[i / denom][c - 'a']++;
      S[i] = c;
    }
    if (t == 2) {
      int ans = 0;
      int l, r;
      cin >> l >> r;
      for (int i = 0; i < 26; i++) {
        if (hoge(i, l - 1, r - 1))
          ans++;
      }
      cout << ans << endl;
    }
  }

  // cout << S << endl;
  return 0;
}


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

#define MOD (1000000007LL)

using namespace std;

const int denom = 1000;

int cntt[500000 / denom][26];
string S;

bool hoge(int n, int l, int r) {
  int L = (l + denom) / denom;
  int R = (r + 1) / denom;
  // cout << L << " " << R << endl;
  for (int I = L; I < R; I++) {
    if (cntt[I][n] > 0)
      return true;
  }
  for (int i = l; i <= min(r, L * denom); i++) {
    // cout << "i: " << i << ", S[i]: " << S[i] << endl;
    if (S[i] == n + 'a')
      return true;
  }
  for (int i = max(l, R * denom); i <= r; i++) {
    if (S[i] == n + 'a')
      return true;
  }
  // cout << "false" << endl;
  return false;
}

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

  for (int i = 0; i < N; i++) {
    cntt[i / denom][S[i] - 'a']++;
  }

  for (int q = 0; q < Q; q++) {
    int t;
    cin >> t;
    if (t == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      cntt[i / denom][S[i] - 'a']--;
      cntt[i / denom][c - 'a']++;
      S[i] = c;
    }
    if (t == 2) {
      int ans = 0;
      int l, r;
      cin >> l >> r;
      for (int i = 0; i < 26; i++) {
        if (hoge(i, l - 1, r - 1))
          ans++;
      }
      cout << ans << endl;
    }
  }

  // cout << S << endl;
  return 0;
}