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

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

// const i64 mod = 1000000007ll;
const i64 smod = 998244353ll;
const i64 inf = 1'000'000'000'000'000'000ll + 7;
// const i64 inf = 1'000'000'007;

const long double eps = 1e-8;

const i64 MAXN = 2 * 100'000 + 5;

i64 n;
i64 a[MAXN];
i64 c[MAXN];

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);

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

  for (i64 i = 2; i <= n; i++) {
    c[i - a[i]] += 1;
  }

  i64 R = 0;
  for (i64 i = 1; i < n; i++) {
    R += c[a[i] + i];
    c[i + 1 - a[i + 1]] -= 1;
  }

  cout << R << endl;

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

// const i64 mod = 1000000007ll;
const i64 smod = 998244353ll;
const i64 inf = 1'000'000'000'000'000'000ll + 7;
// const i64 inf = 1'000'000'007;

const long double eps = 1e-8;

const i64 MAXN = 2 * 100'000 + 5;

i64 n;
i64 a[MAXN];
map<i64, i64> c;

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);

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

  for (i64 i = 2; i <= n; i++) {
    c[i - a[i]] += 1;
  }

  i64 R = 0;
  for (i64 i = 1; i < n; i++) {
    R += c[a[i] + i];
    c[i + 1 - a[i + 1]] -= 1;
  }

  cout << R << endl;

  return 0;
}
Problem p02691 (C++)

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

int main() {
  ll n;
  cin >> n;
  ll h[n];
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  vector<ll> sa;
  for (ll j = 0; j < n; ++j) {
    sa.push_back(j - h[j]);
  }
  ll ans = 0;
  sort(sa.begin(), sa.end());
  for (ll k = 0; k < n; ++k) {
    ans += upper_bound(sa.begin(), sa.end(), k + h[k]) -
           lower_bound(sa.begin(), sa.end(), k + h[k]);
    sa.erase(lower_bound(sa.begin(), sa.end(), k - h[k]));
  }
  cout << ans << endl;
  return 0;
}


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

int main() {
  ll n;
  cin >> n;
  ll h[n];
  for (int i = 0; i < n; ++i) {
    cin >> h[i];
  }
  vector<ll> sa;
  for (ll j = 0; j < n; ++j) {
    sa.push_back(j - h[j]);
  }
  ll ans = 0;
  sort(sa.begin(), sa.end());
  for (ll k = 0; k < n; ++k) {
    ans += upper_bound(sa.begin(), sa.end(), k + h[k]) -
           lower_bound(sa.begin(), sa.end(), k + h[k]);
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define inf 0x7f7f7f7f7f7f7f7f
#define maxn 1000010
#define ri register int
#define il inline
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define mod 998244353
#define db(x) cout << x << endl;
#define eps 0.0001
using namespace std;
int a[maxn], b[maxn], c[maxn];
int s[maxn];
signed main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    b[a[i] + i]++;
    //	cout << i << " " << a[i]+i<<endl;
    if (i - a[i] > 0)
      c[i - a[i]]++;
  }
  int ans = 0;
  for (int i = 1; i <= 200000; i++)
    if (b[i]) {
      ans += b[i] * c[i];
    }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define ld long double
#define inf 0x7f7f7f7f7f7f7f7f
#define maxn 1000010
#define ri register int
#define il inline
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define mod 998244353
#define db(x) cout << x << endl;
#define eps 0.0001
using namespace std;
int a[maxn], b[maxn], c[maxn];
int s[maxn];
signed main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] + i <= 200000)
      b[a[i] + i]++;
    //	cout << i << " " << a[i]+i<<endl;
    if (i - a[i] > 0)
      c[i - a[i]]++;
  }
  int ans = 0;
  for (int i = 1; i <= 200000; i++)
    if (b[i]) {
      ans += b[i] * c[i];
    }
  cout << ans;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
/*input
32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
*/
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define mod 998244353
#define int long long
#define N 100005

int H[N];
map<int, int> ok;
signed main() {
  int n, m, ans = 0;
  cin >> n; //>>m;
  // memset(ok,1,sizeof(ok));
  for (int i = 1; i <= n; i++) {
    cin >> H[i];
    int reqd = i - H[i];

    ok[i + H[i]]++;
    if (reqd >= 0)
      ans += ok[reqd];
  }
  cout << ans << endl;
}

Fixed submission:
/*input
32
3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5
*/
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define mod 998244353
#define int long long
#define N 200005

int H[N];
map<int, int> ok;
signed main() {
  int n, m, ans = 0;
  cin >> n; //>>m;
  // memset(ok,1,sizeof(ok));
  for (int i = 1; i <= n; i++) {
    cin >> H[i];
    int reqd = i - H[i];

    ok[i + H[i]]++;
    if (reqd >= 0)
      ans += ok[reqd];
  }
  cout << ans << endl;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define INF 2147483647
#define LLINF 9223372036854775807
using namespace std;
map<int, int> mp;
int a[100010];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i] + i]++;
  }
  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    if (i - a[i] >= 1) {
      cnt += mp[i - a[i]];
    }
  }
  cout << cnt << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define forn(i, a, b) for (int i = a; i <= b; i++)
#define INF 2147483647
#define LLINF 9223372036854775807
using namespace std;
map<int, int> mp;
int a[200010];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    mp[a[i] + i]++;
  }
  long long cnt = 0;
  for (int i = 0; i < n; i++) {
    if (i - a[i] >= 1) {
      cnt += mp[i - a[i]];
    }
  }
  cout << cnt << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N;
  cin >> N;
  vector<ll> data(N);
  for (ll i = 0; i < N; i++) {
    cin >> data[i];
  }
  vector<vector<int>> fi(N, vector<int>(N, 0));
  ll count = 0;
  for (ll i = 0; i < N; i++) {
    for (ll j = i + 1; j < N; j++) {
      if (data[i] + data[j] == abs(i - j))
        count++;
    }
  }
  cout << count << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll N;
  cin >> N;
  vector<ll> T(N);
  for (int i = 0; i < N; i++)
    cin >> T[i];
  map<ll, ll> mp;
  for (ll i = 0; i < N; i++)
    mp[i + T[i]]++;
  ll res = 0;
  for (ll i = 0; i < N; i++)
    res += mp[i - T[i]];
  cout << res << endl;
}
Problem p02691 (C++)

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

using namespace std;

const int Nmax = 2e5 + 10;
const int Mod = 1e9 + 7;
const int oo = 1e9;

int n, m;
int vf[Nmax];

int main() {
  /**
      #ifndef ONLINE_JUDGE

          ifstream cin("test.in");

      #endif // ONLINE_JUDGE
  */

  cin >> n;

  long long ans = 0, x;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    ans += vf[(i - x > 0 ? i - x : 0)];
    vf[x + i]++;
  }

  cout << ans;
  return 0;
}


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

using namespace std;

const int Nmax = 2e5 + 10;
const int Mod = 1e9 + 7;
const int oo = 1e9;

int n, m;
map<int, int> vf;

int main() {
  /**
      #ifndef ONLINE_JUDGE

          ifstream cin("test.in");

      #endif // ONLINE_JUDGE
  */

  cin >> n;

  long long ans = 0, x;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    ans += vf[(i - x > 0 ? i - x : 0)];
    vf[x + i]++;
  }

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

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

const int maxn = 1e5 + 100;
int n;
int a[maxn];
map<int, int> b;
// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  // Failed to predict input format
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    b[i - a[i]]++;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += b[a[i] + i];
    b[i - a[i]]--;
  }
  cout << ans << endl;
  return 0;
}


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

const int maxn = 2e5 + 100;
int n;
int a[maxn];
map<int, int> b;
// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You
// use the default template now. You can remove this line by using your custom
// template)
int main() {
  // Failed to predict input format
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    b[i - a[i]]++;
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += b[a[i] + i];
    b[i - a[i]]--;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

int64_t b[200000000] = {0};
int64_t a[2000000];
int main() {
  int64_t n;
  scanf("%lld", &n);

  int64_t i;
  int64_t sum = 0;
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] <= i)
      b[(i - a[i])]++;
  }
  for (i = 1; i <= n; i++)
    sum = sum + b[a[i] + i];

  cout << sum << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <stdio.h>
using namespace std;

int64_t b[200000000] = {0};
int64_t a[2000000];
int main() {
  int64_t n;
  scanf("%lld", &n);

  int64_t i;
  int64_t sum = 0;
  for (i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    if (a[i] <= i)
      b[(i - a[i])]++;
  }
  for (i = 1; i <= n; i++)
    if (a[i] + i < 2000000)
      sum = sum + b[a[i] + i];

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i <= n; i++)
#define per(i, n, x) for (int i = n; i >= x; i--)
#define sz(a) int(a.size())
#define rson mid + 1, r, p << 1 | 1
#define pii pair<int, int>
#define lson l, mid, p << 1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const int inf = 1e9;
int n, a[N];
map<int, int> cnt;
int main() {
  // ios::sync_with_stdio(false);
  // freopen("in","r",stdin);
  cin >> n;
  ll ans = 0;
  rep(i, 1, n) {
    cin >> a[i];
    ans += cnt[i - a[i]];
    cnt[i + a[i]]++;
  }
  cout << ans << endl;
  return 0;
}
// j+a[j]==i-a[i];

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define rep(i, x, n) for (int i = x; i <= n; i++)
#define per(i, n, x) for (int i = n; i >= x; i--)
#define sz(a) int(a.size())
#define rson mid + 1, r, p << 1 | 1
#define pii pair<int, int>
#define lson l, mid, p << 1
#define ll long long
#define pb push_back
#define mp make_pair
#define se second
#define fi first
using namespace std;
const double eps = 1e-8;
const int mod = 1e9 + 7;
const int N = 2e5 + 10;
const int inf = 1e9;
int n, a[N];
map<int, int> cnt;
int main() {
  // ios::sync_with_stdio(false);
  // freopen("in","r",stdin);
  cin >> n;
  ll ans = 0;
  rep(i, 1, n) {
    cin >> a[i];
    ans += cnt[i - a[i]];
    cnt[i + a[i]]++;
  }
  cout << ans << endl;
  return 0;
}
// j+a[j]==i-a[i];
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_ll(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long u_ll;
typedef pair<int, int> pair_;
const long double PI = (acos(-1));
const int INF = 1001001001;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
ll mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll cnt = 0;

  rep(i, n) {
    for (int j = i + a[i] + 1; j < n; j++) {
      if (a[i] + a[j] == j - i)
        cnt++;
    }
  }
  cout << cnt;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_ll(i, n) for (ll i = 0; i < (ll)(n); i++)
using namespace std;
typedef long long ll;
typedef unsigned long long u_ll;
typedef pair<int, int> pair_;
const long double PI = (acos(-1));
const int INF = 1001001001;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
ll mod = 1000000007;
int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll cnt = 0;
  map<int, int> mp;
  rep(j, n) {
    int j_aj = j - a[j];
    int i_p_ai = j + a[j];
    cnt += mp[j_aj];
    mp[i_p_ai]++;
  }
  cout << cnt;
}
Problem p02691 (C++)

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

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

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> l(n), r(n);
  vector<int> d(n, 0);
  rep(i, n) {
    l[i] = i + a[i];
    r[i] = i - a[i];
    if (r[i] > 0)
      d[r[i]] += 1;
  }
  int ans = 0;
  rep(i, n) { ans += d[l[i]]; }
  cout << ans << endl;
  return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> l(n), r(n);
  vector<int> d(n, 0);
  rep(i, n) {
    l[i] = i + a[i];
    r[i] = i - a[i];
    if (r[i] > 0)
      d[r[i]] += 1;
  }
  ll ans = 0;
  rep(i, n) {
    if (l[i] < n)
      ans += d[l[i]];
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define ll long long int
const ll mod = 20123;
using namespace std;
ll n, m;
string s;
ll a[100010];
map<ll, ll> p1, p2;
int main() {
  ios::sync_with_stdio(false);
  ll i = 0, j, flat1 = 0, flat2 = 0, x, k, y, z, t;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    p1[a[i] + i]++;
    p2[i - a[i]]++;
  }
  map<ll, ll>::iterator it;
  ll sum = 0;
  for (it = p1.begin(); it != p1.end(); it++) {
    sum += (it->second) * p2[it->first];
  }
  cout << sum;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#define ll long long int
const ll mod = 20123;
using namespace std;
ll n, m;
string s;
ll a[200010];
map<ll, ll> p1, p2;
int main() {
  ios::sync_with_stdio(false);
  ll i = 0, j, flat1 = 0, flat2 = 0, x, k, y, z, t;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    p1[a[i] + i]++;
    p2[i - a[i]]++;
  }
  map<ll, ll>::iterator it;
  ll sum = 0;
  for (it = p1.begin(); it != p1.end(); it++) {
    sum += (it->second) * p2[it->first];
  }
  cout << sum;
  return 0;
}
Problem p02691 (C++)

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

//    int n=132;
//    bitset<16>binar(n);
//    int a = bitset<16>(binar).to_ulong();
//    cout<<a;

#define int long long
#define pb push_back
#define rep(i, b) for (int i = 0; i < b; i++)
#define all(c) (c).begin(), (c).end()

using namespace std;

signed main() {
  int n;
  cin >> n;
  vector<int> a;
  rep(i, n) { cin >> a[i]; }
  map<int, int> m;
  int val = 0;
  rep(i, n) {
    val = val + m[i - a[i]];
    m[i + a[i]]++;
  }
  cout << val;
}


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

//    int n=132;
//    bitset<16>binar(n);
//    int a = bitset<16>(binar).to_ulong();
//    cout<<a;

#define int long long
#define pb push_back
#define rep(i, b) for (int i = 0; i < b; i++)
#define all(c) (c).begin(), (c).end()

using namespace std;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) { cin >> a[i]; }
  map<int, int> m;
  int val = 0;
  rep(i, n) {
    val = val + m[i - a[i]];
    m[i + a[i]]++;
  }
  cout << val;
}
Problem p02691 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define rep(N) for (int i = 0; i < N; i++)
#define rep2(N1, N2)                                                           \
  for (int i = 0; i < N1; i++)                                                 \
    for (int j = 0; j < N2; j++)
#define rep3(N1, N2, N3)                                                       \
  for (int i = 0; i < N1; i++)                                                 \
    for (int j = 0; j < N2; j++)                                               \
      for (int k = 0; k < N3; k++)
#define For(i, N) for (int i = 0; i < N; i++)
#define ll long long int
void yn(bool b) {
  if (b)
    printf("yes\n");
  else
    printf("no\n");
}

void Yn(bool b) {
  if (b)
    printf("Yes\n");
  else
    printf("No\n");
}

void YN(bool b) {
  if (b)
    printf("YES\n");
  else
    printf("NO\n");
}

int ctoi(char c) { return c - '0'; }

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(N) cin >> A[i];
  vector<ll> WA(N, 0);
  vector<ll> SA(N, 0);
  map<ll, ll> M, K;
  ll ans = 0;
  ll mi = 1000000000000;
  ll ma = 0;
  for (int i = 0; i < N; i++) {
    WA[i] = A[i] + i + 1;
    SA[i] = -A[i] + i + 1;
    M[WA[i]]++;
    K[SA[i]]++;
    ma = max(SA[i], ma);
    mi = min(SA[i], mi);
  }
  for (ll i = mi; i <= ma; i++)
    ans += M[i] * K[i];
  cout << ans;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define rep(N) for (int i = 0; i < N; i++)
#define rep2(N1, N2)                                                           \
  for (int i = 0; i < N1; i++)                                                 \
    for (int j = 0; j < N2; j++)
#define rep3(N1, N2, N3)                                                       \
  for (int i = 0; i < N1; i++)                                                 \
    for (int j = 0; j < N2; j++)                                               \
      for (int k = 0; k < N3; k++)
#define For(i, N) for (int i = 0; i < N; i++)
#define ll long long int
void yn(bool b) {
  if (b)
    printf("yes\n");
  else
    printf("no\n");
}

void Yn(bool b) {
  if (b)
    printf("Yes\n");
  else
    printf("No\n");
}

void YN(bool b) {
  if (b)
    printf("YES\n");
  else
    printf("NO\n");
}

int ctoi(char c) { return c - '0'; }

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  rep(N) cin >> A[i];
  vector<ll> WA(N, 0);
  vector<ll> SA(N, 0);
  map<ll, ll> M, K;
  ll ans = 0;
  ll mi = 1000000000000;
  ll ma = 0;
  for (int i = 0; i < N; i++) {
    WA[i] = A[i] + i + 1;
    SA[i] = -A[i] + i + 1;
    M[WA[i]]++;
    K[SA[i]]++;
    ma = max(SA[i], ma);
    mi = min(SA[i], mi);
  }
  for (auto it = M.begin(); it != M.end(); it++)
    ans += it->second * K[it->first];
  cout << ans;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
#define endl "\n"
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;

ll A[200010];
ll L[200010], R[200010];

int main() {
  ll n;
  cin >> n;
  Rep(i, 1, n) cin >> A[i];
  Rep(i, 1, n) {
    L[A[i] + i]++;
    if (i - A[i] >= 1)
      R[i - A[i]]++;
  }
  ll ans = 0;
  Rep(i, 1, n) { ans += L[i] * R[i]; }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define _GLIBCXX_DEBUG
#define Vl vector<ll>
#define Vs vector<string>
#define Vp vector<pair<ll, ll>>
#define ll long long
#define ALL(v) (v).begin(), (v).end()
#define endl "\n"
using namespace std;
const double pi = acos(-1.0);
const ll MOD = 1e9 + 7;
const ll INF = 1LL << 60;

ll A[200010];
ll L[200010], R[200010];

int main() {
  ll n;
  cin >> n;
  Rep(i, 1, n) cin >> A[i];
  Rep(i, 1, n) {
    if (A[i] + i <= n)
      L[A[i] + i]++;
    if (i - A[i] >= 1)
      R[i - A[i]]++;
  }
  ll ans = 0;
  Rep(i, 1, n) { ans += L[i] * R[i]; }

  cout << ans << endl;

  return 0;
}
Problem p02691 (C++)

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

#define int long long
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define COUT(x) cout << x << endl;
#define PB(x) push_back(x)
#define MP make_pair
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int res = 0;
  for (int i = 1; i < n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (a[i] + a[j] == (j - i))
        res++;
    }
  }
  cout << res << endl;
}

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

#define int long long
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define COUT(x) cout << x << endl;
#define PB(x) push_back(x)
#define MP make_pair
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }

  int res = 0;
  map<int, int> map;
  for (int i = 1; i <= n; i++) {
    map[a[i] + i]++;
  }
  for (int i = 1; i <= n; i++) {
    if (i - a[i] > 0)
      res += map[i - a[i]];
  }
  cout << res << endl;
}
Problem p02691 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    _ = input()
    lst_a = [int(a) for a in input().strip().split(" ")]

    # build hash table
    hash_tbl = {}
    for i, a_i in enumerate(lst_a):
        n_i = i + 1
        tmp = int(n_i - a_i)
        if tmp > 0:
            if tmp in hash_tbl.keys():
                hash_tbl[tmp].append(n_i)
            else:
                hash_tbl[tmp] = [n_i]

    ans = 0
    for j, a_j in enumerate(lst_a):
        n_j = j + 1
        tmp = int(n_j + a_j)
        if tmp in hash_tbl.keys():
            ans += len([idx for idx in hash_tbl[tmp] if idx > j])
    print(int(ans))


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    _ = input()
    lst_a = [int(a) for a in input().strip().split(" ")]

    # build hash table
    hash_tbl = {}
    for i, a_i in enumerate(lst_a):
        n_i = i + 1
        tmp = int(n_i - a_i)
        if tmp > 0:
            if tmp in hash_tbl.keys():
                hash_tbl[tmp].append(n_i)
            else:
                hash_tbl[tmp] = [n_i]

    ans = 0
    for j, a_j in enumerate(lst_a):
        n_j = j + 1
        tmp = int(n_j + a_j)
        if tmp in hash_tbl.keys():
            # ans += len([idx for idx in hash_tbl[tmp] if idx > j])
            ans += len(hash_tbl[tmp])
    print(int(ans))


if __name__ == "__main__":
    main()
Problem p02691 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))

ans = 0
for i in range(n - 1):
    for j in range(i + 1, n):
        if j - i == a[i] + a[j]:
            ans += 1
print(ans)


Fixed submission:
n = int(input())
a = list(map(int, input().split()))

ans = 0
dic1 = {}
dic2 = {}
for i in range(n):
    tmp1 = a[i] + (i + 1)
    tmp2 = -a[i] + (i + 1)
    if tmp1 in dic1:
        dic1[tmp1] += 1
    else:
        dic1[tmp1] = 1
    if tmp2 in dic2:
        dic2[tmp2] += 1
    else:
        dic2[tmp2] = 1
for key in dic1.keys():
    if key in dic2:
        ans += dic1[key] * dic2[key]
print(ans)
Problem p02691 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
As = list(map(int, input().split()))

i_plus_Ai = [0 for i in range(N)]
dict = {}
for i in range(N):
    i_plus_Ai[i] = i + As[i]
    tmp = i - As[i]
    if tmp not in dict:
        dict[tmp] = [i]
    else:
        dict[tmp].append(i)
    dict[tmp].sort()

count = 0
for i in range(N):
    if i_plus_Ai[i] in dict:
        kouho = dict[i_plus_Ai[i]]
        for j in range(len(kouho)):
            if kouho[j] > i:
                count += len(kouho) - j
                break

print(count)


Fixed submission:
N = int(input())
As = list(map(int, input().split()))

i_plus_Ai = [0 for i in range(N)]
dict = {}
for i in range(N):
    i_plus_Ai[i] = i + As[i]
    tmp = i - As[i]
    if tmp not in dict:
        dict[tmp] = [i]
    else:
        dict[tmp].append(i)

count = 0
for i in range(N):
    if i_plus_Ai[i] in dict:
        kouho = dict[i_plus_Ai[i]]
        for j in range(len(kouho)):
            if kouho[j] > i:
                count += len(kouho) - j
                break

print(count)
Problem p02691 (Python)

Buggy submission (status: Runtime Error):
from collections import defaultdict


n = int(input())
a = list(map(int, input().split()))

memo = defaultdict(int)
ans = 0
for i, x in enumerate(a, 1):
    print(memo)
    ans += memo[i - x]
    memo[x + i] += 1

print(ans)


Fixed submission:
from collections import defaultdict


n = int(input())
a = list(map(int, input().split()))

memo = defaultdict(int)
ans = 0
for i, x in enumerate(a, 1):
    ans += memo[i - x]
    memo[x + i] += 1

print(ans)
Problem p02691 (Python)

Buggy submission (status: Runtime Error):
# class Map(dict):
#     def __missing__(self, key):
#         # この割当てなしのほうが速い。
#         # self[key] = 0
#         return 0


def main():
    int(input())

    d = {}
    ans = 0
    for i, height in enumerate(input().split()):
        height = int(height)
        # 引き算のほうの i は足し算のよりも大きい。
        # 従って，このように順番に見ていく必要がある。
        got = d.get(i + height) or 0
        d[i + height] += got + 1
        ans += d.get(i - height) or 0

    return ans


if __name__ == "__main__":
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030


Fixed submission:
# class Map(dict):
#     def __missing__(self, key):
#         # この割当てなしのほうが速い。
#         # self[key] = 0
#         return 0


def main():
    int(input())

    d = {}
    ans = 0
    for i, height in enumerate(input().split()):
        height = int(height)
        # 引き算のほうの i は足し算のよりも大きい。
        # 従って，このように順番に見ていく必要がある。
        got = d.get(i + height) or 0
        d[i + height] = got + 1
        ans += d.get(i - height) or 0

    return ans


if __name__ == "__main__":
    print(main())

# 大いに参考にした。
# https://kmjp.hatenablog.jp/entry/2020/05/03/1030
Problem p02691 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
ans = 0
for i in range(N):
    a = A[i]
    for j in range(min(A[i] + i, N), N):
        if a + A[j] == j - i:
            ans += 1

print(ans)


Fixed submission:
N = int(input())
A = list(map(int, input().split()))
ans = 0

d = {}

for i, a in enumerate(A):
    tmp = i + 1 - a
    if tmp in d.keys():
        d[tmp] += 1
    else:
        d[tmp] = 1

# print(d)

for i, a in enumerate(A):
    tmp = a + i + 1
    tmptmp = i + 1 - a
    if tmp in d.keys():
        ans += d[tmp]
    if tmptmp in d.keys():
        d[tmptmp] = max(0, d[tmptmp] - 1)

print(ans)
Problem p02691 (Python)

Buggy submission (status: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split(" ")))

b = [j - i for i, j in enumerate(a, start=1)]
c = [-j - i for i, j in enumerate(a, start=1)]
result = sum([c.count(i) for i in b])
print(result)


Fixed submission:
n = int(input())
a = list(map(int, input().split(" ")))

b = [j - i for i, j in enumerate(a, start=1)]
# c = [-j - i for i, j in enumerate(a, start=1)]
count = {}
for i, j in enumerate(a, start=1):
    num = -i - j
    if num not in count:
        count[num] = 0
    count[num] += 1
result = sum([count.get(i, 0) for i in b])
print(result)
Problem p02691 (Python)

Buggy submission (status: Runtime Error):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = [0] * n
    ans = 0
    for i in range(n):
        ai = a[i]
        l, r = i + ai, i - ai
        if 0 <= l < n:
            d[l] += 1
        if 0 <= r < n:
            ans += d[r]
        print(d)
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    d = [0] * n
    ans = 0
    for i in range(n):
        ai = a[i]
        l, r = i + ai, i - ai
        if 0 <= l < n:
            d[l] += 1
        if 0 <= r < n:
            ans += d[r]
    print(ans)


if __name__ == "__main__":
    main()
Problem p02691 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
A = [int(i) for i in input().split()]
dp = [0] * 10**7
ans = 0
for i in range(N):
    x = i + 1 - A[i]
    if x >= 0:
        ans += dp[x]
    if x < 10**7:
        dp[i + 1 + A[i]] += 1

print(ans)


Fixed submission:
N = int(input())
A = [int(i) for i in input().split()]
dp = [0] * 10**7
ans = 0
for i in range(N):
    x = i + 1 - A[i]
    if x >= 0:
        ans += dp[x]
    y = i + 1 + A[i]
    if y < 10**7:
        dp[y] += 1

print(ans)
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
int arr[maxn];
int pre[maxn];
int suf[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  for (int i = 0; i < n; ++i) {
    ++pre[i + arr[i]];
    if (i - arr[i] >= 0)
      ++suf[i - arr[i]];
  }
  long long int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += (long long)pre[i] * suf[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
int arr[maxn];
int pre[maxn];
int suf[maxn];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> arr[i];
  for (int i = 0; i < n; ++i) {
    if (i + arr[i] < n)
      ++pre[i + arr[i]];
    if (i - arr[i] >= 0)
      ++suf[i - arr[i]];
  }
  long long int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += (long long)pre[i] * suf[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  ll aa, bb;

  rep(i, n) {
    cin >> aa;
    a[i] = i + aa + 1;
    b[i] = i - aa + 1;
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  ll k = 0, k2 = 0;
  ll ans = 0;

  rep(i, n) {

    if (a[i] == a[i - 1] and i != 0) {
      ans += k2;
    } else {
      k2 = 0;
      while (a[i] >= b[k]) {
        if (a[i] == b[k]) {
          ++ans;
          ++k2;
        }
        ++k;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

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

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n), b(n);
  ll aa, bb;

  rep(i, n) {
    cin >> aa;
    a[i] = i + aa + 1;
    b[i] = i - aa + 1;
  }

  sort(a.begin(), a.end());
  sort(b.begin(), b.end());

  ll k = 0, k2 = 0;
  ll ans = 0;

  rep(i, n) {

    if (a[i] == a[i - 1] and i != 0) {
      ans += k2;
    } else {
      k2 = 0;
      if (k > n - 1) {
        break;
      }
      while (a[i] >= b[k]) {
        if (a[i] == b[k]) {
          ++ans;
          ++k2;
        }
        ++k;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02691 (C++)

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

ll r(ll x, ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return r(x, y / 2) * r(x, y / 2) % mod;
  else
    return x * r(x, (y - 1) / 2) % mod * r(x, (y - 1) / 2) % mod;
}

int main() {
  int n;
  cin >> n;
  vector<ll> wa(n, 0);
  vector<int> sa(2000005, 0);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    wa[i] += (i + a);
    if (i - a >= 0) {
      sa[i - a]++;
    }
  }
  ll count = 0;
  for (int i = 0; i < n; i++) {
    count += sa[wa[i]];
  }
  printf("%lld\n", count);
}

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

ll r(ll x, ll y) {
  if (y == 0)
    return 1;
  else if (y % 2 == 0)
    return r(x, y / 2) * r(x, y / 2) % mod;
  else
    return x * r(x, (y - 1) / 2) % mod * r(x, (y - 1) / 2) % mod;
}

int main() {
  int n;
  cin >> n;
  vector<ll> wa(n, 0);
  map<ll, ll> sa;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    wa[i] += (i + a);
    if (i - a >= 0) {
      sa[i - a]++;
    }
  }
  ll count = 0;
  for (int i = 0; i < n; i++) {
    count += sa[wa[i]];
  }
  printf("%lld\n", count);
}
Problem p02691 (C++)

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

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

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define lli long long int
#define rep(i, n) for (lli i = 0; i < n; i++)
#define loop(i, j, n) for (lli i = j; i < n; i++)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
// Function
lli combi(lli n, lli r) {
  if (r == 0 || r == n)
    return 1;
  else
    return combi(n - 1, r) + combi(n - 1, r - 1);
}
lli sigma1(lli n) { return n * (n + 1) / 2; }
lli sigma2(lli s, lli n) { return sigma1(n) - sigma1(s - 1); }
using namespace std;
// }}} End Header

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  lli n, ans = 0;
  cin >> n;
  vector<lli> a(n + 1);
  loop(i, 1, n + 1) { cin >> a[i]; }
  loop(i, 1, n) {
    loop(j, i + 1, n + 1) {
      if (j - i == a[i] + a[j])
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
// Begin Header {{{
#define all(x) (x).begin(), (x).end()
#define lli long long int
#define rep(i, n) for (lli i = 0; i < n; i++)
#define loop(i, j, n) for (lli i = j; i < n; i++)
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
constexpr int INF = 0x3f3f3f3f;
const long long mod = 1e9 + 7;
// Function
lli combi(lli n, lli r) {
  if (r == 0 || r == n)
    return 1;
  else
    return combi(n - 1, r) + combi(n - 1, r - 1);
}
lli sigma1(lli n) { return n * (n + 1) / 2; }
lli sigma2(lli s, lli n) { return sigma1(n) - sigma1(s - 1); }
using namespace std;
// }}} End Header

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  lli n, ans = 0;
  cin >> n;
  vector<lli> a(n);
  rep(i, n) cin >> a[i];
  map<int, int> mp;
  rep(i, n) {
    int sa = i - a[i];
    int wa = i + a[i];
    ans += mp[sa];
    mp[wa]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (Python)

Buggy submission (status: Time Limit Exceeded):
import sys
from bisect import bisect_right, bisect_left

sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    T = [i + 1 - A[i] for i in range(N)]
    idx = sorted(range(N), key=lambda k: T[k])
    T.sort()

    ans = 0
    for i in range(N):
        target = A[i] + i + 1
        left = bisect_left(T, target)
        if left > N - 1:
            continue
        if T[left] != target:
            continue
        else:
            right = bisect_right(T, target)
            for k in range(left, min(right, N)):
                j = idx[k]
                if i < j:
                    ans += 1
    print(ans)


if __name__ == "__main__":
    main()


Fixed submission:
import sys
from bisect import bisect_right, bisect_left

sys.setrecursionlimit(10**6)
INF = float("inf")
MOD = 10**9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N = int(input())
    A = list(map(int, input().split()))
    T = [i + 1 - A[i] for i in range(N)]
    idx = sorted(range(N), key=lambda k: T[k])
    T.sort()

    ans = 0
    for i in range(N):
        target = A[i] + i + 1
        left = bisect_left(T, target)
        if left > N - 1:
            continue
        if T[left] != target:
            continue
        else:
            right = bisect_right(T, target)
            ans += right - left
    print(ans)


if __name__ == "__main__":
    main()
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N_MAX 200000
#define MODVAL 1000000007

int main(void) {
  int n;
  int a[200000];
  int i, j;
  long long int ret = 0;
  int b[2000000] = {};
  int bias = 1000000;
  int diff;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] <= 200000) {
      diff = i - a[i] + bias;
      b[diff]++;
    }
  }

  ret += b[a[0] + bias];

  for (i = 1; i < n; i++) {
    if (a[i] <= 200000) {
      ret += (long long int)(b[a[i] + bias + i]);
    }
  }

  printf("%lld\n", ret);

  return 0;
}


Fixed submission:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define N_MAX 200000
#define MODVAL 1000000007

int main(void) {
  int n;
  int a[200000];
  int i, j;
  long long int ret = 0;
  int b[2000000] = {};
  int bias = 1000000;
  int diff;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d", &a[i]);
    if (a[i] <= 200000) {
      diff = i - a[i] + bias;
      b[diff]++;
    }
  }

  if (a[0] <= 200000) {
    ret += b[a[0] + bias];
  }

  for (i = 1; i < n; i++) {
    if (a[i] <= 200000) {
      ret += (long long int)(b[a[i] + bias + i]);
    }
  }

  printf("%lld\n", ret);

  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false), cin.tie(0)
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int maxn = 1e5 + 10;
ll a[maxn];
ll l[maxn], r[maxn];
int main() {
  IO;
  ll n;
  cin >> n;
  memset(l, 0, sizeof(l));
  memset(r, 0, sizeof(r));
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i - a[i] > 0) {
      r[i - a[i]]++;
    }
    if (i + a[i] <= n) {
      l[i + a[i]]++;
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += l[i] * r[i];
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define IO ios::sync_with_stdio(false), cin.tie(0)
#define INF 0x3f3f3f3f
typedef long long ll;
using namespace std;
const int maxn = 2e5 + 10;
ll a[maxn];
ll l[maxn], r[maxn];
int main() {
  IO;
  ll n;
  cin >> n;
  memset(l, 0, sizeof(l));
  memset(r, 0, sizeof(r));
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    if (i - a[i] > 0) {
      r[i - a[i]]++;
    }
    if (i + a[i] <= n) {
      l[i + a[i]]++;
    }
  }
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += l[i] * r[i];
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define ll_MAX LONG_LONG_MAX
#define ll_MIN LONG_LONG_MIN
#define pi pair<ll, ll>
#define endl "\n"
#define MAXN 100005
using namespace std;

ll n, arr[MAXN];
map<ll, ll> m, m1;

void solve() {
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> arr[i];
    m[i + arr[i]]++;
    m1[i - arr[i]]++;
  }
  ll cnt = 0;
  for (auto it = m.begin(); it != m.end(); it++) {
    ll p = it->ss;
    ll q = m1[it->ff];
    cnt += (p * q);
  }
  cout << cnt << endl;
}

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


Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define ll_MAX LONG_LONG_MAX
#define ll_MIN LONG_LONG_MIN
#define pi pair<ll, ll>
#define endl "\n"
#define MAXN 200005
using namespace std;

ll n, arr[MAXN];
map<ll, ll> m, m1;

void solve() {
  cin >> n;
  for (ll i = 1; i <= n; i++) {
    cin >> arr[i];
    m[i + arr[i]]++;
    m1[i - arr[i]]++;
  }
  ll cnt = 0;
  for (auto it = m.begin(); it != m.end(); it++) {
    ll p = it->ss;
    ll q = m1[it->ff];
    cnt += (p * q);
  }
  cout << cnt << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  solve();
}
Problem p02691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sc(x) scanf("%lld", &x);
#define int long long
#define fi first
#define se second
#define endl '\n'
#define P pair<int, int>
#define fi first
#define se second

int A[100004];
int B[100005];
map<int, int> mp;
signed main() {
  int n;
  sc(n) for (int i = 1; i <= n; i++) {
    sc(A[i]) B[i] = A[i] - i;
    mp[B[i]]++;
  }
  int a = 0;

  for (int i = 1; i <= n; i++) {
    a += mp[-(A[i] + i)];
  }
  cout << a;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sc(x) scanf("%lld", &x);
#define int long long
#define fi first
#define se second
#define endl '\n'
#define P pair<int, int>
#define fi first
#define se second

int A[200004];
int B[200005];
map<int, int> mp;
signed main() {
  int n;
  sc(n) for (int i = 1; i <= n; i++) {
    sc(A[i]) B[i] = A[i] - i;
    mp[B[i]]++;
  }
  int a = 0;

  for (int i = 1; i <= n; i++) {
    a += mp[-(A[i] + i)];
  }
  cout << a;
  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++)
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;

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


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

int main() {
  int n;
  cin >> n;
  vi a(n + 1);
  rep(i, n) cin >> a[i + 1];
  map<int, int> mp;
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int sa = i - a[i];
    ans += mp[sa];
    int wa = i + a[i];
    mp[wa]++;
  }
  cout << ans << endl;
}