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

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

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> l(q), r(q);
  for (int i = 0; i < q; i++)
    cin >> l[i] >> r[i];

  vector<int> accum(n + 1, 0);
  accum[0] = 0;

  for (int i = 1; i <= n; i++) {

    accum[i] += accum[i - 1];

    if (s.at(i - 1) == 'A') {

      if (s.at(i) == 'C') {
        accum[i]++;
      }
    }
  }

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

    cout << accum[r[i] - 1] - accum[l[i] - 1] << endl;
  }

  return 0;
}

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

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> l(q), r(q);
  for (int i = 0; i < q; i++)
    cin >> l[i] >> r[i];

  vector<int> accum(n + 1, 0);
  accum[0] = 0;

  for (int i = 1; i < n; i++) {

    accum[i] += accum[i - 1];

    if (s.at(i - 1) == 'A') {

      if (s.at(i) == 'C') {
        accum[i]++;
      }
    }
  }

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

    cout << accum[r[i] - 1] - accum[l[i] - 1] << endl;
  }

  return 0;
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const int INF = 2e9;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
ll dp[10007];
int main() {
  ll n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  dp[0] = 0;
  ll count = 0;
  for (ll i = 1; i < n; i++) {
    if (s[i] == 'C' && s[i - 1] == 'A') {
      count++;
    }
    dp[i] = count;
  }
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    print(dp[r - 1] - dp[l - 1]);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1e9 + 7;
const int INF = 2e9;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
ll dp[100007];
int main() {
  ll n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  dp[0] = 0;
  ll count = 0;
  for (ll i = 1; i < n; i++) {
    if (s[i] == 'C' && s[i - 1] == 'A') {
      count++;
    }
    dp[i] = count;
  }
  rep(i, q) {
    ll l, r;
    cin >> l >> r;
    print(dp[r - 1] - dp[l - 1]);
  }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define COUT(x) cout << (x) << "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define REP(i, n) for (int i = 0; i < n; i++)
#define YES(x) cout << (x ? "YES" : "NO") << "\n"
#define Yes(x) cout << (x ? "Yes" : "No") << "\n"
#define dump(x) cout << #x << " = " << (x) << "\n"
#define endl "\n"
using G = vector<vector<int>>;
using M = map<int, int>;
using P = pair<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
using V = vector<int>;
using ll = long long;
template <class T> bool CHMAX(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool CHMIN(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  IOS;
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  int l[q], r[q];
  REP(i, q) {
    cin >> l[i] >> r[i];
    l[i];
    r[i];
  }
  vector<int> c(q + 1, 0);
  REP(i, n) {
    c[i + 1] = c[i];
    if (i == 0)
      continue;
    if (s[i] == 'C' && s[i - 1] == 'A')
      c[i + 1]++;
  }
  REP(i, q) COUT(c[r[i]] - c[l[i]]);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ALL(x) (x).begin(), (x).end()
#define COUT(x) cout << (x) << "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define REP(i, n) for (int i = 0; i < n; i++)
#define YES(x) cout << (x ? "YES" : "NO") << "\n"
#define Yes(x) cout << (x ? "Yes" : "No") << "\n"
#define dump(x) cout << #x << " = " << (x) << "\n"
#define endl "\n"
using G = vector<vector<int>>;
using M = map<int, int>;
using P = pair<int, int>;
using PQ = priority_queue<int>;
using PQG = priority_queue<int, vector<int>, greater<int>>;
using V = vector<int>;
using ll = long long;
template <class T> bool CHMAX(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool CHMIN(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

int main() {
  IOS;
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  int l[q], r[q];
  REP(i, q) {
    cin >> l[i] >> r[i];
    l[i];
    r[i];
  }
  vector<int> c(n + 1, 0);
  REP(i, n) {
    c[i + 1] = c[i];
    if (i == 0)
      continue;
    if (s[i] == 'C' && s[i - 1] == 'A')
      c[i + 1]++;
  }
  REP(i, q) COUT(c[r[i]] - c[l[i]]);
  return 0;
}
Problem p03087 (C++)

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

int main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> l(N), r(N);
  for (int i = 0; i < Q; ++i) {
    cin >> l.at(i) >> r.at(i);
  }

  vector<int> ac(N);
  int count = 0;
  for (int i = 0; i < N; ++i) {
    if (S.at(i) == 'A') {
      if (i < N - 1 && S.at(i + 1) == 'C') {
        ac.at(count) = i + 1;
        ++count;
      }
    }
  }

  sort(ac.begin(), ac.end());

  for (int i = 0; i < Q; ++i) {
    int ans = upper_bound(ac.begin(), ac.end(), r.at(i) - 1) -
              lower_bound(ac.begin(), ac.end(), l.at(i));
    cout << ans << endl;
  }
}

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

int main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> l(Q), r(Q);
  for (int i = 0; i < Q; ++i) {
    cin >> l.at(i) >> r.at(i);
  }

  vector<int> ac(N);
  int count = 0;
  for (int i = 0; i < N; ++i) {
    if (S.at(i) == 'A') {
      if (i < N - 1 && S.at(i + 1) == 'C') {
        ac.at(count) = i + 1;
        ++count;
      }
    }
  }

  sort(ac.begin(), ac.end());

  for (int i = 0; i < Q; ++i) {
    int ans = upper_bound(ac.begin(), ac.end(), r.at(i) - 1) -
              lower_bound(ac.begin(), ac.end(), l.at(i));
    cout << ans << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int t[10001], v[10001];
char a[10001];
int main() {
  int n, m, k = 0, l, r, ans, i;
  scanf("%d%d", &n, &m);
  scanf("%s", a);
  for (i = 0; i < n; i++) {
    if (a[i] == 'A' && a[i + 1] == 'C')
      v[i + 1] = 1;
  }
  for (i = 1; i <= n; i++)
    t[i] = t[i - 1] + v[i];
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &l, &r);
    ans = t[r] - t[l - 1] - (v[r] ? 1 : 0);
    printf("%d\n", ans);
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int t[100001], v[100001];
char a[100001];
int main() {
  int n, m, k = 0, l, r, ans, i;
  scanf("%d%d", &n, &m);
  scanf("%s", a);
  for (i = 0; i < n; i++) {
    if (a[i] == 'A' && a[i + 1] == 'C')
      v[i + 1] = 1;
  }
  for (i = 1; i <= n; i++)
    t[i] = t[i - 1] + v[i];
  for (i = 1; i <= m; i++) {
    scanf("%d%d", &l, &r);
    ans = t[r] - t[l - 1] - (v[r] ? 1 : 0);
    printf("%d\n", ans);
  }
  return 0;
}
Problem p03087 (C++)

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

using namespace std;
int k[10005];
int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == 'C' && s[i - 1] == 'A')
      k[i] = k[i - 1] + 1;
    else
      k[i] = k[i - 1];
  }
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    cout << k[b] - k[a] << endl;
  }
  return 0;
}

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

using namespace std;
int k[100005];
int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == 'C' && s[i - 1] == 'A')
      k[i] = k[i - 1] + 1;
    else
      k[i] = k[i - 1];
  }
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    cout << k[b] - k[a] << endl;
  }
  return 0;
}
Problem p03087 (C++)

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

int dp[MAX] = {};
int n, q;
string s;

void make() {
  int i = 0;
  dp[0] = 0;
  while (1) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      dp[i + 1] = dp[i] + 1;
    } else {
      dp[i + 1] = dp[i];
    }
    if (i + 1 == n - 1) {
      break;
    }
    i++;
  }

  // rep(i,n) {
  // 	cout << i << " " << dp[i] << endl;
  // }
}

int main() {
  cin >> n >> q >> s;
  make();
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    cout << dp[r - 1] - dp[l - 1] << endl;
  }
}


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

int dp[MAX] = {};
int n, q;
string s;

void make() {
  int i = 0;
  dp[0] = 0;
  while (1) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      dp[i + 1] = dp[i] + 1;
    } else {
      dp[i + 1] = dp[i];
    }
    if (i + 1 == n - 1) {
      break;
    }
    i++;
  }

  // rep(i,n) {
  // 	cout << i << " " << dp[i] << endl;
  // }
}

int main() {
  cin >> n >> q >> s;
  make();
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    cout << dp[r - 1] - dp[l - 1] << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, cc, n) for (int i = cc; i < n; ++i)
#define lrep(i, cc, n) for (long long i = cc; i < n; ++i)
#define rrep(i, cc, n) for (long i = cc; i > n; --i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;
const ll inf = 1001001001;

int countac(string a, int b, int c) {
  int count = 0;
  b--;
  c--;
  rep(j, b, c) {
    if (a[j] == 'A' && a[j + 1] == 'C')
      count++;
  }
  return count;
}

int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<int> l(q);
  vector<int> r(q);
  rep(i, 0, q) { cin >> l[i] >> r[i]; }

  rep(i, 0, q) { cout << countac(s, l[i], r[i]) << endl; }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, cc, n) for (int i = cc; i < n; ++i)
#define lrep(i, cc, n) for (long long i = cc; i < n; ++i)
#define rrep(i, cc, n) for (long i = cc; i > n; --i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;
const ll inf = 1001001001;

int countac(string a, int b, int c) {
  int count = 0;
  b--;
  c--;
  rep(j, b, c) {
    if (a[j] == 'A' && a[j + 1] == 'C')
      count++;
  }
  return count;
}

int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<int> l(q);
  vector<int> r(q);
  vector<int> aclength(n, 0);
  rep(i, 0, q) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }
  int cntac = 0;
  string temps;
  temps += s[0];
  rep(i, 1, n) {
    temps += s[i];
    if (temps[i - 1] == 'A' && temps[i] == 'C')
      cntac++;
    aclength[i] = cntac;
  }
  rep(i, 0, q) { cout << aclength[r[i]] - aclength[l[i]] << endl; }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
#include <vector>
#define MAX_N 10000
using namespace std;

int dp[MAX_N + 1];

int main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> l(Q), r(Q);
  for (int i = 0; i < Q; ++i)
    cin >> l[i] >> r[i];

  dp[0] = 0;
  for (int j = 0; j < N - 1; ++j) {
    dp[j + 1] = (S[j] == 'A' && S[j + 1] == 'C') ? dp[j] + 1 : dp[j];
  }

  for (int i = 0; i < Q; ++i) {
    cout << (dp[r[i] - 1] - dp[l[i] - 1]) << endl;
  }
}


Fixed submission:
#include <iostream>
#include <string>
#include <vector>
#define MAX_N 100000
using namespace std;

int dp[MAX_N + 1];

int main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> l(Q), r(Q);
  for (int i = 0; i < Q; ++i)
    cin >> l[i] >> r[i];

  dp[0] = 0;
  for (int j = 0; j < N - 1; ++j) {
    dp[j + 1] = (S[j] == 'A' && S[j + 1] == 'C') ? dp[j] + 1 : dp[j];
  }

  for (int i = 0; i < Q; ++i) {
    cout << (dp[r[i] - 1] - dp[l[i] - 1]) << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define MOD 1000000007
#define int long long
signed main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> A;
  rep(i, 1, N) {
    if (S.at(i) == 'C') {
      if (S.at(i - 1) == 'A')
        A.push_back(i);
    }
  }
  rep(i, 0, Q) {
    int l, r;
    cin >> l >> r;
    int count = 0;
    rep(j, 0, A.size()) {
      if (A.at(j) < r) {
        if (l <= A.at(j))
          count++;
      } else
        break;
    }
    cout << count << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define MOD 1000000007
#define int long long
signed main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> A;
  rep(i, 1, N) {
    if (S.at(i) == 'C') {
      if (S.at(i - 1) == 'A')
        A.push_back(i);
    }
  }
  rep(i, 0, Q) {
    int l, r;
    cin >> l >> r;
    int count = upper_bound(A.begin(), A.end(), r - 1) -
                lower_bound(A.begin(), A.end(), l);
    cout << count << endl;
  }
}
Problem p03087 (C++)

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

int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  int ar[n];
  bool aFlag = false;
  ar[0] = 0;
  for (int i = 0; i < n; i++) {
    if (aFlag && s[i] == 'C') {
      ar[i + 1] = 1 + ar[i];
    } else {
      ar[i + 1] = ar[i];
    }
    aFlag = false;
    if (s[i] == 'A') {
      aFlag = true;
    }
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << ar[r] - ar[l] << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  int ar[n + 1];
  bool aFlag = false;
  ar[0] = 0;
  for (int i = 0; i < n; i++) {
    if (aFlag && s[i] == 'C') {
      ar[i + 1] = 1 + ar[i];
    } else {
      ar[i + 1] = ar[i];
    }
    aFlag = false;
    if (s[i] == 'A') {
      aFlag = true;
    }
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << ar[r] - ar[l] << endl;
  }

  return 0;
}
Problem p03087 (C++)

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

using namespace std;

int main() {
  int n, q;
  string s;
  vector<int> rep(n, 0);

  cin >> n >> q >> s;

  for (int i = 0; i < n - 1; i++) {
    if (s.substr(i, 2) == "AC") {
      rep[i + 1] = 1;
    }
  }

  for (int i = 0; i < n - 1; i++) {
    rep[i + 1] += rep[i];
  }

  for (int i = 0; i < q; i++) {
    int l1, r1;
    cin >> l1 >> r1;
    cout << rep[r1 - 1] - rep[l1 - 1] << endl;
  }

  return 0;
}


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

using namespace std;

int main() {
  int n, q;
  string s;
  vector<int> rep(n, 0);

  cin >> n >> q >> s;

  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      rep[i + 1] = 1;
    }
  }

  for (int i = 0; i < n - 1; i++) {
    rep[i + 1] += rep[i];
  }

  for (int i = 0; i < q; i++) {
    int l1, r1;
    cin >> l1 >> r1;
    cout << rep[r1 - 1] - rep[l1 - 1] << endl;
  }

  return 0;
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int cnt[10];
int main() {
  int N, Q;
  cin >> N >> Q;
  string s;
  cin >> s;
  cnt[0] = 0;
  for (int i = 1; i < N; i++) {
    if (s[i - 1] == 'A' && s[i] == 'C')
      cnt[i] = cnt[i - 1] + 1;
    else
      cnt[i] = cnt[i - 1];
  }
  for (int i = 0; i < Q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << cnt[y] - cnt[x] << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int cnt[100001];
int main() {
  int N, Q;
  cin >> N >> Q;
  string s;
  cin >> s;
  cnt[0] = 0;
  for (int i = 1; i < N; i++) {
    if (s[i - 1] == 'A' && s[i] == 'C')
      cnt[i] = cnt[i - 1] + 1;
    else
      cnt[i] = cnt[i - 1];
  }
  for (int i = 0; i < Q; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    cout << cnt[y] - cnt[x] << endl;
  }
}
Problem p03087 (C++)

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

int l[10010], r[10010];
int t[10010];
int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  for (int i = 0; i < q; ++i) {
    cin >> l[i] >> r[i];
  }
  t[0] = 0;
  for (int i = 0; i < n - 1; ++i) {
    if ((s[i] == 'A') && (s[i + 1] == 'C')) {
      t[i + 1] = t[i] + 1;
    } else {
      t[i + 1] = t[i];
    }
  }

  for (int i = 0; i < q; ++i) {
    cout << t[r[i] - 1] - t[l[i] - 1] << endl;
  }

  return 0;
}


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

int l[100010], r[100010];
int t[100010];
int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  for (int i = 0; i < q; ++i) {
    cin >> l[i] >> r[i];
  }
  t[0] = 0;
  for (int i = 0; i < n - 1; ++i) {
    if ((s[i] == 'A') && (s[i + 1] == 'C')) {
      t[i + 1] = t[i] + 1;
    } else {
      t[i + 1] = t[i];
    }
  }

  for (int i = 0; i < q; ++i) {
    cout << t[r[i] - 1] - t[l[i] - 1] << endl;
  }

  return 0;
}
Problem p03087 (C++)

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

int main(void) {
  int n, q;
  char s[10010];
  cin >> n >> q;
  cin >> s;

  vector<int> a(n, 0);
  for (int i = 0; i < n - 1; i++) {
    a[i + 1] = a[i];
    if (s[i] == 'A' && s[i + 1] == 'C')
      a[i + 1]++;
    // cout << a[i] << " ";
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    cout << a[r] - a[l] << endl;
  }
}


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

int main(void) {
  int n, q;
  char s[100010];
  cin >> n >> q;
  cin >> s;

  vector<int> a(n, 0);
  for (int i = 0; i < n - 1; i++) {
    a[i + 1] = a[i];
    if (s[i] == 'A' && s[i + 1] == 'C')
      a[i + 1]++;
    // cout << a[i] << " ";
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    cout << a[r] - a[l] << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <time.h> //for clock
#include <unordered_map>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;
using ll = long long;

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

  int imos[10001];
  int i;

  imos[0] = 0;

  for (i = 1; i < s.size(); i++) {
    if (s[i - 1] == 'A' && s[i] == 'C') {
      imos[i] = imos[i - 1] + 1;
    } else {
      imos[i] = imos[i - 1];
    }
  }
  imos[s.size()] = imos[s.size() - 1];

  int l, r;
  for (i = 0; i < q; i++) {
    cin >> l >> r;
    if (r == s.size()) {
      cout << imos[r] - imos[l - 1] << endl;
    } else {
      if (s[r - 1] == 'A' && s[r] == 'C') {
        cout << imos[r] - imos[l - 1] - 1 << endl;
      } else {
        cout << imos[r] - imos[l - 1] << endl;
      }
    }
  }

  /*
  for(i = 0; i <= s.size(); i++){
      cout << imos[i] << endl;
  }
  */
}


Fixed submission:
#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <time.h> //for clock
#include <unordered_map>
#include <vector>
#define rep(i, a) for (int i = 0; i < (a); i++)
using namespace std;
using ll = long long;

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

  int imos[100001];
  int i;

  imos[0] = 0;

  for (i = 1; i < s.size(); i++) {
    if (s[i - 1] == 'A' && s[i] == 'C') {
      imos[i] = imos[i - 1] + 1;
    } else {
      imos[i] = imos[i - 1];
    }
  }
  imos[s.size()] = imos[s.size() - 1];

  int l, r;
  for (i = 0; i < q; i++) {
    cin >> l >> r;
    if (r == s.size()) {
      cout << imos[r] - imos[l - 1] << endl;
    } else {
      if (s[r - 1] == 'A' && s[r] == 'C') {
        cout << imos[r] - imos[l - 1] - 1 << endl;
      } else {
        cout << imos[r] - imos[l - 1] << endl;
      }
    }
  }

  /*
  for(i = 0; i <= s.size(); i++){
      cout << imos[i] << endl;
  }
  */
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int main() {

  int n, q;
  string s;
  cin >> n >> q;
  cin >> s;
  int check[10000] = {};
  int check100[100] = {};

  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 'A' && s[i + 1] == 'C')
      check[i] = 1;
  }

  for (int i = 0; i < n; i++) {
    if (check[i] == 1)
      check100[i / 100]++;
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    int cnt = 0;
    cin >> l >> r;
    l--;
    r--;

    for (int j = l / 100 + 1; j < r / 100; j++) {
      cnt = cnt + check100[j];
    }

    for (int j = l; j < (l / 100 + 1) * 100 && j < r; j++) {
      cnt = cnt + check[j];
    }

    if (l / 100 != r / 100) {
      for (int j = r / 100 * 100; j < r; j++) {
        cnt = cnt + check[j];
      }
    }

    printf("%d\n", cnt);
  }

  return 0;
}

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

using namespace std;

int main() {

  int n, q;
  string s;
  cin >> n >> q;
  cin >> s;
  int check[100000] = {};
  int check100[1000] = {};

  for (int i = 0; i < n - 1; i++) {
    if (s[i] == 'A' && s[i + 1] == 'C')
      check[i] = 1;
  }

  for (int i = 0; i < n; i++) {
    if (check[i] == 1)
      check100[i / 100]++;
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    int cnt = 0;
    cin >> l >> r;
    l--;
    r--;

    for (int j = l / 100 + 1; j < r / 100; j++) {
      cnt = cnt + check100[j];
    }

    for (int j = l; j < (l / 100 + 1) * 100 && j < r; j++) {
      cnt = cnt + check[j];
    }

    if (l / 100 != r / 100) {
      for (int j = r / 100 * 100; j < r; j++) {
        cnt = cnt + check[j];
      }
    }

    printf("%d\n", cnt);
  }

  return 0;
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  int l[Q];
  int r[Q];
  for (int i = 0; i < Q; i++) {
    cin >> l[i] >> r[i];
  }
  int L[N];
  int R[N];
  int A = 0;
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (A == 1 && S[i] == 'C')
      count++;
    if (S[i] == 'A')
      A = 1;
    else
      A = 0;
    L[i] = count;
  }
  count = 0;
  A = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (A == 1 && S[i] == 'A')
      count++;
    if (S[i] == 'C')
      A = 1;
    else
      A = 0;
    R[i] = count;
  }
  for (int i = 0; i < N; i++) {
    cout << R[l[i] - 1] + L[r[i] - 1] - L[N - 1] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  int l[Q];
  int r[Q];
  for (int i = 0; i < Q; i++) {
    cin >> l[i] >> r[i];
  }
  int L[N];
  int R[N];
  int A = 0;
  int count = 0;
  for (int i = 0; i < N; i++) {
    if (A == 1 && S[i] == 'C')
      count++;
    if (S[i] == 'A')
      A = 1;
    else
      A = 0;
    L[i] = count;
  }
  count = 0;
  A = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (A == 1 && S[i] == 'A')
      count++;
    if (S[i] == 'C')
      A = 1;
    else
      A = 0;
    R[i] = count;
  }
  for (int i = 0; i < Q; i++) {
    cout << L[r[i] - 1] - L[l[i] - 1] << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, Q;
  string s;
  cin >> N >> Q >> s;
  vector<int> t(N + 1, 0);
  for (int i = 0; i < N; i++)
    t.at(i + 1) = t.at(i) + ((s.at(i) == 'A' && s.at(i + 1) == 'C') ? 1 : 0);
  int l, r;
  for (int i = 0; i < Q; i++) {
    cin >> l >> r;
    cout << t.at(r - 1) - t.at(l - 1) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, Q;
  string s;
  cin >> N >> Q >> s;
  vector<int> t(N + 1, 0);
  for (int i = 0; i < N - 1; i++)
    t.at(i + 1) = t.at(i) + ((s.at(i) == 'A' && s.at(i + 1) == 'C') ? 1 : 0);
  int l, r;
  for (int i = 0; i < Q; i++) {
    cin >> l >> r;
    cout << t.at(r - 1) - t.at(l - 1) << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long l;
typedef long long ll;
typedef vector<int> vi;
typedef vector<l> vl;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  std::cout.unsetf(
      std::ios::floatfield); // this and next line for floating precision upto 6
  std::cout.precision(10);
  ll a, b, c, d, x, i, j, n, t, m, k, y;
  double p, q, r, w, u;
  string str = "", str1 = "";
  ll a1, a2, b1, b2;
  char ch;
  cin >> n >> t;
  cin >> str;
  vll v;
  for (i = 0; i < n; i = i + 1) {
    if (str[i] == 'A' && str[i + 1] == 'C') {
      v.pb(i + 1);
      i++;
    }
  }
  while (t--) {
    cin >> a >> b;
    vll::iterator it = lower_bound(all(v), a);
    vll::iterator itr = lower_bound(all(v), b);
    if (*it >= b) {
      cout << "0\n";
      continue;
    }
    if (*itr == b) {
      cout << itr - it << "\n";
    } else {
      itr--;
      cout << itr - it + 1 << "\n";
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long l;
typedef long long ll;
typedef vector<int> vi;
typedef vector<l> vl;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  std::cout.unsetf(
      std::ios::floatfield); // this and next line for floating precision upto 6
  std::cout.precision(10);
  ll a, b, c, d, x, i, j, n, t, m, k, y;
  double p, q, r, w, u;
  string str = "", str1 = "";
  ll a1, a2, b1, b2;
  char ch;
  cin >> n >> t;
  cin >> str;
  vll v;
  for (i = 0; i < n; i = i + 1) {
    if (str[i] == 'A' && str[i + 1] == 'C') {
      v.pb(i + 1);
      i++;
    }
  }
  while (t--) {
    cin >> a >> b;
    vll::iterator it = lower_bound(all(v), a);
    vll::iterator itr = lower_bound(all(v), b);
    if (it == v.end()) {
      cout << "0\n";
      continue;
    }
    if (*it >= b || it >= itr) {
      cout << "0\n";
      continue;
    }
    if (*itr == b) {
      cout << itr - it << "\n";
    } else {
      itr--;
      cout << itr - it + 1 << "\n";
    }
  }
}
Problem p03087 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define mins(a, b) a = min(a, b)
#define maxs(a, b) a = max(a, b)
using namespace std;

int main() {
  int n, q;
  int x[100010];
  for (int r = 0; r < 100010; r++) {
    x[r] = 0;
  }
  cin >> n >> q;
  string s;
  cin >> s;
  pair<int, int> len[q];
  for (int i = 0; i < q; i++) {
    cin >> len[i].first >> len[i].second;
  }
  for (int i = 1; i <= 100010; i++) {
    if (s[i - 1] == 'A' and s[i] == 'C')
      x[i] = 1 + x[i - 1];
    else
      x[i] += x[i - 1];
  }
  for (int i = 0; i < q; i++) {
    cout << x[len[i].second - 1] - x[len[i].first - 1] << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
#define mins(a, b) a = min(a, b)
#define maxs(a, b) a = max(a, b)
using namespace std;

int main() {
  int n, q;
  int x[100010];
  for (int r = 0; r < 100010; r++) {
    x[r] = 0;
  }
  cin >> n >> q;
  string s;
  cin >> s;
  pair<int, int> len[q];
  for (int i = 0; i < q; i++) {
    cin >> len[i].first >> len[i].second;
  }
  for (int i = 1; i <= s.size(); i++) {
    if (s[i - 1] == 'A' and s[i] == 'C')
      x[i] = 1 + x[i - 1];
    else
      x[i] += x[i - 1];
  }
  for (int i = 0; i < q; i++) {
    cout << x[len[i].second - 1] - x[len[i].first - 1] << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

string s;
int dp[100000];

int cnt(int k) {
  if (k < 1)
    return dp[k] = 0;
  if (dp[k] != -1)
    return dp[k];
  if (s[k] == 'C' && s[k - 1] == 'A')
    return dp[k] = cnt(k - 1) + 1;
  else
    return cnt(k - 1);
}

int main() {
  int n, q, l, r;
  cin >> n >> q >> s;
  for (int i = 0; i < n; i++) {
    dp[i] = -1;
  }
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    cout << cnt(r - 1) - cnt(l - 1) << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

string s;
int dp[100000];

int cnt(int k) {
  if (k < 1)
    return dp[k] = 0;
  if (dp[k] != -1)
    return dp[k];
  if (s[k] == 'C' && s[k - 1] == 'A')
    return dp[k] = cnt(k - 1) + 1;
  else
    return dp[k] = cnt(k - 1);
}

int main() {
  int n, q, l, r;
  cin >> n >> q >> s;
  for (int i = 0; i < n; i++) {
    dp[i] = -1;
  }
  for (int i = 0; i < q; i++) {
    cin >> l >> r;
    cout << cnt(r - 1) - cnt(l - 1) << endl;
  }
}
Problem p03087 (C++)

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

bool is_prime(int n) {
  if (n == 0 || n == 1)
    return (false);
  if (n % 2 == 0 && n != 2)
    return (false);
  for (int i = 3; i * i < n + 1; i += 2) {
    if (n % i == 0) {
      return (false);
    }
  }
  return (true);
}

int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<int> cum(n + 1, 0);
  for (int i = 0; i < n + 1; i++) {
    if (s.substr(i, 2) == "AC" && i < n - 1) {
      cum[i + 2] = cum[i + 1] + 1;
    } else {
      cum[i + 2] = cum[i + 1];
    }
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << cum[r] - cum[l] << '\n';
  }

  return 0;
}


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

bool is_prime(int n) {
  if (n == 0 || n == 1)
    return (false);
  if (n % 2 == 0 && n != 2)
    return (false);
  for (int i = 3; i * i < n + 1; i += 2) {
    if (n % i == 0) {
      return (false);
    }
  }
  return (true);
}

int main() {
  int n, q;
  string s;
  cin >> n >> q >> s;
  vector<int> cum(n + 1, 0);
  for (int i = 0; i < n - 1; i++) {
    if (s.substr(i, 2) == "AC" && i < n - 1) {
      cum[i + 2] = cum[i + 1] + 1;
    } else {
      cum[i + 2] = cum[i + 1];
    }
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << cum[r] - cum[l] << '\n';
  }

  return 0;
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  vector<int> l(Q), r(Q), ans(N + 1);
  for (int i = 0; i < Q; i++) {
    cin >> l.at(i) >> r.at(i);
  }
  ans.at(0) = 0;

  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'A' && S.at(i + 1) == 'C')
      ans.at(i + 1) = ans.at(i) + 1;
    else
      ans.at(i + 1) = ans.at(i);
  }
  for (int i = 0; i < Q; i++) {
    cout << ans.at(r.at(i) - 1) - ans.at(l.at(i) - 1) << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;
  S.push_back('D');
  vector<int> l(Q), r(Q), ans(N + 1);
  for (int i = 0; i < Q; i++) {
    cin >> l.at(i) >> r.at(i);
  }
  ans.at(0) = 0;

  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'A' && S.at(i + 1) == 'C')
      ans.at(i + 1) = ans.at(i) + 1;
    else
      ans.at(i + 1) = ans.at(i);
  }
  for (int i = 0; i < Q; i++) {
    cout << ans.at(r.at(i) - 1) - ans.at(l.at(i) - 1) << endl;
  }
}
Problem p03087 (C++)

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

using namespace std;
using ull = unsigned long long;
using ll = long long;

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

#define EPS (1e-7)
#define ALL(x) begin(x), end(x)

int countAC(const string &s) {
  const std::string find_word("AC");
  std::string::size_type pos = s.find(find_word);
  int count = 0;
  while (pos != std::string::npos) {
    count++;
    pos = s.find(find_word, pos + find_word.length());
  }
  return count;
}

int main() {
  ll N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;

  vector<pair<ll, ll>> LRs(Q);

  for (size_t i = 0; i < Q; i++) {
    cin >> LRs[i].first >> LRs[i].second;
  }

  // 最初に全部見つけておく。
  vector<ll> ACpos(N, 0);
  for (size_t i = 0; i < N - 1; i++) {
    if (S[i] == 'A' && S[i + 1] == 'C') {
      ACpos[i] = 1;
    }
  }

  for_each(ALL(LRs), [S, ACpos](auto lr) {
    int ans = 0;
    for (size_t i = lr.first - 1; i < lr.second - 1; i++) {
      if (ACpos[i] == 1)
        ans++;
    }
    cout << ans << endl;
  });

  return 0;
}


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

using namespace std;
using ull = unsigned long long;
using ll = long long;

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

#define EPS (1e-7)
#define ALL(x) begin(x), end(x)

int countAC(const string &s) {
  const std::string find_word("AC");
  std::string::size_type pos = s.find(find_word);
  int count = 0;
  while (pos != std::string::npos) {
    count++;
    pos = s.find(find_word, pos + find_word.length());
  }
  return count;
}

int main() {
  ll N, Q;
  cin >> N >> Q;
  string S;
  cin >> S;

  vector<pair<ll, ll>> LRs(Q);

  for (size_t i = 0; i < Q; i++) {
    cin >> LRs[i].first >> LRs[i].second;
  }

  // 最初に全部見つけておく。
  vector<ll> ACpos(N, 0);
  for (size_t i = 0; i < N - 1; i++) {
    if (S[i] == 'A' && S[i + 1] == 'C') {
      ACpos[i] = 1;
    }
  }

  // そして累積和を作る
  vector<ll> Sums(N + 1);
  for (size_t i = 0; i < N; i++) {
    Sums[i + 1] = Sums[i] + ACpos[i];
  }

  // 累積和を使って回答する
  for_each(ALL(LRs), [S, Sums](auto lr) {
    const ll ans = Sums[lr.second - 1] - Sums[lr.first - 1];
    cout << ans << endl;
  });

  return 0;
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

int main(void) {
  int64_t N, Q;
  std::string s;
  std::cin >> N >> Q >> s;
  std::vector<std::pair<int64_t, int64_t>> q_list(Q);

  for (int64_t i = 0; i < Q; ++i) {
    int64_t a, b;
    std::cin >> a >> b;
    q_list.at(i) = std::make_pair(a, b);
  }

  std::vector<int64_t> thousand_list(10 ^ 6 / 1000);
  for (int64_t i = 0; i < s.size() - 1; ++i) {
    if (s.at(i) == 'A' && s.at(i + 1) == 'C')
      thousand_list.at(i / 1000)++;
  }

  auto Count = [&](int64_t l, int64_t r) {
    int64_t count = 0;
    for (int64_t i = l; i < r; ++i) {
      if (s.at(i) == 'A' && s.at(i + 1) == 'C')
        count++;
    }
    return count;
  };

  for (auto &q : q_list) {
    int64_t count = 0;
    int64_t start = q.first - 1;
    int64_t end = q.second - 1;
    int64_t start_index = start / 1000;
    int64_t end_index = end / 1000;
    if (end_index - start_index >= 2) {
      for (int64_t i = start_index + 1; i < end_index; ++i) {
        count += thousand_list.at(i);
      }
      count += Count(start, start + 1000 - (start % 1000));
      count += Count(end - (end % 1000), end);
    } else {
      count = Count(start, end);
    }
    std::cout << count << std::endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>

int main(void) {
  int64_t N, Q;
  std::string s;
  std::cin >> N >> Q >> s;
  std::vector<std::pair<int64_t, int64_t>> q_list(Q);

  for (int64_t i = 0; i < Q; ++i) {
    int64_t a, b;
    std::cin >> a >> b;
    q_list.at(i) = std::make_pair(a, b);
  }

  std::vector<int64_t> thousand_list(1000000 / 1000);
  for (int64_t i = 0; i < s.size() - 1; ++i) {
    if (s.at(i) == 'A' && s.at(i + 1) == 'C')
      thousand_list.at(i / 1000)++;
  }

  auto Count = [&](int64_t l, int64_t r) {
    int64_t count = 0;
    for (int64_t i = l; i < r; ++i) {
      if (s.at(i) == 'A' && s.at(i + 1) == 'C')
        count++;
    }
    return count;
  };

  for (auto &q : q_list) {
    int64_t count = 0;
    int64_t start = q.first - 1;
    int64_t end = q.second - 1;
    int64_t start_index = start / 1000;
    int64_t end_index = end / 1000;
    if (end_index - start_index >= 2) {
      for (int64_t i = start_index + 1; i < end_index; ++i) {
        count += thousand_list.at(i);
      }
      count += Count(start, start + 1000 - (start % 1000));
      count += Count(end - (end % 1000), end);
    } else {
      count = Count(start, end);
    }
    std::cout << count << std::endl;
  }

  return 0;
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

int main() {
  int n, q;
  std::cin >> n >> q;

  std::string s;
  std::cin >> s;

  std::vector<int> pos_ac;
  for (int i = 0; i < s.length() - 1; i++) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      pos_ac.push_back(i);
    }
  }

  for (int i = 0; i < s.length() - 1; i++) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      pos_ac.push_back(i + n);
    }
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    std::cin >> l >> r;
    l--;
    r--;

    int count = 0;
    auto pos = *lower_bound(pos_ac.begin(), pos_ac.end(), l) + 1;
    for (auto pos = *lower_bound(pos_ac.begin(), pos_ac.end(), l) + 1; pos <= r;
         pos = *lower_bound(pos_ac.begin(), pos_ac.end(), pos) + 1) {
      count++;
    }

    std::cout << count << std::endl;
  }
}


Fixed submission:
#include <iostream>
#include <vector>

int main() {
  int n, q;
  std::cin >> n >> q;

  std::string s;
  std::cin >> s;

  std::vector<int> pos_ac;
  for (int i = 0; i < s.length() - 1; i++) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      pos_ac.push_back(i);
    }
  }

  for (int i = 0; i < s.length() - 1; i++) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      pos_ac.push_back(i + n);
    }
  }

  for (int i = 0; i < q; i++) {
    int l, r;
    std::cin >> l >> r;
    l--;
    r--;

    auto l_itr = lower_bound(pos_ac.begin(), pos_ac.end(), l);
    auto r_itr = lower_bound(pos_ac.begin(), pos_ac.end(), r);

    int count = r_itr - l_itr;

    std::cout << count << std::endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include "math.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  int n, q;
  cin >> n >> q;
  vector<pair<char, int>> s(n);
  rep(i, n) cin >> s[i].first;
  vector<int> l(q), r(q);
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    l[i] = x;
    r[i] = y;
  }
  for (int i = 0; i < n - 1; i++) {
    if (s[i].first == 'A') {
      if (s[i + 1].first == 'C') {
        s[i + 1].second = s[i].second + 1;
      } else
        s[i + 1].second = s[i].second;
    } else
      s[i + 1].second = s[i].second;
  }
  for (int i = 0; i < q; i++) {
    int x = s[r[i] - 1].second - s[l[i] - 1].second;
    cout << x << endl;
  }
}


Fixed submission:
#include "bits/stdc++.h"
#include "math.h"
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  int n, q;
  cin >> n >> q;
  vector<pair<char, int>> s(n);
  rep(i, n) cin >> s[i].first;
  vector<int> l(q), r(q);
  for (int i = 0; i < q; i++) {
    int x, y;
    cin >> x >> y;
    l[i] = x;
    r[i] = y;
  }
  for (int i = 0; i < n - 1; i++) {
    if (s[i].first == 'A') {
      if (s[i + 1].first == 'C') {
        s[i + 1].second = s[i].second + 1;
      } else
        s[i + 1].second = s[i].second;
    } else
      s[i + 1].second = s[i].second;
  }
  for (int i = 0; i < q; i++) {
    int x = s[r[i] - 1].second - s[l[i] - 1].second;
    cout << x << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define cans cout << ans << endl
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
typedef long long ll;

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    int ans = 0;
    for (int i = l; i < r; i++) {
      if (s[i] == 'A' && s[i + 1] == 'C')
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define cans cout << ans << endl
#define cyes cout << "Yes" << endl
#define cno cout << "No" << endl
typedef long long ll;

int main() {
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  vector<int> sum(n + 1);
  vector<int> l(q), r(q);
  rep(i, q) cin >> l[i] >> r[i];
  sum[0] = 0;
  for (int i = 1; i < s.size(); i++) {
    if (s[i - 1] == 'A' && s[i] == 'C')
      sum[i + 1] = sum[i] + 1;
    else
      sum[i + 1] = sum[i];
  }
  for (int i = 0; i < q; i++) {
    int ans = sum[r[i]] - sum[l[i]];
    cout << ans << endl;
  }
  return 0;
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  int N, Q, ans = 0, l, r;
  string S;
  cin >> N >> Q >> S;
  vector<int> Z(Q);
  for (int i = 0; i < N - 1; i++) {
    if (S[i] == 'A' && S[i + 1] == 'C')
      ans++;
    Z[i + 1] = ans;
  }
  for (int j = 0; j < Q; j++) {
    scanf("%d%d", &l, &r);
    cout << Z[r - 1] - Z[l - 1] << endl;
  }
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  int N, Q, ans = 0, l, r;
  string S;
  cin >> N >> Q >> S;
  vector<int> Z(N);
  for (int i = 0; i < N - 1; i++) {
    if (S[i] == 'A' && S[i + 1] == 'C')
      ans++;
    Z[i + 1] = ans;
  }
  for (int j = 0; j < Q; j++) {
    scanf("%d%d", &l, &r);
    cout << Z[r - 1] - Z[l - 1] << endl;
  }
}
Problem p03087 (C++)

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

int main() {
  int N, Q, l, r, sum;
  string S;
  cin >> N >> Q >> S;
  vector<int> vec(N);
  sum = 0;
  rep(i, N) {
    if (S.at(i) == 'A' && S.at(i + 1) == 'C')
      sum += 1;
    vec.at(i) = sum;
  }

  rep(i, Q) {
    cin >> l >> r;
    if (l == 1) {
      if (r == 1)
        cout << 0 << endl;
      else
        cout << vec.at(r - 2) << endl;
    } else
      cout << vec.at(r - 2) - vec.at(l - 2) << endl;
  }
}

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

int main() {
  int N, Q, l, r, sum;
  string S;
  cin >> N >> Q >> S;
  vector<int> vec(N);
  sum = 0;
  rep(i, N - 1) {
    if (S.at(i) == 'A' && S.at(i + 1) == 'C')
      sum += 1;
    vec.at(i) = sum;
  }

  rep(i, Q) {
    cin >> l >> r;
    if (l == 1) {
      if (r == 1)
        cout << 0 << endl;
      else
        cout << vec.at(r - 2) << endl;
    } else
      cout << vec.at(r - 2) - vec.at(l - 2) << endl;
  }
}
Problem p03087 (C++)

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

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

  vector<int> AC(N + 1);
  AC.at(0) = 0;
  for (int i = 1; i < N; i++) {
    if (S.at(i) == 'C' && S.at(i - 1) == 'A') {
      AC.at(i + 1) = AC.at(i) + 1;
    } else {
      AC.at(i + 1) = AC.at(i);
    }
  }

  int ans;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    if (S.at(l - 1) == 'C' && S.at(l - 2) == 'A') {
      ans = AC.at(r) - AC.at(l - 1) - 1;
    } else {
      ans = AC.at(r) - AC.at(l - 1);
    }
    cout << ans << endl;
  }
}


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

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

  vector<int> AC(N + 1);
  AC.at(0) = 0;
  for (int i = 1; i < N; i++) {
    if (S.at(i) == 'C' && S.at(i - 1) == 'A') {
      AC.at(i + 1) = AC.at(i) + 1;
    } else {
      AC.at(i + 1) = AC.at(i);
    }
  }

  int ans;
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    if (l > 1 && S.at(l - 1) == 'C' && S.at(l - 2) == 'A') {
      ans = AC.at(r) - AC.at(l - 1) - 1;
    } else {
      ans = AC.at(r) - AC.at(l - 1);
    }
    cout << ans << endl;
  }
}
Problem p03087 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
using namespace std;
int a[11000];
int main() {
  int n, q, i, j = 0;
  cin >> n >> q;
  string s;
  cin >> s;
  a[0] = 0;
  for (i = 1; i < n; i++) {
    if (s[i - 1] == 'A' && s[i] == 'C')
      j++;
    a[i] = j;
  }
  while (q--) {
    int st, e;
    cin >> st >> e;
    cout << a[e - 1] - a[st - 1] << endl;
  }
  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
int a[110000];
int main() {
  int n, q, i, j = 0;
  cin >> n >> q;
  string s;
  cin >> s;
  a[0] = 0;
  for (i = 1; i < n; i++) {
    if (s[i - 1] == 'A' && s[i] == 'C')
      j++;
    a[i] = j;
  }
  while (q--) {
    int st, e;
    cin >> st >> e;
    cout << a[e - 1] - a[st - 1] << endl;
  }
  return 0;
}