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

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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  string str;
  cin >> str;
  int q;
  cin >> q;
  vector<set<int>> vec(26, set<int>());
  for (int i = 0; i < n; ++i) {
    vec[str[i] - 'a'].insert(i);
  }
  for (int i = 0; i < q; ++i) {
    int type;
    cin >> type;
    if (type == 1) {
      char c;
      int j;
      cin >> j >> c;
      int k = 0;
      for (k = 0; k < 26; ++k) {
        if (vec[k].find(j - 1) != vec[k].end())
          break;
      }
      vec[k].erase(j - 1);
      vec[c - 'a'].insert(j - 1);

    } else {
      int l, r;
      cin >> l >> r;
      int sum = 0;
      for (int k = 0; k < 26; ++k) {

        auto it1 = lower_bound(vec[k].begin(), vec[k].end(), l - 1);
        int index = *it1;
        if (it1 != vec[k].end())
          if (index <= (r - 1) && index >= (l - 1))
            sum++;
      }
      cout << sum << "\n";
    }
  }
}


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

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  string str;
  cin >> str;
  int q;
  cin >> q;
  vector<set<int>> vec(26, set<int>());
  for (int i = 0; i < n; ++i) {
    vec[str[i] - 'a'].insert(i);
  }
  for (int i = 0; i < q; ++i) {
    int type;
    cin >> type;
    if (type == 1) {
      char c;
      int j;
      cin >> j >> c;
      int k = 0;
      for (k = 0; k < 26; ++k) {
        if (vec[k].find(j - 1) != vec[k].end())
          break;
      }
      vec[k].erase(j - 1);
      vec[c - 'a'].insert(j - 1);

    } else {
      int l, r;
      cin >> l >> r;
      int sum = 0;
      for (int k = 0; k < 26; ++k) {

        auto it1 = vec[k].lower_bound(l - 1);
        int index = *it1;
        if (it1 != vec[k].end())
          if (index <= (r - 1) && index >= (l - 1))
            sum++;
      }
      cout << sum << "\n";
    }
  }
}
Problem p02763 (C++)

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

const int MAXN = 1e5;

int n;
string s;
int seg[4 * MAXN + 20];
int q;

void build(int pos, int l, int r) {
  if (l == r) {
    seg[pos] = 1 << (s[l] - 'a');
  } else {
    build(pos << 1, l, (l + r) >> 1);
    build((pos << 1) + 1, ((l + r) >> 1) + 1, r);
    seg[pos] = seg[pos << 1] | seg[(pos << 1) + 1];
  }
}

void upd(int pos, int l, int r, int tar) {
  if (l == r) {
    seg[pos] = 1 << (s[l] - 'a');
  } else {
    if (tar <= ((l + r) >> 1)) {
      upd(pos << 1, l, ((l + r) >> 1), tar);
    } else {
      upd((pos << 1) + 1, ((l + r) >> 1) + 1, r, tar);
    }
    seg[pos] = seg[pos << 1] | seg[(pos << 1) + 1];
  }
}

int que(int pos, int l, int r, int ql, int qr) {
  if (ql > qr)
    return 0;
  if (l == ql && r == qr) {
    return seg[pos];
  } else {
    int m = (l + r) >> 1;
    int a = que(pos << 1, l, m, ql, min(m, qr));
    int b = que((pos << 1) + 1, m + 1, r, max(ql, m + 1), qr);
    return a | b;
  }
}

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

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

  build(1, 0, n - 1);

  while (q--) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      s[i - 1] = c;
      upd(1, 0, n - 1, i - 1);
    } else if (type == 2) {
      int l, r;
      cin >> l >> r;
      cout << __builtin_popcount(que(1, 0, n - 1, l - 1, r - 1)) << "\n";
    }
  }
}


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

const int MAXN = 5e5;

int n;
string s;
int seg[4 * MAXN + 20];
int q;

void build(int pos, int l, int r) {
  if (l == r) {
    seg[pos] = 1 << (s[l] - 'a');
  } else {
    build(pos << 1, l, (l + r) >> 1);
    build((pos << 1) + 1, ((l + r) >> 1) + 1, r);
    seg[pos] = seg[pos << 1] | seg[(pos << 1) + 1];
  }
}

void upd(int pos, int l, int r, int tar) {
  if (l == r) {
    seg[pos] = 1 << (s[l] - 'a');
  } else {
    if (tar <= ((l + r) >> 1)) {
      upd(pos << 1, l, ((l + r) >> 1), tar);
    } else {
      upd((pos << 1) + 1, ((l + r) >> 1) + 1, r, tar);
    }
    seg[pos] = seg[pos << 1] | seg[(pos << 1) + 1];
  }
}

int que(int pos, int l, int r, int ql, int qr) {
  if (ql > qr)
    return 0;
  if (l == ql && r == qr) {
    return seg[pos];
  } else {
    int m = (l + r) >> 1;
    int a = que(pos << 1, l, m, ql, min(m, qr));
    int b = que((pos << 1) + 1, m + 1, r, max(ql, m + 1), qr);
    return a | b;
  }
}

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

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

  build(1, 0, n - 1);

  while (q--) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      s[i - 1] = c;
      upd(1, 0, n - 1, i - 1);
    } else if (type == 2) {
      int l, r;
      cin >> l >> r;
      cout << __builtin_popcount(que(1, 0, n - 1, l - 1, r - 1)) << "\n";
    }
  }
}
Problem p02763 (C++)

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

#define mt make_tuple
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sqr(x) ((ll)(x) * (x))

int main() {
  string s;
  cin >> s;

  vector<set<int>> v(26);

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

  int q;
  cin >> q;

  while (q--) {
    int instrukcija;
    cin >> instrukcija;

    if (instrukcija == 1) {
      int pos;
      char c;
      cin >> pos >> c;
      --pos;
      v[s[pos] - 'a'].erase(pos);
      s[pos] = c;
      v[c - 'a'].insert(pos);
    } else {
      int l, r;
      cin >> l >> r;
      --l, --r;
      int odgovor = 0;
      for (int i = 0; i < 26; ++i) {
        auto it = v[i].lower_bound(l);
        if (it != v[i].end() && *it <= r) {
          ++odgovor;
        }
      }
      cout << odgovor << '\n';
    }
  }

  return 0;
}

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

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;
typedef vector<vpii> vvpii;

#define mt make_tuple
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sqr(x) ((ll)(x) * (x))

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;

  vector<set<int>> v(26);

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

  int q;
  cin >> q;

  while (q--) {
    int instrukcija;
    cin >> instrukcija;

    if (instrukcija == 1) {
      int pos;
      char c;
      cin >> pos >> c;
      --pos;
      v[s[pos] - 'a'].erase(pos);
      s[pos] = c;
      v[c - 'a'].insert(pos);
    } else {
      int l, r;
      cin >> l >> r;
      --l, --r;
      int odgovor = 0;
      for (int i = 0; i < 26; ++i) {
        auto it = v[i].lower_bound(l);
        if (it != v[i].end() && *it <= r) {
          ++odgovor;
        }
      }
      cout << odgovor << '\n';
    }
  }

  return 0;
}
Problem p02763 (C++)

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

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

  for (int i = 0; i < q; ++i) {
    int kind;
    cin >> kind;
    if (kind == 1) {
      int iq;
      char cq, charbef;
      cin >> iq >> cq;
      --iq;
      charbef = s[iq];
      s[iq] = cq;
      sevec[charbef - 'a'].erase(iq);
      sevec[cq - 'a'].insert(iq);
    } else {
      int lq, rq;
      cin >> lq >> rq;
      --lq;
      --rq;
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        auto ite = lower_bound(sevec[i].begin(), sevec[i].end(), lq);
        if (ite != sevec[i].end()) {
          int val = *ite;
          if (val <= rq)
            ++ans;
        }
      }
      cout << ans << endl;
    }
  }
}

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

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

  for (int i = 0; i < q; ++i) {
    int kind;
    cin >> kind;
    if (kind == 1) {
      int iq;
      char cq, charbef;
      cin >> iq >> cq;
      --iq;
      charbef = s[iq];
      s[iq] = cq;
      sevec[charbef - 'a'].erase(iq);
      sevec[cq - 'a'].insert(iq);
    } else {
      int lq, rq;
      cin >> lq >> rq;
      --lq;
      --rq;
      int ans = 0;
      for (int i = 0; i < 26; ++i) {
        auto ite = sevec[i].lower_bound(lq);
        if (ite != sevec[i].end()) {
          int val = *ite;
          if (val <= rq)
            ++ans;
        }
      }
      cout << ans << endl;
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<set<int>> vs(26);
  rep(i, n) {
    char now = s[i] - 'a';
    vs.at(now).insert(i);
  }
  int q;
  cin >> q;
  rep(i, q) {
    int qq;
    cin >> qq;
    if (qq == 1) {
      int j;
      cin >> j;
      j--;
      char c;
      cin >> c;
      rep(k, 26) {
        if (vs.at(k).count(j) == 1)
          vs.at(k).erase(j);
      }
      vs.at(c - 'a').insert(j);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(j, 26) {
        auto itr = lower_bound(vs.at(j).begin(), vs.at(j).end(), l);
        if (itr == vs.at(j).end() || r < *itr)
          continue;
        ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(n) begin(n), end(n)
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<set<int>> vs(26);
  rep(i, n) {
    char now = s[i] - 'a';
    vs.at(now).insert(i);
  }
  int q;
  cin >> q;
  rep(i, q) {
    int qq;
    cin >> qq;
    if (qq == 1) {
      int j;
      cin >> j;
      j--;
      char c;
      cin >> c;
      rep(k, 26) {
        if (vs.at(k).count(j) == 1)
          vs.at(k).erase(j);
      }
      vs.at(c - 'a').insert(j);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      rep(j, 26) {
        auto itr = vs.at(j).lower_bound(l);
        if (itr == vs.at(j).end() || r < *itr)
          continue;
        ans++;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int i, j;
  int q;
  cin >> q;
  vector<set<int>> sets(26);
  for (i = 0; i < n; i++) {
    sets.at(s.at(i) - 'a').insert(i);
  }
  for (i = 0; i < q; i++) {
    int num;
    cin >> num;
    if (num == 1) {
      int ind;
      char c;
      cin >> ind >> c;
      for (j = 0; j < 26; j++)
        sets.at(j).erase(ind - 1);
      sets.at(c - 'a').insert(ind - 1);
    } else if (num == 2) {
      int l, r;
      cin >> l >> r;
      int kind = 0;
      for (j = 0; j < 26; j++) {
        auto it = lower_bound(sets.at(j).begin(), sets.at(j).end(), l - 1);
        if (it != sets.at(j).end() && *it <= r - 1)
          kind++;
      }
      cout << kind << endl;
    }
  }
}


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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
vector<set<int>> alpha(26);
string S;
/* function */
void op1(int i, char c) {
  alpha[S[i] - 'a'].erase(i);
  S[i] = c;
  alpha[S[i] - 'a'].insert(i);
}
void op2(int l, int r) {
  int cnt = 0;
  for (int i = 0; i < 26; i++) {
    auto itr = lower_bound(alpha[i].begin(), alpha[i].end(), l);
    if (itr != alpha[i].end() && r >= *itr)
      cnt++;
  }
  cout << cnt << '\n';
}
/* main */
int main() {
  int N, Q;
  cin >> N >> S >> Q;

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

  int op, l, r, ind;
  char c;
  for (int i = 0; i < Q; i++) {
    cin >> op;
    if (op == 2) {
      cin >> l >> r;
      l--, r--;
      op2(l, r);
    } else {
      cin >> ind >> c;
      ind--;
      op1(ind, c);
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 10000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* global variables */
vector<set<int>> alpha(26);
string S;
/* function */
void op1(int i, char c) {
  alpha[S[i] - 'a'].erase(i);
  S[i] = c;
  alpha[S[i] - 'a'].insert(i);
}
void op2(int l, int r) {
  int cnt = 0;
  for (int i = 0; i < 26; i++) {
    auto itr = alpha[i].lower_bound(l);
    if (itr != alpha[i].end() && r >= *itr)
      cnt++;
  }
  cout << cnt << '\n';
}
/* main */
int main() {
  int N, Q;
  cin >> N >> S >> Q;

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

  int op, l, r, ind;
  char c;
  for (int i = 0; i < Q; i++) {
    cin >> op;
    if (op == 2) {
      cin >> l >> r;
      l--, r--;
      op2(l, r);
    } else {
      cin >> ind >> c;
      ind--;
      op1(ind, c);
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 500000
set<int> s[26];
signed main() {
  int n;
  cin >> n;
  for (int i = 0; i <= 26; i++)
    s[i].insert(n);
  string str;
  cin >> str;
  for (int i = 0; i < str.size(); i++)
    s[str[i] - 'a'].insert(i);
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int op;
    cin >> op;
    if (op == 1) {
      int pos;
      char ch;
      cin >> pos >> ch;
      pos--;
      if (ch == str[pos])
        continue;
      s[str[pos] - 'a'].erase(pos);
      str[pos] = ch;
      s[ch - 'a'].insert(pos);
    } else {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      l--;
      r--;
      for (int j = 0; j < 26; j++) {
        int pos = *s[j].lower_bound(l);
        if (pos <= r)
          ans++;
      }
      cout << ans << '\n';
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define N 500000
set<int> s[26];
signed main() {
  int n;
  cin >> n;
  for (int i = 0; i < 26; i++)
    s[i].insert(n);
  string str;
  cin >> str;
  for (int i = 0; i < str.size(); i++)
    s[str[i] - 'a'].insert(i);
  int m;
  cin >> m;
  for (int i = 0; i < m; i++) {
    int op;
    cin >> op;
    if (op == 1) {
      int pos;
      char ch;
      cin >> pos >> ch;
      pos--;
      if (ch == str[pos])
        continue;
      s[str[pos] - 'a'].erase(pos);
      str[pos] = ch;
      s[ch - 'a'].insert(pos);
    } else {
      int l, r;
      cin >> l >> r;
      int ans = 0;
      l--;
      r--;
      for (int j = 0; j < 26; j++) {
        int pos = *s[j].lower_bound(l);
        if (pos <= r)
          ans++;
      }
      cout << ans << '\n';
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
void query1(const int &p, const char &c, vector<set<int>> &a) {
  for (int i = 0; i < 26; i++) {
    if (a[i].count(p)) {
      a[i].erase(p);
      break;
    }
  }
  a[c - 'a'].insert(p);
}
void query2(const int &l, const int &r, const vector<set<int>> &a) {
  int cnt = 0;
  for (int i = 0; i < 26; i++) {
    if (lower_bound(a[i].begin(), a[i].end(), l) !=
        upper_bound(a[i].begin(), a[i].end(), r))
      cnt++;
  }
  printf("%d\n", cnt);
}
int main() {
  int n;
  scanf("%d", &n);
  char s[n];
  scanf("%s", s);
  vector<set<int>> a(26);
  for (int i = 0; i < n; i++)
    a[s[i] - 'a'].insert(i);
  int q;
  scanf("%d", &q);
  while (q--) {
    int p;
    scanf("%d", &p);
    if (p == 1) {
      int i;
      char c;
      scanf("%d %c", &i, &c);
      query1(i - 1, c, a);
    } else {
      int l, r;
      scanf("%d%d", &l, &r);
      query2(l - 1, r - 1, a);
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
void query1(const int &p, const char &c, vector<set<int>> &a) {
  for (int i = 0; i < 26; i++) {
    if (a[i].count(p)) {
      a[i].erase(p);
      break;
    }
  }
  a[c - 'a'].insert(p);
}
void query2(const int &l, const int &r, const vector<set<int>> &a) {
  int cnt = 0;
  for (int i = 0; i < 26; i++) {
    if (a[i].lower_bound(l) != a[i].upper_bound(r))
      cnt++;
  }
  printf("%d\n", cnt);
}
int main() {
  int n;
  scanf("%d", &n);
  char s[n];
  scanf("%s", s);
  vector<set<int>> a(26);
  for (int i = 0; i < n; i++)
    a[s[i] - 'a'].insert(i);
  int q;
  scanf("%d", &q);
  while (q--) {
    int p;
    scanf("%d", &p);
    if (p == 1) {
      int i;
      char c;
      scanf("%d %c", &i, &c);
      query1(i - 1, c, a);
    } else {
      int l, r;
      scanf("%d%d", &l, &r);
      query2(l - 1, r - 1, a);
    }
  }
  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())
#define show(x)                                                                \
  {                                                                            \
    for (auto i : x) {                                                         \
      cout << i << " ";                                                        \
    }                                                                          \
    cout << endl;                                                              \
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  // alphabetごとにsetで管理、lower_bound
  int N, Q;
  string S;
  cin >> N >> S >> Q;
  vector<set<int>> alpha(26);
  rep(i, N) alpha[S[i] - 'a'].insert(i);
  rep(_, Q) {
    int q;
    cin >> q;
    if (q == 1) {
      int i;
      char c;
      cin >> i >> c;
      --i;
      if (S[i] == c)
        continue;
      alpha[S[i] - 'a'].erase(i);
      alpha[c - 'a'].insert(i);
      S[i] = c;
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      int cnt = 0;
      rep(j, 26) {
        auto itr = lower_bound(alpha[j].begin(), alpha[j].end(), l);
        if (itr != alpha[j].end() && *itr < r)
          cnt++;
      }
      printf("%d\n", cnt);
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \
  {                                                                            \
    for (auto i : x) {                                                         \
      cout << i << " ";                                                        \
    }                                                                          \
    cout << endl;                                                              \
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  // alphabetごとにsetで管理、lower_bound
  int N, Q;
  string S;
  cin >> N >> S >> Q;
  vector<set<int>> alpha(26);
  rep(i, N) alpha[S[i] - 'a'].insert(i);
  rep(_, Q) {
    int q;
    cin >> q;
    if (q == 1) {
      int i;
      char c;
      cin >> i >> c;
      --i;
      if (S[i] == c)
        continue;
      alpha[S[i] - 'a'].erase(i);
      alpha[c - 'a'].insert(i);
      S[i] = c;
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      int cnt = 0;
      rep(j, 26) {
        auto itr = alpha[j].lower_bound(l);
        if (itr != alpha[j].end() && *itr < r)
          cnt++;
      }
      printf("%d\n", cnt);
    }
  }
  return 0;
}
Problem p02763 (C++)

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

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


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

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int q;
  cin >> q;
  vector<set<int>> is(26);
  rep(i, n) { is[s[i] - 'a'].insert(i); }
  rep(qi, q) {
    int ty;
    cin >> ty;
    if (ty == 1) {
      int i;
      char c;
      cin >> i >> c;
      --i;
      is[s[i] - 'a'].erase(i);
      s[i] = c;
      is[s[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      --l;
      int ans = 0;
      rep(i, 26) {
        auto it = is[i].lower_bound(l);
        if (it != is[i].end() && *it < r)
          ++ans;
      }
      printf("%d\n", ans);
    }
  }
  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 ll long long
#define P pair<int, int>
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int MOD = 1000000007;
const int INF = 2002002002;
const ll LLINF = 9009009009009009009;
using namespace std;

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

  vector<set<int>> alp(26);
  rep(i, n) alp[s[i] - 'a'].insert(i);

  int type, ind, l, r;
  char c;
  vector<int> ans;
  rep(i, q) {
    cin >> type;
    if (type == 1) {
      cin >> ind >> c;
      ind--;
      alp[s[ind] - 'a'].erase(ind);
      alp[c - 'a'].insert(ind);
      s[ind] = c;
    } else {
      cin >> l >> r;
      l--;
      r--;
      int ans_tmp = 0;
      rep(j, 26) {
        auto lb = lower_bound(alp[j].begin(), alp[j].end(), l);
        if (*lb > r || lb == alp[j].end())
          continue;
        ans_tmp++;
      }
      ans.push_back(ans_tmp);
    }
  }
  for (int a : ans)
    cout << a << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ll long long
#define P pair<int, int>
#define fast_io                                                                \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
const int MOD = 1000000007;
const int INF = 2002002002;
const ll LLINF = 9009009009009009009;
using namespace std;

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

  vector<set<int>> alp(26);
  rep(i, n) alp[s[i] - 'a'].insert(i);

  int type, ind, l, r;
  char c;
  vector<int> ans;
  rep(i, q) {
    cin >> type;
    if (type == 1) {
      cin >> ind >> c;
      ind--;
      alp[s[ind] - 'a'].erase(ind);
      alp[c - 'a'].insert(ind);
      s[ind] = c;
    } else {
      cin >> l >> r;
      l--;
      r--;
      int ans_tmp = 0;
      rep(j, 26) {
        auto lb = alp[j].lower_bound(l);
        if (*lb > r || lb == alp[j].end())
          continue;
        ans_tmp++;
      }
      ans.push_back(ans_tmp);
    }
  }
  for (int a : ans)
    cout << a << endl;
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Runtime Error):
// review
#include <algorithm>
#include <iostream>
#include <set>

using namespace std;
typedef long long ll;

struct Query {
  ll type, i, l, r;
  char c;
};

const int MAXQ = 20010;

ll n;
ll q;
string s;

Query queries[MAXQ];

void solve() {
  set<int> positions[26];
  for (int i = 0; i < n; i++) {
    positions[s[i] - 'a'].insert(i + 1);
  }
  for (int i = 0; i < q; i++) {
    Query &query = queries[i];
    if (query.type == 1) {
      positions[s[i] - 'a'].erase(query.i);
      positions[query.c - 'a'].insert(query.i);
      s[query.i - 1] = query.c;
    } else {
      int cnt = 0;
      for (int j = 0; j < 26; j++) {
        auto iter = positions[j].lower_bound(query.l);
        if (iter != positions[j].end() && *iter <= query.r) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }
}

int main() {
  cin >> n >> s >> q;
  for (int i = 0; i < q; i++) {
    Query &query = queries[i];
    cin >> query.type;
    if (query.type == 1) {
      cin >> query.i >> query.c;
    } else {
      cin >> query.l >> query.r;
    }
  }
  solve();
}


Fixed submission:
// review
#include <algorithm>
#include <iostream>
#include <set>

using namespace std;
typedef long long ll;

struct Query {
  ll type, i, l, r;
  char c;
};

const int MAXQ = 20010;

ll n;
ll q;
string s;

Query queries[MAXQ];

void solve() {
  set<int> positions[26];
  for (int i = 0; i < n; i++) {
    positions[s[i] - 'a'].insert(i + 1);
  }
  for (int i = 0; i < q; i++) {
    Query &query = queries[i];
    if (query.type == 1) {
      positions[s[query.i - 1] - 'a'].erase(query.i);
      positions[query.c - 'a'].insert(query.i);
      s[query.i - 1] = query.c;
    } else {
      int cnt = 0;
      for (int j = 0; j < 26; j++) {
        auto iter = positions[j].lower_bound(query.l);
        if (iter != positions[j].end() && *iter <= query.r) {
          cnt++;
        }
      }
      cout << cnt << endl;
    }
  }
}

int main() {
  cin >> n >> s >> q;
  for (int i = 0; i < q; i++) {
    Query &query = queries[i];
    cin >> query.type;
    if (query.type == 1) {
      cin >> query.i >> query.c;
    } else {
      cin >> query.l >> query.r;
    }
  }
  solve();
}
Problem p02763 (C++)

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

int main() {
  Int N, Q;
  string s;
  cin >> N >> s >> Q;

  set<Int> S[26];

  rep(i, N) {
    rep(j, 26) {
      if (s[i] == 'a' + j)
        S[j].insert(i);
      S[j].insert(N);
    }
  }

  rep(k, Q) {
    int a;
    cin >> a;
    if (a == 1) {
      Int i;
      char c;
      cin >> i >> c;
      i--;
      rep(j, 26) {
        if (S[j].find(i) == S[j].end()) {
        } else
          S[j].erase(i);
      }
      S[c - 'a'].insert(i);
    } else if (a == 2) {
      Int l, r;
      cin >> l >> r;
      l--;
      r--;
      int cnt = 0;
      rep(j, 26) {
        if (*S[j].lower_bound(l) <= r)
          cnt++;
      }
      cout << cnt << endl;
    }
  }
  return 0;
}

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

int main() {
  Int N, Q;
  string s;
  cin >> N >> s >> Q;

  set<Int> S[26];

  rep(i, N) { S[s[i] - 'a'].insert(i); }
  rep(i, 26) S[i].insert(N);

  rep(k, Q) {
    int a;
    cin >> a;
    if (a == 1) {
      Int i;
      char c;
      cin >> i >> c;
      i--;
      rep(j, 26) {
        if (S[j].find(i) == S[j].end()) {
        } else
          S[j].erase(i);
      }
      S[c - 'a'].insert(i);
    } else if (a == 2) {
      Int l, r;
      cin >> l >> r;
      l--;
      r--;
      int cnt = 0;
      rep(j, 26) {
        if (*S[j].lower_bound(l) <= r)
          cnt++;
      }
      cout << cnt << 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 REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e15;

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll q;
  cin >> q;
  vector<set<ll>> st(26);
  REP(i, 26) st[i].insert(INF);

  REP(i, n) { st[s[i] - 'a'].insert(i); }

  while (q--) {
    ll qn;
    cin >> qn;
    if (qn == 1) {
      ll i;
      char c;
      cin >> i >> c;
      --i;
      st[s[i] - 'a'].erase(i);
      s[i] = c;
      st[s[i] - 'a'].insert(i);
    } else {
      ll ans = 0;
      ll l, r;
      cin >> l >> r;
      --l, --r;
      REP(k, 26) {
        auto itl = lower_bound(ALL(st[k]), l);
        if (*itl == INF)
          continue;

        if (*itl <= r)
          ++ans;
      }
      cout << ans << endl;
    }
  }
  return 0;
}

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

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define FORR(i, m, n) for (int i = m; i >= n; i--)
#define ALL(x) (x).begin(), (x).end()

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 1e15;

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll q;
  cin >> q;
  vector<set<ll>> st(26);
  REP(i, 26) st[i].insert(INF);

  REP(i, n) { st[s[i] - 'a'].insert(i); }

  while (q--) {
    ll qn;
    cin >> qn;
    if (qn == 1) {
      ll i;
      char c;
      cin >> i >> c;
      --i;
      st[s[i] - 'a'].erase(i);
      s[i] = c;
      st[s[i] - 'a'].insert(i);
    } else {
      ll ans = 0;
      ll l, r;
      cin >> l >> r;
      --l, --r;
      REP(k, 26) {
        auto itl = st[k].lower_bound(l);
        if (*itl <= r)
          ++ans;
      }
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int main() {
  int N, Q;
  string S;
  cin >> N >> S >> Q;
  set<int> st[26];
  for (int i = 0; i < N; i++) {
    st[S[i] - 'a'].insert(i);
  }
  for (int i = 0; i < 26; i++) {
    st[i].insert(N);
  }
  while (Q--) {
    int q;
    cin >> q;
    if (q == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      st[S[i] - 'a'].erase(i);
      S[i] = c;
      st[S[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        auto itr = lower_bound(st[i].begin(), st[i].end(), l);
        int num = *itr;
        if (num <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int main() {
  int N, Q;
  string S;
  cin >> N >> S >> Q;
  set<int> st[26];
  for (int i = 0; i < N; i++) {
    st[S[i] - 'a'].insert(i);
  }
  for (int i = 0; i < 26; i++) {
    st[i].insert(N);
  }
  while (Q--) {
    int q;
    cin >> q;
    if (q == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      st[S[i] - 'a'].erase(i);
      S[i] = c;
      st[S[i] - 'a'].insert(i);
    } else {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int ans = 0;
      for (int i = 0; i < 26; i++) {
        auto itr = st[i].lower_bound(l);
        int num = *itr;
        if (num <= r)
          ans++;
      }
      cout << ans << endl;
    }
  }
}
Problem p02763 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <set>
#include <string>

using namespace std;

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

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

  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      int p;
      char c;
      cin >> p >> c;

      st[s[p - 1] - 'a'].erase(p);
      s[p - 1] = c;
      st[c - 'a'].insert(p);
    } else {
      int l, r;
      cin >> l >> r;

      int ans = 0;
      for (int i = 0; i < 26; i++) {
        auto it = lower_bound(st[i].begin(), st[i].end(), l);
        if (it != st[i].end() && *it <= r) {
          ans++;
        }
      }
      printf("%d\n", ans);
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <set>
#include <string>

using namespace std;

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

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

  for (int i = 0; i < q; i++) {
    int t;
    cin >> t;
    if (t == 1) {
      int p;
      char c;
      cin >> p >> c;

      st[s[p - 1] - 'a'].erase(p);
      s[p - 1] = c;
      st[c - 'a'].insert(p);
    } else {
      int l, r;
      cin >> l >> r;

      int ans = 0;
      for (int i = 0; i < 26; i++) {
        auto it = st[i].lower_bound(l);
        if (it != st[i].end() && *it <= r) {
          ans++;
        }
      }
      printf("%d\n", ans);
    }
  }

  return 0;
}
Problem p02763 (C++)

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

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

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

  while (q-- > 0) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      pos[s[i]].erase(i);
      pos[c - 'a'].insert(i);
      s[i] = c;
    }
    if (type == 2) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int kind = 0;
      for (int i = 0; i < 26; i++) {
        set<int>::iterator it = pos[i].lower_bound(l);
        set<int>::iterator last = pos[i].upper_bound(n);
        if (it != last && *it <= r) {
          kind++;
        }
      }
      cout << kind << endl;
    }
  }

  return 0;
}


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

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

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

  while (q-- > 0) {
    int type;
    cin >> type;
    if (type == 1) {
      int i;
      char c;
      cin >> i >> c;
      i--;
      pos[s[i] - 'a'].erase(i);
      pos[c - 'a'].insert(i);
      s[i] = c;
    }
    if (type == 2) {
      int l, r;
      cin >> l >> r;
      l--;
      r--;
      int kind = 0;
      for (int i = 0; i < 26; i++) {
        set<int>::iterator it = pos[i].lower_bound(l);
        set<int>::iterator last = pos[i].upper_bound(n);
        if (it != last && *it <= r) {
          kind++;
        }
      }
      cout << kind << endl;
    }
  }

  return 0;
}
Problem p02763 (C++)

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

void solve() {
  ll n, q;
  string s;
  cin >> n >> s >> q;
  vector<set<ll>> st(26);
  for (int i = 0; i < n; i++) {
    int x = s[i] - 'a';
    st[x].insert(i);
  }
  cout << endl;
  for (int i = 0; i < q; i++) {
    int x;
    cin >> x;
    if (x == 1) {
      int y;
      char c1, c2;
      cin >> y >> c2;
      c1 = s[y - 1];
      st[c1 - 'a'].erase(y - 1);
      st[c2 - 'a'].insert(y - 1);
      s[y - 1] = c2;
    } else {
      int l, r, sum = 0;
      cin >> l >> r;
      for (int j = 0; j < 26; j++) {
        if (lower_bound(st[j].begin(), st[j].end(), l - 1) != st[j].end()) {
          int y = *lower_bound(st[j].begin(), st[j].end(), l - 1);
          if (y <= r - 1)
            sum++;
        }
      }
      cout << sum << endl;
    }
  }

  return;
}

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


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long ll;

void solve() {
  ll n, q;
  string s;
  cin >> n >> s >> q;
  vector<set<ll>> st(26);
  for (int i = 0; i < n; i++) {
    int x = s[i] - 'a';
    st[x].insert(i);
  }
  cout << endl;
  for (int i = 0; i < q; i++) {
    int x;
    cin >> x;
    if (x == 1) {
      int y;
      char c1, c2;
      cin >> y >> c2;
      c1 = s[y - 1];
      st[c1 - 'a'].erase(y - 1);
      st[c2 - 'a'].insert(y - 1);
      s[y - 1] = c2;
    } else {
      int l, r, sum = 0;
      cin >> l >> r;
      for (int j = 0; j < 26; j++) {
        if (st[j].lower_bound(l - 1) != st[j].end()) {
          int y = *(st[j].lower_bound(l - 1));
          if (y <= r - 1)
            sum++;
        }
      }
      cout << sum << endl;
    }
  }

  return;
}

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int seg(int l, int r, int n, int nl, int nr, vector<int> &t) {
  if (r <= nl || nr <= l)
    return 0;
  else if (l <= nl && nr <= r)
    return t[n];
  else {
    int mid = (nl + nr) / 2;
    return seg(l, r, n * 2, nl, mid, t) + seg(l, r, n * 2 + 1, mid, nr, t);
  }
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int N;
  for (int i = 2; i < n; i *= 2)
    N = i;
  N *= 2;
  vector<vector<int>> t(26, vector<int>(N * 2, 0));
  for (int i = 0; i < n; i++)
    t[s[i] - 'a'][i + N]++;
  for (int i = 0; i < 26; i++) {
    for (int j = N - 1; j > 0; j--)
      t[i][j] = t[i][j * 2] + t[i][j * 2 + 1];
  }
  int q;
  cin >> q;
  vector<int> x;
  for (int i = 0; i < q; i++) {
    int type, a;
    cin >> type >> a;
    if (type == 1) {
      char b;
      cin >> b;
      for (int j = N + a - 1; j > 0; j /= 2) {
        t[b - 'a'][j]++;
        t[s[a - 1] - 'a'][j]--;
      }
      s[a - 1] = b;
    }
    if (type == 2) {
      int b;
      cin >> b;
      int kind = 0;
      for (int j = 0; j < 26; j++) {
        if (seg(a - 1, b, 1, 0, N, t[j]))
          kind++;
      }
      x.push_back(kind);
    }
  }
  for (int i = 0; i < x.size(); i++)
    cout << x[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int seg(int l, int r, int n, int nl, int nr, vector<int> &t) {
  if (r <= nl || nr <= l)
    return 0;
  else if (l <= nl && nr <= r)
    return t[n];
  else {
    int mid = (nl + nr) / 2;
    return seg(l, r, n * 2, nl, mid, t) + seg(l, r, n * 2 + 1, mid, nr, t);
  }
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int N = 1;
  for (int i = 2; i < n; i *= 2)
    N = i;
  N *= 2;
  vector<vector<int>> t(26, vector<int>(N * 2, 0));
  for (int i = 0; i < n; i++)
    t[s[i] - 'a'][i + N]++;
  for (int i = 0; i < 26; i++) {
    for (int j = N - 1; j > 0; j--)
      t[i][j] = t[i][j * 2] + t[i][j * 2 + 1];
  }
  int q;
  cin >> q;
  vector<int> x;
  for (int i = 0; i < q; i++) {
    int type, a;
    cin >> type >> a;
    if (type == 1) {
      char b;
      cin >> b;
      for (int j = N + a - 1; j > 0; j /= 2) {
        t[b - 'a'][j]++;
        t[s[a - 1] - 'a'][j]--;
      }
      s[a - 1] = b;
    }
    if (type == 2) {
      int b;
      cin >> b;
      int kind = 0;
      for (int j = 0; j < 26; j++) {
        if (seg(a - 1, b, 1, 0, N, t[j]))
          kind++;
      }
      x.push_back(kind);
    }
  }
  for (int i = 0; i < x.size(); i++)
    cout << x[i] << endl;
  return 0;
}
Problem p02764 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>

using namespace std;

struct Node {
  double x, y, r;

  Node(double x, double y, double r) : x(x), y(y), r(r) {}
};

int n, k;
vector<int> x, y, c;

double distance(double px, double py) {
  vector<double> dis(n);
  for (int i = 0; i < n; ++i) {
    dis[i] = c[i] * hypot(px - x[i], py - y[i]);
  }
  nth_element(dis.begin(), dis.begin() + k - 1, dis.end());
  return dis[k - 1];
}

double distance_lb(double px, double py, double r) {
  vector<double> dis(n);
  for (int i = 0; i < n; ++i) {
    dis[i] = c[i] *
             hypot(max(abs(px - x[i]) - r, 0.0), max(abs(py - y[i]) - r, 0.0));
  }
  nth_element(dis.begin(), dis.begin() + k - 1, dis.end());
  return dis[k - 1];
}

int main() {
  cin >> n >> k;
  x.resize(n);
  y.resize(n);
  c.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> c[i];
  }
  queue<Node> que;
  double res = distance(0, 0);
  que.emplace(0, 0, 1000);
  while (!que.empty()) {
    auto node = que.front();
    que.pop();
    for (double dx : {-0.5, 0.5}) {
      for (double dy : {-0.5, 0.5}) {
        double nx = node.x + dx * node.r, ny = node.y + dy * node.r,
               nr = node.r / 2;
        double dis_lb = distance_lb(nx, ny, nr);
        if (res - dis_lb > 5e-7) {
          res = min(res, distance(nx, ny));
          que.emplace(nx, ny, nr);
        }
      }
    }
  }
  cout << fixed << setprecision(15) << res << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <queue>

using namespace std;

struct Node {
  double x, y, r;

  Node(double x, double y, double r) : x(x), y(y), r(r) {}
};

int n, k;
vector<int> x, y, c;

double distance(double px, double py) {
  vector<double> dis(n);
  for (int i = 0; i < n; ++i) {
    dis[i] = c[i] * hypot(px - x[i], py - y[i]);
  }
  nth_element(dis.begin(), dis.begin() + k - 1, dis.end());
  return dis[k - 1];
}

double distance_lb(double px, double py, double r) {
  vector<double> dis(n);
  for (int i = 0; i < n; ++i) {
    dis[i] = c[i] *
             hypot(max(abs(px - x[i]) - r, 0.0), max(abs(py - y[i]) - r, 0.0));
  }
  nth_element(dis.begin(), dis.begin() + k - 1, dis.end());
  return dis[k - 1];
}

int main() {
  cin >> n >> k;
  x.resize(n);
  y.resize(n);
  c.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> x[i] >> y[i] >> c[i];
  }
  queue<Node> que;
  double res = distance(0, 0);
  que.emplace(0, 0, 1000);
  while (!que.empty()) {
    auto node = que.front();
    que.pop();
    for (double dx : {-0.5, 0.5}) {
      for (double dy : {-0.5, 0.5}) {
        double nx = node.x + dx * node.r, ny = node.y + dy * node.r,
               nr = node.r / 2;
        double dis_lb = distance_lb(nx, ny, nr);
        if (res - dis_lb > 5e-7 * max(1.0, dis_lb)) {
          res = min(res, distance(nx, ny));
          que.emplace(nx, ny, nr);
        }
      }
    }
  }
  cout << fixed << setprecision(15) << res << endl;
}
Problem p02765 (Python)

Buggy submission (status: Runtime Error):
n, r = map(int, input())
if n >= 10:
    print(r)
else:
    print(r + 100 * (10 - n))


Fixed submission:
n, r = map(int, input().split())
if n >= 10:
    print(r)
else:
    print(r + 100 * (10 - n))
Problem p02765 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define vi vector<float>
#define vl vector<long long>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair<float, float>
#define umap unordered_map<ll, float>
using namespace std;

#define xx first
#define yy second

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

#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif

  int n, r;
  cin >> n >> r;

  if (n >= 10) {
    cout << r << endl;
  } else {
    cout << r + 100 * (10 - n) << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define vi vector<float>
#define vl vector<long long>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair<float, float>
#define umap unordered_map<ll, float>
using namespace std;

#define xx first
#define yy second

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

  // #ifndef ONLINE_JUDGE
  // 	freopen("in.txt", "r", stdin);
  // 	freopen("out.txt", "w", stdout);
  // #endif

  int n, r;
  cin >> n >> r;

  if (n >= 10) {
    cout << r << endl;
  } else {
    cout << r + 100 * (10 - n) << endl;
  }

  return 0;
}
Problem p02765 (C++)

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

int main() {
  long long n, a, b, s;
  s = 0;
  cin >> n >> a >> b;
  long long v[100000008][1000000008];
  for (int i = 0; i < n; i++) {
    v[i][0] = 1;
    v[i][i] = 1;
  }
  for (int i = 2; i < n; i++) {
    for (int j = 1; j < i; j++) {
      v[i][j] = (v[i - 1][j - 1] + v[i - 1][j]) % 1000000007;
    }
  }
  v[n - 1][a] = 0;
  v[n - 1][b] = 0;
  for (int i = 0; i < n; i++) {
    s = s + v[n - 1][i];
    s = s % 1000000007;
  }
  cout << s << endl;
}


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

int main() {
  int n, k;
  cin >> n >> k;
  if (n > 9)
    cout << k << endl;
  else
    cout << k + 100 * (10 - n) << endl;
}