alexjercan/bugnet huggingface dataset:
Problem p02691 (C++) — buggy submission, verdict: 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];
    if (i + A <= N)
      ++cnt[i + A];
  }
  fin(ans);
  return 0;
}


Error: 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];
    ++cnt[i + A];
  }
  fin(ans);
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MAX = 2000000;
const ll MOD = 1000000007;
ll n;

int main() {
  cin >> n;
  ll side1[400000];
  ll side2[400000];
  ll count = 0;
  for (ll i = 0; i < 400000; i++) {
    side1[i] = 0;
    side2[i] = 0;
  }
  for (ll i = 0; i < n; i++) {
    ll atemp = 0;
    cin >> atemp;
    if (atemp <= 200000) {
      side1[i + atemp]++;
    }
    if (i - atemp >= 0) {
      side2[i - atemp]++;
    }
  }
  for (ll i = 0; i < 400000; i++) {
    count += side1[i] * side2[i];
  }
  cout << count << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef long double ld;
const ll MAX = 2000000;
const ll MOD = 1000000007;
ll n;

int main() {
  cin >> n;
  ll a[n];
  ll count = 0;
  for (ll i = 0; i < n; i++) {
    ll atemp = 0;
    cin >> atemp;
    a[i] = atemp;
    for (ll j = 0; j < i; j++) {
      if ((i - j) == (atemp + a[j]))
        count++;
    }
  }
  cout << count << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> VI;
typedef vector<VI> VVI;
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define PRINT(V)                                                               \\
  for (auto v : (V))                                                           \\
  cout << v << " "

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  map<ll, ll> ai, aj;
  ll c = 0;
  rep(i, n) {
    ++ai[i + a[i]];
    ++aj[i - a[i]];
  }

  for (const auto &e : ai) {
    if (aj.count(e.first) == 1) {
      c += e.second * aj[e.first];
    }
  }
  cout << c << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define PRINT(V)                                                               \\
  for (auto v : (V))                                                           \\
  cout << v << " "
typedef long long ll;

int main() {
  int n;
  cin >> n;
  VI a(n);
  rep(i, n) cin >> a[i];

  int c = 0;
  rep(i, n) FOR(j, i + a[i], n) {
    if (j - i == a[i] + a[j])
      ++c;
  };
  cout << c << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: 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];
    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;
}

Error: 0

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];
    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, verdict: 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];
    if (i + x < 2 * n)
      a[i + x]++;
  }
  cout << ans << endl;
  return 0;
}

Error: 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];
    a[i + x]++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int MOD = 1000000007;

int main() {
  ll N;
  vector<ll> A, B;
  cin >> N;
  ll cnt = 0;
  ll c;
  for (int i = 0; i < N; i++) {
    cin >> c;
    A.push_back(i + 1 - c);
    B.push_back(c + i + 1);
  }
  ll idx_lower, idx_upper;
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  for (int i = 0; i < N; i++) {
    if (binary_search(B.begin(), B.end(), A[i])) {
      idx_upper = distance(B.begin(), upper_bound(B.begin(), B.end(), A[i]));
      idx_lower = distance(B.begin(), lower_bound(B.begin(), B.end(), A[i]));
      cnt += idx_upper - idx_lower;
    }
  }
  cout << cnt;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
const int MOD = 1000000007;

int main() {
  ll N;
  vector<pair<ll, ll>> A;
  cin >> N;
  ll cnt = 0;
  ll c;
  for (int i = 0; i < N; i++) {
    cin >> c;
    if (c < N) {

      A.push_back(make_pair(c, i));
    }
  }
  sort(A.begin(), A.end());

  for (int i = 0; i < A.size(); i++) {

    for (int j = i + 1; j < A.size(); j++) {
      if (A[i].first + A[j].first == labs(A[i].second - A[j].second)) {

        cnt++;

      } else if (A[i].first + A[j].first >= N) {

        break;
      }
    }
  }
  cout << cnt;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <unordered_map>

using namespace std;
using ll = long long;

int main() {
  int N;
  ll ans = 0;
  cin >> N;
  unordered_map<ll, ll> L;
  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    ll l = i + 1 + a;
    ll r = i + 1 - a;
    auto itr = L.find(r);
    if (itr != L.end()) {
      ll &l_ = (*itr).second;
      ans += l_;
    }
    L[l]++;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;

int main() {
  int N;
  int ans = 0;
  cin >> N;
  vector<ll> A(N);
  vector<ll> L(N);
  unordered_multimap<ll, ll> R;
  ll max_L = 0;
  ll max_R = 0;
  for (ll i = 0; i < N; i++) {
    ll a;
    cin >> a;
    ll l = i + 1 + a;
    ll r = i + 1 - a;
    A.at(i) = a;
    L[i] = l;
    R.emplace(r, i);
  }

  for (ll i = 0; i < L.size(); i++) {
    ll l = L.at(i);
    auto range = R.equal_range(l);
    for (auto itr = range.first; itr != range.second; itr++) {
      ll j = (*itr).second;
      if (i + 1 < j + 1) {
        ans++;
      }
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int N, A;
  map<int, int> sum; /* i + Ai を格納 */
  map<int, int> sub; /* i - Ai を格納 */

  /* 入力の受取 */
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> A;
    sum[i + A]++;
    sub[i - A]++;
  }

  long long ans = 0;
  for (auto itr : sum) {
    /* sum と同じ値の要素がなければスキップ */
    if (sub.find(itr.first) == sub.end())
      continue;
    /* sum と同じ値の要素があったら組み合わせ個数を計算 */
    ans += (long long)itr.second * sub.at(itr.first);
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main(void) {
  int N;
  cin >> N;
  vector<long long> A(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> A.at(i);
  }

  long long ans = 0;
  for (int i = 1; i <= N; i++) {
    for (int j = i + 1; j <= N; j++) {
      if (j - i == A.at(i) + A.at(j))
        ans++;
    }
  }

  cout << ans << endl;

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<vector<int>> Matrix;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
static const int INF = 2000000000;

int main() {
  int n;
  cin >> n;
  vector<ll> L(200010), R(200010), a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  rep(i, n) {
    ll l = i - a[i], r = i + a[i];
    if (l >= 0)
      L[l]++;
    if (r < n)
      R[r]++;
  }
  rep(i, n) ans += (ll)L[i] * R[i];
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef vector<vector<int>> Matrix;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
static const int INF = 2000000000;

const ll mod = 1000000007;
ll f(ll C) {
  ll ans = C * C * C * C * C;
  return ans;
}

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  ll ans = 0;
  vector<vector<bool>> vec(n, vector<bool>(n, false));
  rep(i, n) {
    rep(j, n) {
      if (!vec[i][j]) {
        vec[i][j] = vec[j][i] = true;
        if (i != j)
          if (abs(i - j) == a[i] + a[j])
            ans++;
      }
    }
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;

int main() {
  int N;
  cin >> N;
  map<long long int, long long int> mp;

  vector<long long int> vec(N);
  for (int i = 0; i < N; i++) {
    cin >> vec[i];
    mp[i + vec[i]]++;
  }

  long long int ans = 0;
  for (int i = 0; i < N; i++) {
    ans += mp[i - vec[i]];
  }

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> vec(N);

  for (int i = 0; i < N; i++) {
    cin >> vec.at(i);
  }
  int count = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (vec[i] + vec[j] == j - i) {
        count++;
      }
    }
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define CO(a, b, c) ((a) ? (b) : (c))
#define mp(i, j) make_pair(i, j)
using namespace std;

int main(void) {

  int n;
  cin >> n;
  int a;
  map<long, long> add{}, sub{};
  for (int i = 1; i < n + 1; i++) {
    cin >> a;
    auto pair = add.insert(mp(a + i, 0));
    (pair.first->second)++;
    pair = sub.insert(mp(i - a, 0));
    (pair.first->second)++;
  }

  long long count = 0;
  for (auto it = add.begin(); it != add.end(); it++) {
    auto f = sub.find(it->first);
    if (f != sub.end()) {
      count += (it->second * f->second);
    }
  }
  cout << count << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
#include <map>
#include <vector>
#define CO(a, b, c) ((a) ? (b) : (c))
#define mp(i, j) make_pair(i, j)
using namespace std;

int main(void) {

  int n;
  cin >> n;
  int a;
  multimap<int, int> add{}, sub{};
  for (int i = 1; i < n + 1; i++) {
    cin >> a;
    add.insert(mp(a + i, 0));
    sub.insert(mp(i - a, 0));
  }

  int count = 0;
  for (auto it = add.begin(); it != add.end(); it++) {
    count += sub.count(it->first);
  }

  cout << count << endl;

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: 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 = 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;
}


Error: 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 = 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;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define deb(x) cout << #x << " " << x << "\\n";
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; i < n; i++)
#define endl "\\n"
#define ll long long
#define vi vector<int>

// memset(arr, value, size);

int main() {
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif

  ios::sync_with_stdio(0);
  cin.tie(0);

  long t, i, count = 0;
  map<long, long> l, r;
  cin >> t;
  fo(i, t) {
    long a;
    cin >> a;
    r[i - a]++;
    l[i + a]++;
  }

  for (auto el : l) {
    count += el.second * r[el.first];
  }

  cout << count;
  return 0;
}

Error: TLE

Fixed submission:

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

#define deb(x) cout << #x << " " << x << "\\n";
#define fo(i, n) for (i = 0; i < n; i++)
#define Fo(i, k, n) for (i = k; i < n; i++)
#define endl "\\n"
#define ll long long
#define vi vector<int>

// memset(arr, value, size);
ll arr[200000 + 10];

int main() {
#ifndef ONLINE_JUDGE
  freopen("input1.txt", "r", stdin);
  freopen("output1.txt", "w", stdout);
#endif
  ios::sync_with_stdio(0);
  cin.tie(0);

  int t, i, count = 0;
  cin >> t;

  fo(i, t) { cin >> arr[i + 1]; }

  Fo(i, 1, t + 1) {
    int j = i + arr[i] + 1;
    while (j < t + 1) {
      if (abs(i - j) == arr[i] + arr[j])
        count++;
      j++;
    }
  }

  cout << count;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int l[N], A_temp;
  map<int, int> r;
  for (int i = 0; i < N; i++) {
    cin >> A_temp;
    l[i] = i + A_temp;
    r[i - A_temp] += 1;
  }
  long int ans = 0;
  for (int i = 0; i < N - 1; i++)
    ans += r[l[i]];
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  cin >> N;
  int l[N], r[N], A_temp;
  for (int i = 0; i < N; i++) {
    cin >> A_temp;
    l[i] = i + A_temp;
    r[i] = i - A_temp;
  }
  int ans = 0;
  for (int i = 0; i < N - 1; i++) {
    for (int j = i + 1; j < N; j++) {
      if (l[i] == r[j])
        ans += 1;
    }
  }
  cout << ans;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

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

  map<int, int> mp;
  int n;
  cin >> n;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    ans += mp[i - x];
    mp[x + i]++;
  }

  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  // your code goes here
  int n;
  cin >> n;
  vector<int> vec(n + 2, 0);
  for (int i = 1; i <= n; i++)
    cin >> vec[i];
  int count = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if ((j - i) == vec[i] + vec[j])
        count++;
    }
  }
  cout << count;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = (int)1e9;
const ll inf = (ll)1e18;
const int mod = 1000000007;
const int MAX_N = 100010;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  /*------------------------------*/

  int n;
  cin >> n;

  map<ll, ll> mp1, mp2;
  ll ans = 0;

  rep(i, n) {
    ll a;
    cin >> a;
    ans += mp1[i - a];
    ans += mp2[i + a];
    mp1[i + a]++;
    mp2[i - a]++;
  }

  out(ans);

  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) {
  ll g = gcd(a, b);
  return a / g * b;
}
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int INF = (int)1e9;
const ll inf = (ll)1e18;
const int mod = 1000000007;
const int MAX_N = 100010;
const double PI = acos(-1);

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  /*------------------------------*/

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

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

  out(ans);

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define orep(i, n) for (long long i = 1; i <= (n); ++i)     // one rep
#define drep(i, n) for (long long i = (n)-1; i >= 0; --i)   // down rep
#define srep(i, s, t) for (long long i = (s); i < (t); ++i) // set rep
#define rng(x) (x).begin(), (x).end()

// range
#define rrng(a) (a).rbegin(), (a).rend() // reverse range
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define ru(x, y) (((x) + (y)-1) / (y)) // round up
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vl = vector<ll>;

template <typename T> void Yes(T flag) {
  cout << (flag ? "Yes" : "No") << endl;
}

template <typename T> void pv(vector<T> vec) {
  cout << "[";
  for (auto &v : vec) {
    cout << v << ",";
  }
  cout << "]" << endl;
}

template <> void pv(vector<P> vec) {
  cout << "[";
  for (auto &v : vec) {
    cout << "<" << v.fi << "," << v.se << ">"
         << ",";
  }
  cout << "]" << endl;
}

template <typename T> void pv2(vector<vector<T>> vec) {
  for (auto &v : vec)
    pv(v);
}

int main() {
  int n;
  cin >> n;
  ll ans = 0;
  vl l(n, 0);
  // vi r(n,0);
  orep(i, n) {
    ll a;
    cin >> a;
    if (i - a > 0)
      ans += l.at(i - a);
    if (a + i <= n - 1)
      l.at(a + i)++;
  }

  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define orep(i, n) for (long long i = 1; i <= (n); ++i)     // one rep
#define drep(i, n) for (long long i = (n)-1; i >= 0; --i)   // down rep
#define srep(i, s, t) for (long long i = (s); i < (t); ++i) // set rep
#define rng(x) (x).begin(), (x).end()

// range
#define rrng(a) (a).rbegin(), (a).rend() // reverse range
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define ru(x, y) (((x) + (y)-1) / (y)) // round up
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)

using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;
using vp = vector<P>;
using vl = vector<ll>;

template <typename T> void Yes(T flag) {
  cout << (flag ? "Yes" : "No") << endl;
}

template <typename T> void pv(vector<T> vec) {
  cout << "[";
  for (auto &v : vec) {
    cout << v << ",";
  }
  cout << "]" << endl;
}

template <> void pv(vector<P> vec) {
  cout << "[";
  for (auto &v : vec) {
    cout << "<" << v.fi << "," << v.se << ">"
         << ",";
  }
  cout << "]" << endl;
}

template <typename T> void pv2(vector<vector<T>> vec) {
  for (auto &v : vec)
    pv(v);
}

int main() {
  int n;
  cin >> n;
  vi a(n, 0);
  rep(i, n) { cin >> a.at(i); }

  int ans = 0;
  rep(i, n) rep(j, n) {
    if (abs(i - j) == a.at(i) + a.at(j))
      ans++;
  }
  cout << ans / 2 << endl;

  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FOR(i, x, y) for (ll i = x; i < y; i++)
typedef long long ll;
using namespace std;

ll a[200001];
map<ll, ll> cnt;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  cin >> n;
  FOR(i, 0, n) {
    cin >> a[i];
    a[i] -= i;
    cnt[a[i]]++;
  }
  ll ans = 0;
  FOR(i, 0, n) {
    ans += cnt[-2 * i - a[i]];
    cnt[a[i]]--;
  }
  cout << ans;
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;

int a[100001];
map<int, int> cnt;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  FOR(i, 0, n) {
    cin >> a[i];
    a[i] -= i;
    cnt[a[i]]++;
  }
  ll ans = 0;
  FOR(i, 0, n) {
    ans += cnt[-2 * i - a[i]];
    cnt[a[i]]--;
  }
  cout << ans;
  return 0;
}