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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define int long long
#define lowbit(x) ((x) & (-x))
#define hh putchar('\n')
#define kg putchar(' ')
using namespace std;
const int maxn = 200005, mod = 20101009;
int n, a[maxn], ans, tong[maxn];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
inline void write(int a) {
  if (a < 0) {
    putchar('-');
    putchar('1');
  } else {
    if (a >= 10)
      write(a / 10);
    putchar(a % 10 + '0');
  }
}
signed main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 1; i <= n; ++i) {
    if (a[i] <= i)
      ans += tong[-a[i] + i];
    ++tong[a[i] + i];
  }
  write(ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define int long long
#define lowbit(x) ((x) & (-x))
#define hh putchar('\n')
#define kg putchar(' ')
using namespace std;
const int maxn = 200005, mod = 20101009;
int n, a[maxn], ans;
map<int, int> tong;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return x * f;
}
inline void write(int a) {
  if (a < 0) {
    putchar('-');
    putchar('1');
  } else {
    if (a >= 10)
      write(a / 10);
    putchar(a % 10 + '0');
  }
}
signed main() {
  n = read();
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 1; i <= n; ++i) {
    if (a[i] <= i)
      ans += tong[-a[i] + i];
    ++tong[a[i] + i];
  }
  write(ans);
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0),                            \
      cout << fixed << setprecision(20);
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define PI 3.14159265358979
typedef long long ll;
using namespace std;
const ll MOD = 1e9 + 7LL;
const int INF = 2e9;
int N, A[100005];
unordered_map<int, ll> M;

// i<jのときj-i=A[i]+A[j]
// A[i]+i=j-A[j]
int main() {
  INCANT;
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    M[A[i] + i]++;
  }
  ll res = 0;
  rep(i, N) res += M[i - A[i]];

  cout << res << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define INCANT                                                                 \
  cin.tie(0), cout.tie(0), ios::sync_with_stdio(0),                            \
      cout << fixed << setprecision(20);
#define rep(i, n) for (int i = 0; i < n; ++i)
#define ALL(a) (a).begin(), (a).end()
#define PI 3.14159265358979
typedef long long ll;
using namespace std;
const ll MOD = 1e9 + 7LL;
const int INF = 2e9;
int N, A[200005];
unordered_map<int, ll> M;

// i<jのときj-i=A[i]+A[j]
// A[i]+i=j-A[j]
int main() {
  INCANT;
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    M[A[i] + i]++;
  }
  ll res = 0;
  rep(i, N) res += M[i - A[i]];

  cout << res << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(begin, end) for (i = begin; i < end; i++)
#define repj(begin, end) for (j = begin; j < end; j++)
#define init(arr, end, val)                                                    \
  for (i = 0; i < end; i++)                                                    \
    arr[i] = val;
#define printint(i0, i1) printf("%d %d\n", i0, i1)

using namespace std;

typedef long long int ll;
const ll inf = 1000000000;
const ll mod = 1000000007;
const ll nil = -1;
ll i, n, m, k, ans, a;
vector<pair<ll, ll>> iminusa;
ll iplusa[200000];

bool compare_by_b(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

int main() {
  scanf(" %lld", &n);
  rep(0, n) {
    scanf(" %lld", &a);
    iminusa.push_back(make_pair(i, i - a));
    iplusa[i] = i + a;
  }
  sort(iminusa.begin(), iminusa.end(), compare_by_b);
  ans = 0;
  rep(0, n) {
    const ll sum = iplusa[i];
    auto lb = lower_bound(iminusa.begin(), iminusa.end(), make_pair(nil, sum),
                          compare_by_b);
    auto ub = upper_bound(iminusa.begin(), iminusa.end(), make_pair(inf, sum),
                          compare_by_b);
    for (auto it = lb; it != ub; it++) {
      if (it->first > i)
        ans++;
    }
  }

  printf("%lld\n", ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(begin, end) for (i = begin; i < end; i++)
#define repj(begin, end) for (j = begin; j < end; j++)
#define init(arr, end, val)                                                    \
  for (i = 0; i < end; i++)                                                    \
    arr[i] = val;
#define printint(i0, i1) printf("%d %d\n", i0, i1)

using namespace std;

typedef long long int ll;
const ll inf = 1000000000;
const ll mod = 1000000007;
const ll nil = -1;
ll i, n, m, k, ans, a;
vector<pair<ll, ll>> iminusa;
ll iplusa[200000];

bool compare_by_b(pair<ll, ll> a, pair<ll, ll> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  } else {
    return a.first < b.first;
  }
}

int main() {
  scanf(" %lld", &n);
  rep(0, n) {
    scanf(" %lld", &a);
    iminusa.push_back(make_pair(i, i - a));
    iplusa[i] = i + a;
  }
  sort(iminusa.begin(), iminusa.end(), compare_by_b);
  ans = 0;
  rep(0, n) {
    const ll sum = iplusa[i];
    auto lb = lower_bound(iminusa.begin(), iminusa.end(), make_pair(nil, sum),
                          compare_by_b);
    auto ub = upper_bound(iminusa.begin(), iminusa.end(), make_pair(inf, sum),
                          compare_by_b);
    ans += ub - lb;
  }

  printf("%lld\n", ans);
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define int long long
#define P pair<int, int>
#define all(vec) vec.begin(), vec.end()

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int n;
int cnt[100010];
int ans;

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (0 <= i - a + 1)
      ans += cnt[i - a + 1];
    cnt[min(n, i + a + 1)]++;
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
#define pie 3.141592653589793238462643383279
#define mod 1000000007
#define int long long
#define P pair<int, int>
#define all(vec) vec.begin(), vec.end()

int gcd(int x, int y) {
  if (y == 0)
    return x;
  return gcd(y, x % y);
}
int lcm(int x, int y) { return x / gcd(x, y) * y; }
int kai(int x, int y) {
  int res = 1;
  for (int i = x - y + 1; i <= x; i++) {
    res *= i;
    res %= mod;
  }
  return res;
}
int mod_pow(int x, int y, int m) {
  int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x % m;
    }
    x = x * x % m;
    y >>= 1;
  }
  return res;
}

int comb(int x, int y) {
  if (y > x)
    return 0;
  return kai(x, y) * mod_pow(kai(y, y), mod - 2, mod) % mod;
}

int n;
int cnt[200010];
int ans;

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (0 <= i - a + 1)
      ans += cnt[i - a + 1];
    cnt[min(n, i + a + 1)]++;
  }
  cout << ans << endl;
}
Problem p02691 (C++)

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

signed main() {
  lint N;
  cin >> N;
  vector<lint> a(N), b(500000, 0), c(500000, 0);
  for (lint i = 0; i < N; i++) {
    cin >> a[i];
    b[i + a[i]]++;
    if (i - a[i] >= 0)
      c[i - a[i]]++;
  }
  lint ans = 0;
  for (lint i = 0; i < b.size(); i++) {
    if (b[i] != 0 && c[i] != 0)
      ans += b[i] * c[i];
  }
  cout << ans << endl;
}

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

signed main() {
  lint N;
  cin >> N;
  vector<lint> a(N), b(500000, 0), c(500000, 0);
  for (lint i = 0; i < N; i++) {
    cin >> a[i];
    if (i + a[i] <= N)
      b[i + a[i]]++;
    if (i - a[i] >= 0)
      c[i - a[i]]++;
  }
  lint ans = 0;
  for (lint i = 0; i < b.size(); i++) {
    if (b[i] != 0 && c[i] != 0)
      ans += b[i] * c[i];
  }
  cout << ans << endl;
}
Problem p02691 (C++)

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

using namespace std;

int a[4100000];
int main() {
  int n;
  long long ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (i >= x)
      ans += a[i - x];
    a[i + x]++;
  }
  cout << ans << endl;
  return 0;
}

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

using namespace std;

int a[4100000];
int main() {
  int n;
  long long ans = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if (i >= x)
      ans += a[i - x];
    if (i + x < 2 * n)
      a[i + x]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

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

#define all(x) x.begin(), x.end()
#define lc (i << 1)
#define rc (i << 1 | 1)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 1e5 + 5, LN = 17, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
int N, a[MN];
map<ll, int> mp;

int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    mp[i - a[i]]++;
  }
  ll ret = 0;
  for (int i = 1; i <= N; i++) {
    ret += mp[i + a[i]];
  }
  cout << ret << '\n';
  return 0;
}


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

#define all(x) x.begin(), x.end()
#define lc (i << 1)
#define rc (i << 1 | 1)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
const int MN = 2e5 + 5, LN = 17, MOD = 1e9 + 7, INF = 0x3f3f3f3f;
int N, a[MN];
map<ll, int> mp;

int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
    mp[i - a[i]]++;
  }
  ll ret = 0;
  for (int i = 1; i <= N; i++) {
    ret += mp[i + a[i]];
  }
  cout << ret << '\n';
  return 0;
}
Problem p02691 (C++)

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

int main() {
  int n;
  cin >> n;
  ll an[n];
  rep(i, n) cin >> an[i];
  ll pn[n];
  rep(i, n) pn[i] = i - an[i] + 1;
  ll qn[n];
  rep(i, n) qn[i] = i + an[i] + 1;
  ll pdp[n];
  rep(i, n) pdp[i] = 0;
  rep(i, n) { pdp[pn[i]]++; }
  ll cnt = 0;
  rep(i, n) if (qn[i] < n) { cnt += pdp[qn[i]]; }
  cout << cnt;
}


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

int main() {
  int n;
  cin >> n;
  ll an[n];
  rep(i, n) cin >> an[i];
  ll pn[n];
  rep(i, n) pn[i] = i - an[i] + 1;
  ll qn[n];
  rep(i, n) qn[i] = i + an[i] + 1;
  ll pdp[n];
  rep(i, n) pdp[i] = 0;
  rep(i, n) if (pn[i] >= 0) { pdp[pn[i]]++; }
  ll cnt = 0;
  rep(i, n) if (qn[i] < n) { cnt += pdp[qn[i]]; }
  cout << cnt;
}
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(NULL);
  map<long long int, long long int> m;
  long long int n;
  cin >> n;
  long long int c = 0;
  vector<long long int> a(n);
  for (long long int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[0] = 0;
  for (long long int i = 1; i <= n; i++) {
    if (m.count(i - a[i]) > 0) {
      c += m[i - a[i]];
    }
    m[i + a[i]]++;
  }
  cout << c;
  return 0;
}

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

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  map<long long int, long long int> m;
  long long int n;
  cin >> n;
  long long int c = 0;
  vector<long long int> a(n + 1);
  for (long long int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  a[0] = 0;
  for (long long int i = 1; i <= n; i++) {
    if (m.count(i - a[i]) > 0) {
      c += m[i - a[i]];
    }
    m[i + a[i]]++;
  }
  cout << c;
  return 0;
}
Problem p02691 (C++)

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

using namespace std;

long long a[100005];
long long a_plus_i[100005];

int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] + i <= n && a[i] + i >= 1) {
      a_plus_i[a[i] + i] += 1;
    }
  }
  long long pairs = 0;
  for (long long j = 1; j <= n; j++) {
    if (j - a[j] >= 1 && j - a[j] <= n) {
      pairs += a_plus_i[j - a[j]];
    }
  }
  cout << pairs << "\n";
  return 0;
}


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

using namespace std;

long long a[200005];
long long a_plus_i[200005];

int main() {
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] + i <= n && a[i] + i >= 1) {
      a_plus_i[a[i] + i] += 1;
    }
  }
  long long pairs = 0;
  for (long long j = 1; j <= n; j++) {
    if (j - a[j] >= 1 && j - a[j] <= n) {
      pairs += a_plus_i[j - a[j]];
    }
  }
  cout << pairs << "\n";
  return 0;
}
Problem p02691 (C++)

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

int main() {
  int n;
  scanf("%d", &n);
  long long ans = 0;
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    //        ans += ct[i - x];
    //        ct[i + x]++;
    ct[i - x]++;
    ct2[i + x]++;
  }
  for (int i = 1; i <= 1000200000; i++) {
    ans += ct[i] * ct2[i];
  }
  printf("%lld", ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
map<int, int> ct;
map<int, int> ct2;

int main() {
  int n;
  scanf("%d", &n);
  long long ans = 0;
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    //        ans += ct[i - x];
    //        ct[i + x]++;
    ct[i - x]++;
    ct2[i + x]++;
  }
  for (int i = 1; i <= 200000; i++) {
    ans += (long long)ct[i] * (long long)ct2[i];
  }
  printf("%lld", ans);
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
template <typename T> void fin(T const &t) {
  std::cout << t << std::endl;
  exit(0);
}

int const MAXN = 2e5;

int cnt[MAXN];

int main() {
  int N;
  std::cin >> N;
  std::fill_n(cnt, 0, N + 1);
  int64_t ans = 0;
  int A;
  std::cin >> A; // (1)
  if (1 + A <= N)
    ++cnt[1 + A];
  for (int i = 2; i <= N; ++i) {
    std::cin >> A; // (i)
    if (i > A)
      ans += cnt[i - A];
    ++cnt[i + A];
  }
  fin(ans);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
template <typename T> void fin(T const &t) {
  std::cout << t << std::endl;
  exit(0);
}

int const MAXN = 2e5;

int cnt[MAXN];

int main() {
  int N;
  std::cin >> N;
  std::fill_n(cnt, 0, N + 1);
  int64_t ans = 0;
  int A;
  std::cin >> A; // (1)
  if (1 + A <= N)
    ++cnt[1 + A];
  for (int i = 2; i <= N; ++i) {
    std::cin >> A; // (i)
    if (i > A)
      ans += cnt[i - A];
    if (i + A <= N)
      ++cnt[i + A];
  }
  fin(ans);
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5 + 5;
map<int, int> mp;
int a[maxn];
int n;
ll ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    if (i - a[i] > 0)
      mp[i - a[i]]++;
  for (int i = 1; i <= n; i++)
    if (mp[i + a[i]])
      ans += mp[i + a[i]];
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 2e5 + 5;
map<int, int> mp;
int a[maxn];
int n;
ll ans;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    if (i - a[i] > 0)
      mp[i - a[i]]++;
  for (int i = 1; i <= n; i++)
    if (mp[i + a[i]])
      ans += mp[i + a[i]];
  cout << ans;
  return 0;
}
Problem p02691 (C++)

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

typedef long long ll;

ll N;
ll A[100010];
map<ll, ll> mp;

int main() {
  cin >> N;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i] - i]++;
  }

  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    ll num = -A[i] - i;
    mp[A[i] - i]--;
    ans += mp[num];
  }

  cout << ans << endl;
}


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

typedef long long ll;

ll N;
ll A[200010];
map<ll, ll> mp;

int main() {
  cin >> N;
  for (ll i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i] - i]++;
  }

  ll ans = 0;
  for (ll i = 0; i < N; i++) {
    ll num = -A[i] - i;
    mp[A[i] - i]--;
    ans += mp[num];
  }

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

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int64;
int P[200000];
int M[200000];

int main() {
  memset(P, 0, sizeof(P));
  memset(P, 0, sizeof(M));
  int N;
  cin >> N;
  int tmp;
  int64 ans = 0;
  for (int i = 0; i < N; ++i) {
    cin >> tmp;
    if (tmp < 200000) {
      ++P[i + tmp];
      if (i - tmp > 0)
        ++M[i - tmp];
    }
  }
  for (int i = 1; i < N; ++i) {
    ans += (int64)P[i] * M[i];
  }
  cout << ans << "\n";
  return 0;
}


Fixed submission:
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int64;
int P[400000];
int M[200000];

int main() {
  memset(P, 0, sizeof(P));
  memset(P, 0, sizeof(M));
  int N;
  cin >> N;
  int tmp;
  int64 ans = 0;
  for (int i = 0; i < N; ++i) {
    cin >> tmp;
    if (tmp < 200000) {
      ++P[i + tmp];
      if (i - tmp > 0)
        ++M[i - tmp];
    }
  }
  for (int i = 1; i < N; ++i) {
    ans += (int64)P[i] * M[i];
  }
  cout << ans << "\n";
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
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 ll INF = 2e15;
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n;
  cin >> n;
  vl a(n);
  vl L(200007, 0);
  vl R(200007, 0);
  rep(i, n) {
    cin >> a[i];
    L[i + 1 + a[i]]++;
    R[i + 1 - a[i]]++;
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += L[i] * R[i];
  }
  print(ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vl = vector<ll>;
using vll = vector<vl>;
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 ll INF = 2e15;
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;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }

int main() {
  ll n;
  cin >> n;
  vl a(n);
  vl L(200007, 0);
  vl R(200007, 0);
  rep(i, n) {
    cin >> a[i];
    ll x = i + 1 + a[i], y = i + 1 - a[i];
    if (0 < x && x < 200007) {
      L[x]++;
    }
    if (0 < y && y < 200007) {
      R[y]++;
    }
  }
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    ans += L[i] * R[i];
  }
  print(ans);
}
Problem p02691 (C++)

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

long long ckc[1000000], ckt[1000000], res;

int main() {
  long long n;
  std::cin >> n;
  long long a[n];
  for (long long i = 1; i <= n; i++) {
    std::cin >> a[i];
    if ((a[i] + i >= 0) && (a[i] + i <= 2 * 100000))
      ckc[a[i] + i]++;
    if ((i - a[i] >= 0) && (i - a[i] <= 2 * 100000))
      ckt[i - a[i]]++;
  }
  for (long long i = 0; i <= 2 * 100000; i++) {
    res += ckc[i] * ckt[i];
  }
  std::cout << res;
}

Fixed submission:
#include <iostream>

long long ckc[1000000], ckt[1000000], res;

int main() {
  long long n;
  std::cin >> n;
  long long a[n + 1];
  for (long long i = 1; i <= n; i++) {
    std::cin >> a[i];
    if ((a[i] + i >= 0) && (a[i] + i <= 2 * 100000))
      ckc[a[i] + i]++;
    if ((i - a[i] >= 0) && (i - a[i] <= 2 * 100000))
      ckt[i - a[i]]++;
  }
  for (long long i = 0; i <= 2 * 100000; i++) {
    res += ckc[i] * ckt[i];
  }
  std::cout << res;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double PI = 3.1415926535897932; // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> pre(n);
  map<int, int> mp;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ++mp[i - a[i]];
    pre[i] = i - a[i];
  }
  ll ans = 0;
  for (int i = 0; i + 1 < n; ++i) {
    --mp[pre[i - a[i]]];
    ans += mp[a[i] + i];
  }

  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef uint64_t ull;
typedef pair<int, int> P;
constexpr double PI = 3.1415926535897932; // acos(-1)
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> pre(n);
  map<int, int> mp;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ++mp[i - a[i]];
    pre[i] = i - a[i];
  }
  ll ans = 0;
  for (int i = 0; i + 1 < n; ++i) {
    --mp[pre[i]];
    ans += mp[a[i] + i];
  }

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

Buggy submission (status: Time Limit Exceeded):
// https://atcoder.jp/contests/abc166/tasks/abc166_e
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
const ll LINF = 0x1fffffffffffffff;
#define FOR(i, a, b) for (ll i = (a); i < static_cast<ll>(b); ++i)
#define REP(i, n) FOR(i, 0, n)
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;
}
int main() {
  ll N;
  cin >> N;
  vector<ll> an(N + 1);
  FOR(i, 1, N + 1) { cin >> an.at(i); }
  ll ans = 0;
  FOR(i, 1, N + 1) {
    FOR(j, i + 1, N + 1) {
      ll v = abs(j - i);
      if (an[i] + an[j] == v) {
        ++ans;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
// https://atcoder.jp/contests/abc166/tasks/abc166_e
#include <bits/stdc++.h>
#if LOCAL
#include "dump.hpp"
#else
#define dump(...)
#endif
using namespace std;
using ll = long long;
const ll LINF = 0x1fffffffffffffff;
#define FOR(i, a, b) for (ll i = (a); i < static_cast<ll>(b); ++i)
#define REP(i, n) FOR(i, 0, n)
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;
}
int main() {
#if LOCAL & 01
  std::ifstream in("./test/sample-1.in");
  // std::ifstream in("./input.txt");
  std::cin.rdbuf(in.rdbuf());
#else
  cin.tie(0);
  ios::sync_with_stdio(false);
#endif
  ll N;
  cin >> N;
  vector<ll> an(N + 1);
  struct Info {
    ll a;
    ll ij;
    Info(ll ia, ll ii) : a(ia), ij(ii) {}
  };
  unordered_map<ll, vector<Info>> itbl, jtbl;
  FOR(i, 1, N + 1) {
    ll a;
    cin >> a;
    an.at(i) = a;
    auto aii = i + a;
    itbl[aii].push_back(Info(a, i));
    auto jaj = i - a;
    jtbl[jaj].push_back(Info(a, i));
  }

  ll ans = 0;
  for (auto &aij : itbl) {
    auto it = jtbl.find(aij.first);
    if (it != jtbl.end()) {
      ans += (aij.second.size() * it->second.size());
    }
  }

  cout << ans << endl;

  return 0;
}

int main_() {
  ll N;
  cin >> N;
  vector<ll> an(N + 1);
  FOR(i, 1, N + 1) { cin >> an.at(i); }
  ll ans = 0;
  FOR(i, 1, N + 1) {
    FOR(j, i + 1, N + 1) {
      ll v = abs(j - i);
      if (an[i] + an[j] == v) {
        ++ans;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

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

typedef long long ll;
typedef long double ld;
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  ll ans = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + A.at(i) + 1; j < N; j++) {
      if (j - i == A.at(i) + A.at(j)) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}

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

typedef long long ll;
typedef long double ld;
#define pi 3.14159265359
#define inf 2147483647
#define INF 9223372036854775807
#define mod 1000000007
#define mod2 998244353

int main() {
  int N;
  cin >> N;
  vector<ll> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  ll ans = 0;
  map<ll, ll> dist, sum;
  for (int i = 0; i < N; i++) {
    ll d = i - A.at(i);
    ll s = i + A.at(i);
    if (dist.count(d)) {
      dist.at(d)++;
    } else {
      dist[d] = 1;
    }
    if (sum.count(s)) {
      sum.at(s)++;
    } else {
      sum[s] = 1;
    }
  }
  for (int i = 0; i < N; i++) {
    if (dist.count(i) && sum.count(i)) {
      ans += dist.at(i) * sum.at(i);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<ll> V(n);
  REP(i, n) { V[i] = a[i] + i + 1; }
  vector<pair<ll, ll>> v(n);
  REP(i, n) {
    v[i].first = a[i] - i - 1;
    v[i].second = i;
  }
  sort(v.begin(), v.end());
  ll ans = 0;
  REP(i, n) {
    auto it1 = lower_bound(v.begin(), v.end(), pair<ll, ll>(-V[i], -1));
    if (it1 == v.end())
      continue;
    if (it1->first == -V[i]) {
      auto it2 = upper_bound(v.begin(), v.end(), pair<ll, ll>(-V[i], n + 10));
      ans += it2 - it1;
      FOR(j, it1 - v.begin(), it2 - v.begin()) {
        if (v[j].second == i)
          ans--;
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, n) for (ll i = 0; i < n; i++)
#define REPR(i, n) for (ll i = n - 1; i >= 0; i--)

typedef long long ll;
#define INF 1e18

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  REP(i, n) cin >> a[i];
  vector<ll> V(n);
  REP(i, n) { V[i] = a[i] + i + 1; }
  vector<pair<ll, ll>> v(n);
  REP(i, n) {
    v[i].first = a[i] - i - 1;
    v[i].second = i;
  }
  sort(v.begin(), v.end());
  ll ans = 0;
  REP(i, n) {
    auto it1 = lower_bound(v.begin(), v.end(), pair<ll, ll>(-V[i], -1));
    if (it1 == v.end())
      continue;
    if (it1->first == -V[i]) {
      auto it2 = upper_bound(v.begin(), v.end(), pair<ll, ll>(-V[i], n + 10));
      ans += it2 - it1;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define N 100123
#define endl '\n'

using namespace std;

ll v[N], foward[N], ans, n;

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n;

  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
  }

  for (int i = 1; i <= n; ++i) {
    if (i + v[i] <= n)
      foward[i + v[i]]++;
  }

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

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define N 200123
#define endl '\n'

using namespace std;

ll v[N], foward[N], ans, n;

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n;

  for (int i = 1; i <= n; ++i) {
    cin >> v[i];
  }

  for (int i = 1; i <= n; ++i) {
    if (i + v[i] <= n)
      foward[i + v[i]]++;
  }

  for (int i = n; i >= 1; --i) {
    if (i - v[i] > 0) {
      ans += foward[i - v[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;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9 + 1;
const ll MOD = 1e9 + 7;
const double PI =
    3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799;

int n;
map<ll, ll> mp;
map<ll, ll> mpp;
ll ans = 0;
int a[100000];

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    mp[i - a[i - 1]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (mp.count(a[i - 1] + i))
      mpp[a[i - 1] + i]++;
  }
  for (pair<ll, ll> x : mp) {
    // cout << x.first << " " <<x.second <<endl;
    ans += mpp[x.first] * x.second;
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define m1(x) memset(x, -1, sizeof(x))
const int INF = 1e9 + 1;
const ll MOD = 1e9 + 7;
const double PI =
    3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799;

int n;
map<ll, ll> mp;
map<ll, ll> mpp;
ll ans = 0;
int a[200000];

int main() {
  cin >> n;
  rep(i, n) cin >> a[i];
  for (int i = 1; i <= n; i++) {
    mp[i - a[i - 1]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (mp.count(a[i - 1] + i))
      mpp[a[i - 1] + i]++;
  }
  for (pair<ll, ll> x : mp) {
    // cout << x.first << " " <<x.second <<endl;
    ans += mpp[x.first] * x.second;
  }
  cout << ans << endl;
}
Problem p02691 (C++)

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

ll n, a[100001], ans;
map<ll, ll> mp;

int main() {

  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    mp[i + a[i]]++;
    ans += mp[i - a[i]];
  }
  cout << ans;
}


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

ll n, a[200001], ans;
map<ll, ll> mp;

int main() {

  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    mp[i + a[i]]++;
    ans += mp[i - a[i]];
  }
  cout << ans;
}
Problem p02691 (C++)

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

using namespace std;

#define INF (1 << 30)
#define EPS 1e-10
#define MOD 1000000007

#define rep(i, n) FOR(i, 0, n)
#define FOR(i, x, n) for (int i = (x); i < (n); ++i)
#define all(v) (v).begin(), (v).end()

using ll = long long;

template <class T> void chmax(T &a, T b) { a > b ?: a = b; }
template <class T> void chmin(T &a, T b) { a < b ?: a = b; }

int a[100001], b[100000];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    b[i - 1] = i - a[i];
  }
  sort(b, b + n);
  ll ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += upper_bound(b, b + n, a[i] + i) - lower_bound(b, b + n, a[i] + i);
  }
  cout << ans << endl;

  return 0;
}

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

using namespace std;

#define INF (1 << 30)
#define EPS 1e-10
#define MOD 1000000007

#define rep(i, n) FOR(i, 0, n)
#define FOR(i, x, n) for (int i = (x); i < (n); ++i)
#define all(v) (v).begin(), (v).end()

using ll = long long;

template <class T> void chmax(T &a, T b) { a > b ?: a = b; }
template <class T> void chmin(T &a, T b) { a < b ?: a = b; }

int a[200001], b[200000];

int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    b[i - 1] = i - a[i];
  }
  sort(b, b + n);
  ll ans = 0;
  for (int i = 1; i <= n; ++i) {
    ans += upper_bound(b, b + n, a[i] + i) - lower_bound(b, b + n, a[i] + i);
  }
  cout << ans << endl;

  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const long long int LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
#define NMAX 100005

ll tab[NMAX];
ll tabdiff[NMAX];
ll tabsum[NMAX];
int main() {
  int n;
  ll count = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> tab[i];
    if (i - tab[i] >= 0) {
      tabdiff[i - tab[i]]++;
    }
    if (i + tab[i] < n) {
      tabsum[i + tab[i]]++;
    }
  }

  for (int i = 0; i < NMAX; ++i) {
    count += tabsum[i] * tabdiff[i];
  }

  cout << count << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const long long int LLINF = 0x3f3f3f3f3f3f3f3f;
const double EPS = 1e-9;
#define NMAX 100005 * 2

ll tab[NMAX];
ll tabdiff[NMAX];
ll tabsum[NMAX];
int main() {
  int n;
  ll count = 0;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> tab[i];
    if (i - tab[i] >= 0) {
      tabdiff[i - tab[i]]++;
    }
    if (i + tab[i] < n) {
      tabsum[i + tab[i]]++;
    }
  }

  for (int i = 0; i < NMAX; ++i) {
    count += tabsum[i] * tabdiff[i];
  }

  cout << count << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;

// --------------------------------------
int n;
int a[100005];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  REP(i, n) cin >> a[i];

  vector<int> sum(n, 0);

  ll ans = 0;
  REP(i, n) {
    if (i - a[i] >= 0)
      ans += sum[i - a[i]];
    if (i + a[i] < n)
      sum[i + a[i]]++;
  }

  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define RREP(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, k, n) for (int i = (k); i < (int)(n); i++)
#define all(i, n) (i), (i + n)

int dx4[4] = {1, 0, -1, 0};
int dy4[4] = {0, -1, 0, 1};
int dx8[8] = {1, 0, -1, 1, -1, 1, 0, -1};
int dy8[8] = {1, 1, 1, 0, 0, -1, -1, -1};

typedef pair<int, int> P;
typedef pair<string, int> SP;
typedef long long ll;
typedef pair<ll, ll> PLL;

const int INF = 1e9;
const ll LLINF = 1e18;
const int MAX_V = 1e6 + 1;
const ll mod = 1000000007;

// --------------------------------------
int n;
int a[200005];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n;
  REP(i, n) cin >> a[i];

  vector<int> sum(n, 0);

  ll ans = 0;
  REP(i, n) {
    if (i - a[i] >= 0)
      ans += sum[i - a[i]];
    if (i + a[i] < n)
      sum[i + a[i]]++;
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 1e5 + 5;
const ll maxm = 1e3 + 5;
template <class T> inline T qread(T &IEE) {
  register T x = 0, f = 1;
  register char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') {
      f = -f;
    }
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return IEE = x * f;
}
template <class T> inline void qwrite(T a) {
  if (!a) {
    putchar('0');
    return;
  }
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a > 9)
    qwrite(a / 10);
  putchar(a % 10 + 48);
  return;
}
template <class T> inline T ab(T a) { return (a > 0) ? a : -a; }
template <class T> inline void swa(T &a, T &b) {
  a ^= b ^= a ^= b;
  return;
}
ll cnt, n;
ll a[maxn];
map<ll, ll> miu;
int main() {
  //	freopen("1.in", "r", stdin);
  //	freopen("1.out", "w", stdout);
  qread(n);
  for (int i = 1; i <= n; i++) {
    qread(a[i]);
    miu[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    miu[i - a[i]]--;
    cnt += miu[a[i] + i];
  }
  cout << cnt << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll maxn = 2e5 + 5;
const ll maxm = 1e3 + 5;
template <class T> inline T qread(T &IEE) {
  register T x = 0, f = 1;
  register char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') {
      f = -f;
    }
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 1) + (x << 3) + (ch ^ 48);
    ch = getchar();
  }
  return IEE = x * f;
}
template <class T> inline void qwrite(T a) {
  if (!a) {
    putchar('0');
    return;
  }
  if (a < 0) {
    putchar('-');
    a = -a;
  }
  if (a > 9)
    qwrite(a / 10);
  putchar(a % 10 + 48);
  return;
}
template <class T> inline T ab(T a) { return (a > 0) ? a : -a; }
template <class T> inline void swa(T &a, T &b) {
  a ^= b ^= a ^= b;
  return;
}
ll cnt, n;
ll a[maxn];
map<ll, ll> miu;
int main() {
  //	freopen("1.in", "r", stdin);
  //	freopen("1.out", "w", stdout);
  qread(n);
  for (int i = 1; i <= n; i++) {
    qread(a[i]);
    miu[i - a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    miu[i - a[i]]--;
    cnt += miu[a[i] + i];
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll ans;
int main() {
  ll n, a[100005];
  cin >> n;
  map<ll, ll> m;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    m[i - a[i]]++;
  }
  for (ll i = 1; i <= n; i++) {
    ll x = a[i] + i;
    ans = ans + m[x];
  }
  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll ans;
int main() {
  ll n, a[200005];
  cin >> n;
  map<ll, ll> m;
  for (ll i = 1; i <= n; i++) {
    cin >> a[i];
    m[i - a[i]]++;
  }
  for (ll i = 1; i <= n; i++) {
    ll x = a[i] + i;
    ans = ans + m[x];
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int MOD = 1e9 + 7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct edge {
  ll to, cost;
};

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

// 素数
vector<bool> IsPrime;
void sieve(size_t max) {
  if (max + 1 > IsPrime.size()) {
    IsPrime.resize(max + 1, true);
  }
  IsPrime[0] = false;
  IsPrime[1] = false;

  for (size_t i = 2; i * i <= max; ++i) {
    if (IsPrime[i]) {
      for (size_t j = 2; i * j <= max; ++j) {
        IsPrime[i * j] = false;
      }
    }
  }
}

// べき乗の余り
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n) {
    if (n & 1) {
      (res *= a) %= mod;
    } else {
      (a *= a) %= mod;
    }
    n >>= 1;
  }
  return res;
}

// 回文
bool kai(string S) {
  bool flag = true;
  for (ll i = 0; i < S.size() / 2; ++i) {
    if (S[i] != S[S.size() - i - 1]) {
      flag = false;
      break;
    }
  }
  return flag;
}
// ---------------------------------------------

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  ll ans = 0;
  map<ll, ll> ma;
  for (ll i = 1; i <= n; ++i) {
    cin >> a[i];
    ma[a[i] + i]++;
    ans += ma[i - a[i]];
  }
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
typedef long double ld;
using namespace std;
const long long int INF = 1e18;
const long long int MOD = 1e9 + 7;

typedef pair<ll, ll> pairs;
typedef vector<pairs> p;

struct edge {
  ll to, cost;
};

ll gcd(ll a, ll b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

ll keta(ll N) {
  int tmp{};
  while (N > 0) {
    tmp += (N % 10);
    N /= 10;
  }
  N = tmp;
  return N;
}

// 素数
vector<bool> IsPrime;
void sieve(size_t max) {
  if (max + 1 > IsPrime.size()) {
    IsPrime.resize(max + 1, true);
  }
  IsPrime[0] = false;
  IsPrime[1] = false;

  for (size_t i = 2; i * i <= max; ++i) {
    if (IsPrime[i]) {
      for (size_t j = 2; i * j <= max; ++j) {
        IsPrime[i * j] = false;
      }
    }
  }
}

// べき乗の余り
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n) {
    if (n & 1) {
      (res *= a) %= mod;
    } else {
      (a *= a) %= mod;
    }
    n >>= 1;
  }
  return res;
}

// 回文
bool kai(string S) {
  bool flag = true;
  for (ll i = 0; i < S.size() / 2; ++i) {
    if (S[i] != S[S.size() - i - 1]) {
      flag = false;
      break;
    }
  }
  return flag;
}
// ---------------------------------------------

int main() {
  ll n;
  cin >> n;
  vector<ll> a(2 * 1e5 + 10);
  ll ans = 0;
  map<ll, ll> ma;
  for (ll i = 1; i <= n; ++i) {
    cin >> a[i];
    ma[a[i] + i]++;
    ans += ma[i - a[i]];
  }
  cout << ans << endl;

  return 0;
}