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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define INF 0x3f3f3f3f
#define int long long
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef __int128 _int;
const int maxn = 1e5 + 6;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
int n, k;
int a[maxn];
map<int, int> num;
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * f;
}
void write(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
  return;
}
signed main() {
  cin >> n;
  int ans = 0;
  num.clear();
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ans += num[i - a[i]];
    num[i + a[i]] += 1;
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define INF 0x3f3f3f3f
#define int long long
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef __int128 _int;
const int maxn = 2e5 + 6;
const int mod = 1e9 + 7;
const double pi = acos(-1.0);
int n, k;
int a[maxn];
map<int, int> num;
int read() {
  int s = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    s = s * 10 + ch - '0';
    ch = getchar();
  }
  return s * f;
}
void write(int x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9)
    write(x / 10);
  putchar(x % 10 + '0');
  return;
}
signed main() {
  cin >> n;
  int ans = 0;
  num.clear();
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    ans += num[i - a[i]];
    num[i + a[i]] += 1;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

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

int main() {
  ll n;
  cin >> n;
  vll a(n);
  rep(i, n) cin >> a[i];
  map<ll, vll> mpp;
  map<ll, vll> mpm;
  rep(i, n) {
    ll p = i + a[i];
    ll m = i - a[i];
    mpp[p].push_back(i);
    mpm[m].push_back(i);
  }
  ll ans = 0;
  for (auto q : mpp) {
    ll k = q.first;
    vll vp = q.second;
    vll vm = mpm[k];
    rep(i, vp.size()) rep(j, vm.size()) {
      if (vp[i] < vm[j])
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;

int main() {
  ll n;
  cin >> n;
  vll a(n);
  rep(i, n) cin >> a[i];
  map<ll, vll> mpp;
  map<ll, vll> mpm;
  rep(i, n) {
    ll p = i + a[i];
    ll m = i - a[i];
    mpp[p].push_back(i);
    mpm[m].push_back(i);
  }
  ll ans = 0;
  for (auto q : mpp) {
    ll k = q.first;
    vll vp = q.second;
    vll vm = mpm[k];
    ll sp = vp.size();
    ll sm = vm.size();
    ans += sp * sm;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> plus(N), minus(300000, 0);
  rep(i, N) {
    int A;
    cin >> A;
    plus[i] = i + 1 + A;
    if (i + 1 - A > 0) {
      minus.at(i + 1 - A)++;
    }
  }
  ll ans = 0;
  rep(i, N) { ans += minus[plus[i]]; }
  cout << ans << endl;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> plus(N), minus(300000, 0);
  rep(i, N) {
    int A;
    cin >> A;
    plus[i] = i + 1 + A;
    if (i + 1 - A > 0) {
      minus.at(i + 1 - A)++;
    }
  }
  ll ans = 0;
  rep(i, N) {
    if (plus[i] < 300000) {
      ans += minus[plus[i]];
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n, ans = 0;
LL a[2000100], b[2000100];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i > a[i]) {
      b[i - a[i]]++;
    }
  }

  for (int i = 1; i <= n; i++) {
    if (i > a[i]) {
      b[i - a[i]]--;
    }
    ans += b[i + a[i]];
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n, ans = 0;
LL a[2000100], b[2000100];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i > a[i]) {
      b[i - a[i]]++;
    }
  }

  for (int i = 1; i <= n; i++) {
    if (i > a[i]) {
      b[i - a[i]]--;
    }
    if (i + a[i] <= n) {
      ans += b[i + a[i]];
    }
  }
  cout << ans << endl;
}
Problem p02691 (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++)
typedef long long ll;

int main() {
  int n;
  cin >> n;
  int A[n];
  rep(i, n) cin >> A[i];
  ll count = 0;
  rep(i, n) {
    for (int j = i + A[i]; j < n; j++) {
      if (A[i] + A[j] == j - i)
        count++;
    }
  }
  cout << count << endl;
}

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

int main() {
  int n;
  cin >> n;
  int A[n];
  rep(i, n) cin >> A[i];
  map<int, int> mp;
  rep(i, n) mp[A[i] + i + 1]++;
  ll ans = 0;
  rep(i, n) ans += mp[i + 1 - A[i]];
  cout << ans << endl;
}
Problem p02691 (C++)

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

#define x first
#define y second
#define pii pair<int, int>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
const int maxn = 5e3 + 35;
map<LL, int> mp;
int n, a[maxn];
int main() {
  cin >> n;
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (mp.count(i - a[i]))
      ans += mp[i - a[i]];
    mp[i + a[i]] += 1;
  }
  cout << ans << '\n';
  return 0;
}

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

#define x first
#define y second
#define pii pair<int, int>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long LL;
const int maxn = 3e5 + 35;
map<LL, int> mp;
int n, a[maxn];
int main() {
  cin >> n;
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    if (mp.count(i - a[i]))
      ans += mp[i - a[i]];
    mp[i + a[i]] += 1;
  }
  cout << ans << '\n';
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF = 1LL << 60;
ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N, 0);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<P> C(0);
  vector<P> D(0);
  for (ll i = 0; i < N; i++) {
    C.emplace_back(P(i + A[i], i));
    if (i > 0)
      D.emplace_back(P(i - A[i], i));
    ;
  }

  // 自分より番号大きいものだけ見ろ

  ll ans = 0;

  sort(C.begin(), C.end());
  sort(D.begin(), D.end());

  for (ll i = 0; i < N - 1; i++) {
    ll x = C[i].first;
    ll y = C[i].second; // yより大きい番号しか残すな
    ll s = lower_bound(D.begin(), D.end(), P(x, 0)) - D.begin();
    ll t = upper_bound(D.begin(), D.end(), P(x, INF)) -
           D.begin(); // ｓからｔ-1番目までをみる
    ans += t - s;
    D.erase(lower_bound(D.begin(), D.end(), P(i + 1 - A[i + 1], i + 1)));
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using Map = map<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF = 1LL << 60;
ll MOD = 1000000007;

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N, 0);
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
  }
  vector<P> C(0);
  vector<P> D(0);
  for (ll i = 0; i < N; i++) {
    C.emplace_back(P(i + A[i], i));
    if (i > 0)
      D.emplace_back(P(i - A[i], i));
    ;
  }

  // 自分より番号大きいものだけ見ろ

  ll ans = 0;

  sort(C.begin(), C.end());
  sort(D.begin(), D.end());

  for (ll i = 0; i < N - 1; i++) {
    ll x = C[i].first;
    ll y = C[i].second; // yより大きい番号しか残すな
    ll s = lower_bound(D.begin(), D.end(), P(x, 0)) - D.begin();
    ll t = upper_bound(D.begin(), D.end(), P(x, INF)) -
           D.begin(); // ｓからｔ-1番目までをみる
    ans += t - s;
    // D.erase(lower_bound(D.begin(), D.end(), P(i+1-A[i+1],i+1)))
    ;
  }
  cout << ans << endl;

  return 0;
}
Problem p02691 (C++)

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

#define scd second
#define fst first
#define mkp make_pair
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vlong;
typedef vector<vint> vvint;
typedef vector<vlong> vvlong;
typedef vector<bool> vbool;

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

  int n;
  cin >> n;
  vector<int> arr(n);
  for (auto &x : arr)
    cin >> x;
  unordered_map<int, int> cnt;
  ll ans = 0;
  for (int i = n - 1; i >= 0; i++) {
    auto itr = cnt.find(arr[i] + i + 1);
    if (itr != cnt.end()) {
      ans += itr->second;
    }
    cnt[i + 1 - arr[i]]++;
  }
  cout << ans << endl;
}

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

#define scd second
#define fst first
#define mkp make_pair
#define pb push_back
#define pf push_front
#define all(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vlong;
typedef vector<vint> vvint;
typedef vector<vlong> vvlong;
typedef vector<bool> vbool;

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

  int n;
  cin >> n;
  vector<int> arr(n);
  for (auto &x : arr)
    cin >> x;
  unordered_map<int, int> cnt;
  ll ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    auto itr = cnt.find(arr[i] + i + 1);
    if (itr != cnt.end()) {
      ans += itr->second;
    }
    cnt[i + 1 - arr[i]]++;
  }
  cout << ans << endl;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5, M = 1e9 + 7, OO = 0x3f3f3f3f;
typedef long long ll;
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
const double Pii = 3.14159265359;
ll n;
ll a[N], d[N], fr[N];

int main() {
  FIO cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    d[i] = i - a[i];
  }
  for (int i = 0; i < n; ++i)
    if (d[i] < N && d[i] >= 0)
      fr[d[i]]++;
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int didx = a[i] + i;
    ans += fr[didx];
    // cout << didx << " " << fr[didx] << endl;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long N = 2e5 + 5, M = 1e9 + 7, OO = 0x3f3f3f3f;
typedef long long ll;
#define FIO                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);
const double Pii = 3.14159265359;
ll n;
ll a[N], d[N], fr[N];

int main() {
  FIO cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    d[i] = i - a[i];
  }
  for (int i = 0; i < n; ++i)
    if (d[i] < N && d[i] >= 0)
      fr[d[i]]++;
  ll ans = 0;
  for (int i = 0; i < n; ++i) {
    int didx = a[i] + i;
    if (didx >= 0 && didx < N)
      ans += fr[didx];
    // cout << didx << " " << fr[didx] << endl;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int N, A[200005], B[200005];
long long ans;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i - A[i] > 0) {
      B[i - A[i]]++;
    }
  }
  for (int i = 0; i < N; i++) {
  }
  for (int i = 0; i < N; i++) {
    ans += B[i + A[i]];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int N, A[200005], B[200005];
long long ans;
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    if (i - A[i] > 0) {
      B[i - A[i]]++;
    }
  }
  for (int i = 0; i < N; i++) {
  }
  for (int i = 0; i < N; i++) {
    if (i + A[i] < 200005) {
      ans += B[i + A[i]];
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\n"
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

int main() {
  fastio ll n;
  cin >> n;
  ll a[n];
  rep(i, 0, n - 1) cin >> a[i];
  ll dp[n + 1];
  rep(i, 0, n) dp[i] = 0;
  ll ans = 0;
  rep(i, 0, n - 1) {
    if (0 <= i - a[i] && i - a[i] <= n) {
      ans += dp[i - a[i]];
    }
    dp[a[i] + i]++;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// #include <boost/multiprecision/cpp_ll.hpp>
// typedef boost::multiprecision::cpp_ll ll;
typedef long double dd;
#define i_7 (ll)(1E9 + 7)
// #define i_7 998244353
#define i_5 i_7 - 2
ll mod(ll a) {
  ll c = a % i_7;
  if (c >= 0)
    return c;
  return c + i_7;
}
typedef pair<ll, ll> l_l;
ll inf = (ll)1E18;
#define rep(i, l, r) for (ll i = l; i <= r; i++)
#define pb push_back
ll max(ll a, ll b) {
  if (a < b)
    return b;
  else
    return a;
}
ll min(ll a, ll b) {
  if (a > b)
    return b;
  else
    return a;
}
void Max(ll &pos, ll val) { pos = max(pos, val); } // Max(dp[n],dp[n-1]);
void Min(ll &pos, ll val) { pos = min(pos, val); }
void Add(ll &pos, ll val) { pos = mod(pos + val); }
dd EPS = 1E-9;
#define endl "\n"
#define fastio                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);

int main() {
  fastio ll n;
  cin >> n;
  ll a[n];
  rep(i, 0, n - 1) cin >> a[i];
  ll dp[n + 1];
  rep(i, 0, n) dp[i] = 0;
  ll ans = 0;
  rep(i, 0, n - 1) {
    if (0 <= i - a[i] && i - a[i] <= n) {
      ans += dp[i - a[i]];
    }
    if (0 <= a[i] + i && a[i] + i <= n) {
      dp[a[i] + i]++;
    }
  }
  cout << ans << endl;

  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Cr7                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define N 100100
ll n, a[N];
map<ll, ll> mp;
int main() {
  Cr7 cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  for (ll i = 2; i <= n; i++)
    mp[a[i] + a[1] - i + 1]++;
  ll need = 0, Res = mp[0];
  for (ll i = 2; i <= n; i++) {
    need--;
    need -= (a[i] - a[i - 1]);
    Res += mp[need];
  }
  cout << Res << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define Cr7                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define N 200200
ll n, a[N];
map<ll, ll> mp;
int main() {
  Cr7 cin >> n;
  for (ll i = 1; i <= n; i++)
    cin >> a[i];
  for (ll i = 2; i <= n; i++)
    mp[a[i] + a[1] - i + 1]++;
  ll need = 0, Res = mp[0];
  for (ll i = 2; i <= n; i++) {
    need--;
    need -= (a[i] - a[i - 1]);
    Res += mp[need];
  }
  cout << Res << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define pb push_back
#define POP pop_back()
#define ll long long
#define db double
#define POP pop_back()
#define endl '\n'
#define PII pair<int, int>
#define FI first
#define SE second
#define VI vector<int>
#define QI queue<int>
#define SI stack<int>
#define debug cout << "debug" << endl;
#define PLL pair<ll, ll>
#define PDD pair<double, double>
#define ULL unsigned long long
#define fo(i, a, b) for (int i = a; i <= b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define mem(x, a) memset(x, a, sizeof(x))
const int maxn = 1e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int a[maxn];
int main() {
  int n;
  cin >> n;
  ll sum = 0;
  map<int, int> mp;
  fo(i, 1, n) {
    cin >> a[i];
    sum += mp[i - a[i]];
    mp[a[i] + i]++;
  }
  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
#define pb push_back
#define POP pop_back()
#define ll long long
#define db double
#define POP pop_back()
#define endl '\n'
#define PII pair<int, int>
#define FI first
#define SE second
#define VI vector<int>
#define QI queue<int>
#define SI stack<int>
#define debug cout << "debug" << endl;
#define PLL pair<ll, ll>
#define PDD pair<double, double>
#define ULL unsigned long long
#define fo(i, a, b) for (int i = a; i <= b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define mem(x, a) memset(x, a, sizeof(x))
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
int a[maxn];
int main() {
  int n;
  cin >> n;
  ll sum = 0;
  map<int, int> mp;
  fo(i, 1, n) {
    cin >> a[i];
    sum += mp[i - a[i]];
    mp[a[i] + i]++;
  }
  cout << sum << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LL long long
#define MOD 1000000007
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define print(a, n)                                                            \
  for (LL e = 0; e < n; e++)                                                   \
    cout << a[e] << " ";                                                       \
  cout << endl
#define read(a, n)                                                             \
  for (LL e = 0; e < n; e++)                                                   \
    cin >> a[e];
using namespace std;

int main() {
  fastio;
  LL n;
  cin >> n;
  LL a[n];
  read(a, n);

  LL ans = 0;
  for (LL i = 1; i <= n; i++) {
    for (LL j = 0; j + i < n; j++) {
      if (a[j] + a[j + i] == i)
        ans++;
    }
  }

  cout << ans << "\n";

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define LL long long
#define MOD 1000000007
#define fastio                                                                 \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define print(a, n)                                                            \
  for (LL e = 0; e < n; e++)                                                   \
    cout << a[e] << " ";                                                       \
  cout << endl
#define read(a, n)                                                             \
  for (LL e = 0; e < n; e++)                                                   \
    cin >> a[e];
using namespace std;

int main() {
  fastio;
  LL n;
  cin >> n;
  LL a[n];
  read(a, n);

  LL ans = 0;
  map<int, int> m;

  for (int i = 1; i <= n; i++) {
    ans += m[i - a[i - 1]];
    m[i + a[i - 1]]++;
  }

  cout << ans << "\n";

  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mod 1000000007
#define ten5 100005
#define ten52 200005
#define ten6 1000005
#define PI 3.1415926
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mkpr(x1, x2) make_pair(x1, x2)
typedef long long int ll;

// stack<char> stk;
// set<ll> sll;
// map<string,ll> mp,mp1;
// map<pair<ll,ll>,ll>::iterator iter;
// map<ll,ll> mp;
// deque<char> deq;
// vector<ll> vll;
ll num[ten52];
ll num2[ten52];

int main(void) {
  // double ans=0.0,d,d1,d2;
  // float f,g,h;
  long long int m, n, i, j, k = 1;
  long long int a, a1, a2, a3, sum;
  string s, s1, s2;
  // char c,c1;
  sum = a = a1 = a2 = a3 = 0;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> num[i];
  for (i = 1; i <= n; i++)
    num2[i + num[i]]++;
  for (i = 1; i <= n; i++) {
    if (i - num[i] > 0)
      sum += num2[i - num[i]];
  }
  cout << sum;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define mod 1000000007
#define ten5 100005
#define ten52 200005
#define ten6 1000005
#define PI 3.1415926
#define pb(x) push_back(x)
#define all(x) x.begin(), x.end()
#define mkpr(x1, x2) make_pair(x1, x2)
typedef long long int ll;

// stack<char> stk;
// set<ll> sll;
// map<string,ll> mp,mp1;
// map<pair<ll,ll>,ll>::iterator iter;
// map<ll,ll> mp;
// deque<char> deq;
// vector<ll> vll;
ll num[ten52];
ll num2[ten52];

int main(void) {
  // double ans=0.0,d,d1,d2;
  // float f,g,h;
  long long int m, n, i, j, k = 1;
  long long int a, a1, a2, a3, sum;
  string s, s1, s2;
  // char c,c1;
  sum = a = a1 = a2 = a3 = 0;
  cin >> n;
  for (i = 1; i <= n; i++)
    cin >> num[i];
  for (i = 1; i <= n; i++)
    if (i + num[i] <= n)
      num2[i + num[i]]++;
  for (i = 1; i <= n; i++) {
    if (i - num[i] > 0)
      sum += num2[i - num[i]];
  }
  cout << sum;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n);
  for (auto &&e : a) {
    cin >> e;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = i + a[i] + 1; j < n; ++j) {
      if (a[j] == j - a[i] - i) {
        ans++;
      }
    }
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
constexpr ll mod = 1e9 + 7;

int main() {
  ll n, ans = 0;
  cin >> n;
  vector<ll> a(n);
  for (auto &&e : a) {
    cin >> e;
  }
  map<ll, ll> minus, plus;
  for (ll i = 0; i < n; ++i) {
    minus[i - a[i]]++;
    plus[i + a[i]]++;
  }
  for (const auto &e : plus) {
    ans += minus[e.first] * e.second;
  }
  cout << ans;
}
Problem p02691 (C++)

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define DEBUG(x) cout << #x << ": " << (x) << '\n'
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  REP(i, v.size()) cout << ' ' << v[i];                                        \
  cout << '\n'
#define ALL(a) (a).begin(), (a).end()

template <typename T> inline void CHMAX(T &a, const T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void CHMIN(T &a, const T b) {
  if (a > b)
    a = b;
}

constexpr ll MOD = 1000000007ll;
// constexpr ll MOD=998244353ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

const double EPS = 1e-9;
#define EQ0(x) (abs((x)) < EPS)
#define EQ(a, b) (abs((a) - (b)) < EPS)

int cnt[425252];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout<<setprecision(10)<<fixed;
  int n;
  ll a[252521], b[252521];
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    b[i] = i - a[i];
    if (0 <= b[i] && b[i] <= n * 2) {
      ++cnt[b[i]];
    }
  }
  ll ans = 0;
  REP(i, n) { ans += cnt[a[i] + i]; }
  cout << ans << '\n';
  return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define FORR(i, a, b) for (int i = (b)-1; i >= (a); --i)

#define DEBUG(x) cout << #x << ": " << (x) << '\n'
#define DEBUG_VEC(v)                                                           \
  cout << #v << ":";                                                           \
  REP(i, v.size()) cout << ' ' << v[i];                                        \
  cout << '\n'
#define ALL(a) (a).begin(), (a).end()

template <typename T> inline void CHMAX(T &a, const T b) {
  if (a < b)
    a = b;
}
template <typename T> inline void CHMIN(T &a, const T b) {
  if (a > b)
    a = b;
}

constexpr ll MOD = 1000000007ll;
// constexpr ll MOD=998244353ll;
#define FIX(a) ((a) % MOD + MOD) % MOD

const double EPS = 1e-9;
#define EQ0(x) (abs((x)) < EPS)
#define EQ(a, b) (abs((a) - (b)) < EPS)

int cnt[425252];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout<<setprecision(10)<<fixed;
  int n;
  ll a[252521], b[252521];
  cin >> n;
  REP(i, n) {
    cin >> a[i];
    b[i] = i - a[i];
    if (0 <= b[i] && b[i] <= n * 2) {
      ++cnt[b[i]];
    }
  }
  ll ans = 0;
  REP(i, n) {
    if (a[i] < n) {
      ans += cnt[a[i] + i];
    }
  }
  cout << ans << '\n';
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
int binary_search(vector<int> v, int n) { // sorted v, count v=n
  if (v.size() == 0)
    return 0;
  if (v[0] > n)
    return 0;
  if (v[v.size() - 1] < n)
    return 0;
  int l, r, mid, max, min;
  l = 0;
  r = v.size();
  if (v[v.size() - 1] == n) {
    max = v.size() - 1;
  } else {
    while (r - l > 1) {
      mid = (l + r) / 2;
      if (v[mid] <= n) { // v[l]<=n<v[r]
        l = mid;
      } else {
        r = mid;
      }
    }
    max = l; // max means v[all]<=n. all<=max
  }
  l = 0;
  r = v.size();
  if (v[0] == n) {
    min = 0;
  } else {
    while (r - l > 1) {
      mid = (l + r) / 2;
      if (n <= v[mid]) { // v[l]<n<=v[r]
        r = mid;
      } else {
        l = mid;
      }
    }
    min = r; // min means v[all]<=n. all>=min
  }
  // cout<<max<<" "<<min<<endl;
  return max - min + 1;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  long long int cnt = 0;
  vector<int> v;
  rep(i, n) {
    // count v==i-a[i]
    cnt += binary_search(v, i - a[i]);
    // sorted_insert i+a[i]
    v.insert(std::lower_bound(v.begin(), v.end(), i + a[i]), i + a[i]);
    // binary_insert(v,i+a[i]);
    // rep(i,v.size())cout<<v[i]<<" ";
    // cout<<endl;
  }
  cout << cnt << endl;
  /*
  int k;
  cin>>k;
  binary_insert(a,k);
  rep(i,a.size())cout<<a[i]<<" ";
  cout<<endl;
  */

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;
int binary_search(vector<int> v, int n) { // sorted v, count v=n
  if (v.size() == 0)
    return 0;
  if (v[0] > n)
    return 0;
  if (v[v.size() - 1] < n)
    return 0;
  int l, r, mid, max, min;
  l = 0;
  r = v.size();
  if (v[v.size() - 1] == n) {
    max = v.size() - 1;
  } else {
    while (r - l > 1) {
      mid = (l + r) / 2;
      if (v[mid] <= n) { // v[l]<=n<v[r]
        l = mid;
      } else {
        r = mid;
      }
    }
    max = l; // max means v[all]<=n. all<=max
  }
  l = 0;
  r = v.size();
  if (v[0] == n) {
    min = 0;
  } else {
    while (r - l > 1) {
      mid = (l + r) / 2;
      if (n <= v[mid]) { // v[l]<n<=v[r]
        r = mid;
      } else {
        l = mid;
      }
    }
    min = r; // min means v[all]<=n. all>=min
  }
  // cout<<max<<" "<<min<<endl;
  return max - min + 1;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  long long int cnt = 0;
  vector<int> v;
  rep(i, n) {
    // count v==i-a[i]
    cnt += upper_bound(v.begin(), v.end(), i - a[i]) -
           lower_bound(v.begin(), v.end(), i - a[i]);
    // sorted_insert i+a[i]
    v.insert(std::lower_bound(v.begin(), v.end(), i + a[i]), i + a[i]);
    // binary_insert(v,i+a[i]);
    // rep(i,v.size())cout<<v[i]<<" ";
    // cout<<endl;
  }
  cout << cnt << endl;
  /*
  int k;
  cin>>k;
  binary_insert(a,k);
  rep(i,a.size())cout<<a[i]<<" ";
  cout<<endl;
  */

  return 0;
}
Problem p02691 (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 repc(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ll long long
#define vec vector
#define ft first
#define sd second
#define all(vec) vec.begin(), vec.end()
typedef pair<int, int> P;
const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  vec<ll> a(n + 1);
  vec<ll> ai(n + 1);
  repc(i, 1, n) cin >> a[i];
  repc(i, 1, n) { ai[i] = a[i] + i; }

  int ans = 0;
  repc(j, 2, n) {
    int aj = j - a[j];
    repc(i, 1, j - 1) {
      if (ai[i] == aj)
        ans++;
    }
  }
  cout << ans << 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 repc(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ll long long
#define vec vector
#define ft first
#define sd second
#define all(vec) vec.begin(), vec.end()
typedef pair<int, int> P;
const int MOD = 1e9 + 7;
const int INF = 1001001001;

int main() {
  int n;
  cin >> n;
  vec<int> a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  ll ans = 0;
  rep(i, n) {
    int sa = i - a[i];
    ans += mp[sa];
    int wa = a[i] + i;
    mp[wa]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
#define int int64
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define SORT(x) sort(x.begin(), x.end())
#define GSORT(x) sort(x.begin(), x.end(), greater<int>())
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define V(T) vector<T>
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef vector<int> vi;
typedef vector<vi> vvi;
int max(int a, int b) {
  if (b > a)
    return b;
  else
    return a;
}
int min(int a, int b) {
  if (b < a)
    return b;
  else
    return a;
}
const int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  vi A(N);

  map<int, int> L, R;
  rep(i, N) {
    cin >> A[i];
    L[A[i] + i]++;
    R[i - A[i]]++;
  }

  int res = 0;
  for (auto &l : L) {
    for (auto &r : R) {
      if (l.fi == r.fi)
        res += l.se * r.se;
    }
  }

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
#define int int64
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define SORT(x) sort(x.begin(), x.end())
#define GSORT(x) sort(x.begin(), x.end(), greater<int>())
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define V(T) vector<T>
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef vector<int> vi;
typedef vector<vi> vvi;
int max(int a, int b) {
  if (b > a)
    return b;
  else
    return a;
}
int min(int a, int b) {
  if (b < a)
    return b;
  else
    return a;
}
const int INF = 1e18;

signed main() {
  int N;
  cin >> N;
  vi A(N);

  map<int, int> L, R;
  rep(i, N) {
    cin >> A[i];
    L[A[i] + i]++;
    R[i - A[i]]++;
  }

  int res = 0;
  for (auto &l : L) {
    res += l.se * R[l.fi];
  }

  cout << res << endl;

  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define PI 3.141592653589793238
#define mod 1000000007
#define endl '\n'
#define fastio                                                                 \
  ;                                                                            \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;

int main() {
  fastio;
  int n;
  cin >> n;
  vi h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vi neg(n), pos(n);
  map<int, vector<int>> MP;
  for (int i = 0; i < n; i++) {
    neg[i] = i - h[i], pos[i] = i + h[i];
    MP[neg[i]].pb(i);
  }
  ll prs = 0;
  for (int i = 0; i < n - 1; i++) {
    if (MP[pos[i]].size() == 0)
      continue;

    for (int j = 0; j < MP[pos[i]].size(); j++) {
      if (MP[pos[i]][j] > i)
        prs++;
    }

    // int k=MP[pos[i]].size();
    // int idx=
  }
  cout << prs << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define F first
#define S second
#define PI 3.141592653589793238
#define mod 1000000007
#define endl '\n'
#define fastio                                                                 \
  ;                                                                            \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
using namespace std;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<bool> vb;
typedef vector<char> vc;

int main() {
  fastio;
  int n;
  cin >> n;
  vi h(n);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  vi neg(n), pos(n);
  map<int, vector<int>> MP;
  for (int i = 0; i < n; i++) {
    neg[i] = i - h[i], pos[i] = i + h[i];
    MP[neg[i]].pb(i);
  }
  ll prs = 0;
  for (int i = 0; i < n - 1; i++) {
    if (MP[pos[i]].size() == 0)
      continue;
    int k = MP[pos[i]].size();
    int idx = lower_bound(MP[pos[i]].begin(), MP[pos[i]].end(), pos[i]) -
              MP[pos[i]].begin();
    prs += (k - idx);
  }
  cout << prs << endl;

  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define ll long long
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;

// snippets:tmp,cl,cvl,cs,co,coy,con,cov

int main() {
  ll n;
  cin >> n;
  queue<ll> q;
  queue<ll> qq;
  vll d(500000, 0);
  rep(i, n) {
    ll a;
    cin >> a;
    q.push(a + i);
    if (a > i)
      continue;
    d[i - a]++;
  }
  ll count = 0;
  ll a = q.size();
  rep(i, a) {
    count += d[q.front()];
    q.pop();
  }
  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define all(v) v.begin(), v.end()
#define ll long long
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<long long>;
using vvll = vector<vll>;

// snippets:tmp,cl,cvl,cs,co,coy,con,cov

int main() {
  ll n;
  cin >> n;
  queue<ll> q;
  queue<ll> qq;
  vll d(500000, 0);
  rep(i, n) {
    ll a;
    cin >> a;
    if (a > 200000)
      continue;
    q.push(a + i);
    if (a > i)
      continue;
    d[i - a]++;
  }
  ll count = 0;
  ll a = q.size();
  rep(i, a) {
    count += d[q.front()];
    q.pop();
  }
  cout << count << endl;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, n, m) for (int i = n; i < m; i++)
#define elif else if
#define INF 1000000007
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int n, m, cnt = 0, o, p, aa[220000], cntl = 0, ab[220000];
pair<int, int> pp[220000];
string s, ss[220000];
char c, cc[5000][5000] = {};
queue<int> que;
signed main() {
  cin >> n;
  rep(i, n) {
    cin >> aa[i];
    pp[i].first = aa[i] + i + 1;
    pp[i].second = i + 1 - aa[i];
    if (pp[i].second >= 2) {
      ab[pp[i].second]++;
    }
  }
  rep(i, n) {
    cnt += ab[pp[i].first];
    if (pp[i].second >= 2) {
      ab[pp[i].second]--;
    }
  }
  cout << cnt;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define srep(i, n, m) for (int i = n; i < m; i++)
#define elif else if
#define INF 1000000007
using namespace std;
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
int n, m, cnt = 0, o, p, aa[220000], cntl = 0, ab[220000];
pair<int, int> pp[220000];
string s, ss[220000];
char c, cc[5000][5000] = {};
queue<int> que;
signed main() {
  cin >> n;
  rep(i, n) {
    cin >> aa[i];
    pp[i].first = aa[i] + i + 1;
    pp[i].second = i + 1 - aa[i];
    if (pp[i].second >= 2) {
      ab[pp[i].second]++;
    }
  }
  rep(i, n) {
    if (pp[i].first < 200010) {
      cnt += ab[pp[i].first];
    }
    if (pp[i].second >= 2) {
      ab[pp[i].second]--;
    }
  }
  cout << cnt;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vint vector<int>
#define vll vector<ll>

#include <math.h>
// #include <stack>
// #include <queue>

/*
int standerd = int(pow(10.0,9.0)) + 7;
*/
string ans_Yes = "Yes";
string ans_No = "No";
string ans_yes = "yes";
string ans_no = "no";

#define MAXV 1000000

ll VALM[MAXV] = {};
ll VALP[MAXV] = {};
ll C;
ll N;
ll M;
ll K;

int main() {

  cin >> N;
  ll H[N + 1];
  for (int ni = 1; ni <= N; ni++) {
    ll tmp;
    cin >> tmp;
    H[ni] = tmp;
  }

  ll cnt = 0;
  for (int ni = N; ni >= 1; ni--) {
    ll hi = H[ni];
    if (hi < MAXV) {
      VALP[ni + hi]++;
      cnt += VALM[ni + hi];
      if (ni - hi >= 0) {
        VALM[ni - hi]++;
      }
    }
  }

  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define ll long long
#define ull unsigned long long
#define vint vector<int>
#define vll vector<ll>

#include <math.h>
// #include <stack>
// #include <queue>

/*
int standerd = int(pow(10.0,9.0)) + 7;
*/
string ans_Yes = "Yes";
string ans_No = "No";
string ans_yes = "yes";
string ans_no = "no";

#define MAXV 1000000

ll VALM[MAXV] = {};
ll VALP[MAXV] = {};
ll C;
ll N;
ll M;
ll K;

int main() {

  cin >> N;
  ll H[N + 1];
  for (int ni = 1; ni <= N; ni++) {
    ll tmp;
    cin >> tmp;
    H[ni] = tmp;
  }

  ll cnt = 0;
  for (int ni = N; ni >= 1; ni--) {
    ll hi = H[ni];
    if (hi < MAXV) {
      if (ni + hi < MAXV) {
        VALP[ni + hi]++;
        cnt += VALM[ni + hi];
      }
      if (ni - hi >= 0) {
        VALM[ni - hi]++;
      }
    }
  }

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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
#define M 1000000007
using namespace std;
ll t;
void fast() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
}
ll power(ll x, ul y, ll p) {
  ll res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void solve() {
  ll n, x, count = 0;
  cin >> n;
  vector<ll> v1(100000);
  for (ll i = 0; i < n; i++) {
    cin >> x;
    v1[i + x]++;
    if (i - x > 0)
      count += v1[i - x];
  }
  cout << count << "\n";
}
int main() {
  fast();
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define ul unsigned long long int
#define va vector<long long int> a
#define vb vector<long long int> b
#define M 1000000007
using namespace std;
ll t;
void fast() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
}
ll power(ll x, ul y, ll p) {
  ll res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
void solve() {
  ll n, x, count = 0;
  cin >> n;
  map<ll, ll> v1;
  for (ll i = 0; i < n; i++) {
    cin >> x;
    v1[i + x]++;
    if (i - x > 0)
      count += v1[i - x];
  }
  cout << count << "\n";
}
int main() {
  fast();
  solve();
  return 0;
}
Problem p02691 (C++)

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

int main() {
  int64_t n;
  cin >> n;
  int64_t ans = 0;
  vector<int64_t> h(n);
  rep(i, n) cin >> h.at(i);
  vector<int64_t> cnt(1000005);
  rep(i, n) {
    cnt.at(h.at(i) + i)++;
    if (i - h.at(i) >= 0 && i - h.at(i) <= 1000000) {
      ans += cnt.at(i - h.at(i));
    }
  }
  cout << ans << endl;
}


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

int main() {
  int64_t n;
  cin >> n;
  int64_t ans = 0;
  vector<int64_t> h(n);
  rep(i, n) cin >> h.at(i);
  vector<int64_t> cnt(1000005);
  rep(i, n) {
    if (h.at(i) + i <= 1000000)
      cnt.at(h.at(i) + i)++;
    if (i - h.at(i) >= 0 && i - h.at(i) <= 1000000) {
      ans += cnt.at(i - h.at(i));
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  vector<vector<int>> S(300000);
  rep(i, N) {
    int x = i - A[i];
    if (x < 0)
      continue;
    if (x >= 300000)
      continue;
    S[x].push_back(i);
  }
  rep(i, 300000) sort(S[i].begin(), S[i].end());
  long long ans = 0;
  rep(i, N) {
    int x = i + A[i];
    int left = -1;
    int right = (int)S[x].size();
    while (right - left > 1) {
      int mid = (right + left) / 2;
      if (S[x][mid] <= i)
        left = mid;
      else
        right = mid;
    }
    ans += (int)S[x].size() - right;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define REP(i, n) for (int i = 1; i < (int)(n); ++i)
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  rep(i, N) cin >> A[i];

  vector<vector<int>> S(300000);
  rep(i, N) {
    int x = i - A[i];
    if (x < 0)
      continue;
    if (x >= 300000)
      continue;
    S[x].push_back(i);
  }
  rep(i, 300000) sort(S[i].begin(), S[i].end());
  long long ans = 0;
  rep(i, N) {
    int x = i + A[i];
    if (x < 0)
      continue;
    if (x > 300000)
      continue;
    int left = -1;
    int right = (int)S[x].size();
    while (right - left > 1) {
      int mid = (right + left) / 2;
      if (S[x][mid] <= i)
        left = mid;
      else
        right = mid;
    }
    ans += (int)S[x].size() - right;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  const int MAXN = 1e5;
  int n, h[MAXN];
  cin >> n;
  map<int, int> cnt;
  for (int i = 1; i <= n; ++i) {
    cin >> h[i - 1];
    ++cnt[i - h[i - 1]];
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    --cnt[i - h[i - 1]];
    ans += cnt[h[i - 1] + i];
  }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  const int MAXN = 2e5;
  int n, h[MAXN];
  cin >> n;
  map<int, int> cnt;
  for (int i = 1; i <= n; ++i) {
    cin >> h[i - 1];
    ++cnt[i - h[i - 1]];
  }
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    --cnt[i - h[i - 1]];
    ans += cnt[h[i - 1] + i];
  }
  cout << ans << '\n';
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
#define LL long long

int N, A[222222], mi[555555] = {};
LL ans = 0ll;

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    if (i - A[i] > 0)
      mi[i - A[i]]++;
  }
  for (int i = 1; i <= N; i++) {
    int pl = i + A[i];
    ans += mi[pl];
    if (i - A[i] > 0)
      mi[i - A[i]]--;
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;
#define LL long long

int N, A[222222], mi[555555] = {};
LL ans = 0ll;

int main() {
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> A[i];
    if (i - A[i] > 0)
      mi[i - A[i]]++;
  }
  for (int i = 1; i <= N; i++) {
    int pl = i + A[i];
    if (pl < 500000)
      ans += mi[pl];
    if (i - A[i] > 0)
      mi[i - A[i]]--;
  }
  cout << ans << endl;

  return 0;
}