alexjercan/bugnet huggingface dataset:
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define inv(n, a)                                                              \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define MOD 1000000007
#define INF 1e7
#define LINF 1e15
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// 'a' = 97 'z' = 122 => 'a' = 0 'z' = 25

int main() {
  // int n; cin >> n;
  // int a;
  // map<ll, ll> m;
  // int ans = 0;
  // for(int i = 1; i <= n; i++){
  //     cin >> a;
  //     ll l = i - a;
  //     ll r = i + a;
  //     ans += m[l];
  //     m[r]++;
  // }
  // cout << ans << endl;
  int n;
  cin >> n;
  map<ll, ll> am;
  ll hi;
  ll ans = 0;
  for (ll i = 1; i <= n; i++) {
    cin >> hi;
    ans += am[i - hi];
    am[hi + i] += 1;
  }
  printf("%lli\\n", ans);
  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define inv(n, a)                                                              \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define MOD 1000000007
#define INF 1e7
#define LINF 1e15
typedef long long ll;
typedef long double ld;
using Graph = vector<vector<int>>;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
// 'a' = 97 'z' = 122 => 'a' = 0 'z' = 25

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> R, L;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    L[i + a[i]]++;
    R[a[i] - i]++;
  }
  int ans = 0;
  for (auto p : L) {
    int key = p.first;
    if (R.count(key))
      ans += L[key] * R[key];
    L.erase(key);
  }
  cout << ans << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bits/stdc++.h>
#define int long long
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000

int MOD = 1000000007;
int modpow(int a, int x) {
  if (a < 0)
    a += MOD;
  int ans = 1;
  while (x > 0) {
    if (x & 1)
      ans = ans * a % MOD;
    a = a * a % MOD;
    x >>= 1;
  }
  return ans;
}

int gyaku(int n) { return modpow(n, MOD - 2); }
////////////////////////////////////////////////////////////

signed main() {
  int n;
  cin >> n;
  vector<int> a(n);

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

  map<int, int> ai;
  map<int, int> aj;
  rep(i, n) {
    ai[a[i] + i]++;
    aj[i - a[i]]++;
  }
  int ans = 0;
  for (auto &&x : ai) {
    int key, value;
    key = x.first;
    value = x.second;
    ans += aj[key] * value;
  }
  cout << ans;
}


Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
// #define int long long
using namespace std;
typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000000000

int MOD = 1000000007;
int modpow(int a, int x) {
  if (a < 0)
    a += MOD;
  int ans = 1;
  while (x > 0) {
    if (x & 1)
      ans = ans * a % MOD;
    a = a * a % MOD;
    x >>= 1;
  }
  return ans;
}

int gyaku(int n) { return modpow(n, MOD - 2); }
////////////////////////////////////////////////////////////

signed main() {
  int n;
  vector<int> a(n);
  cin >> n;
  rep(i, n) cin >> a[i];

  map<int, int> ai;
  map<int, int> aj;
  rep(i, n) {
    ai[a[i] + i]++;
    aj[i - a[i]]++;
  }
  int ans = 0;
  for (auto &&x : ai) {
    int key, value;
    key = x.first;
    value = x.second;
    ans += aj[key] * value;
  }
  cout << ans;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (const auto &e : vec)
    os << e << (&e == &vec.back() ? "\\n" : " ");
  return os;
}

#ifdef _DEBUG
template <class T> void dump(const char *str, T &&h) {
  cerr << str << " = " << h << "\\n";
};
template <class Head, class... Tail>
void dump(const char *str, Head &&h, Tail &&...t) {
  while (*str != ',')
    cerr << *str++;
  cerr << " = " << h << "\\n";
  dump(str + (*(str + 1) == ' ' ? 2 : 1), t...);
}
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

int main() {

  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<int> dp(N);
  lint ans = 0;
  for (int i = 0; i < N; i++) {
    if (i - A[i] >= 0)
      ans += dp[i - A[i]];
    DMP(i, dp);
    if (i + A[i] < N)
      dp[A[i] + i]++;
  }

  cout << ans << "\\n";

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <vector>

using namespace std;
using lint = long long;
constexpr int MOD = 1000000007, INF = 1010101010;
constexpr lint LINF = 1LL << 60;

template <class T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  for (const auto &e : vec)
    os << e << (&e == &vec.back() ? "\\n" : " ");
  return os;
}

#ifdef _DEBUG
template <class T> void dump(const char *str, T &&h) {
  cerr << str << " = " << h << "\\n";
};
template <class Head, class... Tail>
void dump(const char *str, Head &&h, Tail &&...t) {
  while (*str != ',')
    cerr << *str++;
  cerr << " = " << h << "\\n";
  dump(str + (*(str + 1) == ' ' ? 2 : 1), t...);
}
#define DMP(...) dump(#__VA_ARGS__, __VA_ARGS__)
#else
#define DMP(...) ((void)0)
#endif

int main() {

  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;

  vector<int> A(N);
  for (int i = 0; i < N; i++)
    cin >> A[i];

  vector<int> dp(3 * N);
  lint ans = 0;
  for (int i = 0; i < N; i++) {
    if (i - A[i] > 0)
      ans += dp[i - A[i]];
    DMP(i, dp);
    dp[A[i] + i]++;
  }

  cout << ans << "\\n";

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

#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 1e18;
const ll mod = 2019;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }

int main() {

  int n;
  cin >> n;
  vector<int> A(n);
  rep(i, n) cin >> A[i];
  map<int, int> mp;
  ll ans = 0;
  rep(i, n) {
    int gap = i - A[i];
    mp[gap]++;
  }
  rep(i, n) {
    int sum = i + A[i];
    ans += mp[sum];
  }
  cout << ans << endl;
  return 0;
}


Error: -6
Fatal glibc error: malloc assertion failure in sysmalloc: (old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)


Fixed submission:

#include <bits/stdc++.h>
#include <cmath>
#include <cstdlib>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, n, m) for (long long int i = n; i <= m; i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll INF = 1e18;
inline void chmax(ll &a, ll b) { a = max(a, b); }
inline void chmin(ll &a, ll b) { a = min(a, b); }
int main() {
  int n;
  cin >> n;
  vector<int> A(n);
  rep(i, n) cin >> A[n];
  vector<int> sum(n);
  rep(i, n) sum[i] = A[i] + i;
  vector<int> gap(n);
  rep(i, n) gap[i] = A[i] - i;
  ll ans = 0;
  rep(i, n) {
    rep(j, n) {
      if (sum[i] + gap[j] == 0)
        ans++;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: 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;
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;
}


Error: WA

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const int INF = 1e18 + 10;

/*----------------------------------------------*/

signed main() {
  int n;
  cin >> n;
  vi a(n);
  map<int, int> mp;
  int ans = 0;
  rep(i, n) {
    cin >> a[i];

    mp[i + a[i]]++;
    if (i - a[i] >= 0)
      ans += mp[i - a[i]];
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(a, b)                                                            \\
  if ((a) > (b))                                                               \\
    (a) = (b);
#define chmax(a, b)                                                            \\
  if ((a) < (b))                                                               \\
    (a) = (b);
#define vi vector<int>
#define pii pair<int, int>
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()
#define pb push_back
#define pf push_front
int gcd(int a, int b) { /*a>=0,b>=0,￢(a=b=0)*/
  while (min(a, b) > 0) {
    if (a < b)
      swap(a, b);
    a %= b;
  }
  return max(a, b);
}
int dx[] = {0, 1, 0, -1, 1, -1, -1, 1};
int dy[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int MOD = 1e9 + 7;
const int INF = 1e18 + 10;

/*----------------------------------------------*/

signed main() {
  int n;
  cin >> n;
  vi a(n);
  // vi h[n];
  rep(i, n) {
    cin >> a[i];
    // if(a[i]<=n) h[a[i]].pb(i+1);
  }

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

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

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

int main() {

  int n;
  cin >> n;
  map<int, int> ml;
  map<int, int> mr;
  vector<int> v(n + 1);
  for (int i = 1; i <= n; ++i) {
    int a;
    cin >> a;
    v[i] = a;
    // fprintf(stderr,"i: %d a: %d\\n",i,a);
    ml[i + a]++;
    mr[i - a]++;
    // m.insert(make_pair(a,i));
  }

  int64_t count = 0;
  for (pair<int, int> p : ml) {
    if (mr.count(p.first) > 0) {
      count += (int64_t)p.second * mr[p.first];
    }
  }
  cout << count << endl;
}


Error: TLE

Fixed submission:

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

int main() {

  int n;
  cin >> n;
  multimap<int, int> m;
  vector<int> v(n + 1);
  for (int i = 1; i <= n; ++i) {
    int a;
    cin >> a;
    a -= i;
    v[i] = a;
    // fprintf(stderr,"i: %d a: %d\\n",i,a);
    m.insert(make_pair(a, i));
  }

  int count = 0;
  for (int i = 1; i <= n; ++i) {
    int lookfor = -v[i] - 2 * i;
    count += m.count(lookfor);
    // fprintf(stderr,"i: %d lookfor: %d count
    // %d\\n",i,lookfor,m.count(lookfor));
  }
  cout << count << endl;
}
Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 1e9 + 7, MAX = 1e18, larg = 1e5, INF = -1e9;
long long A, B, C, D, E, F, G, H, I, J, K, L, N, M, O, P, Q, R, S, T, U, V, W;
typedef long long ll;
typedef pair<ll, ll> Pa;

int arr[200000];
map<int, int> cnt;
int main() {
  cin >> N;
  rep(i, N) {
    cin >> arr[i];
    if (cnt.find(arr[i] - i) == cnt.end())
      cnt[arr[i] - i] = 1;
    else
      cnt[arr[i] - i]++;
  }
  ll sum = 0;
  rep(i, N) {
    if (cnt.find(arr[i] - i) != cnt.end())
      sum += cnt[-arr[i] - i];
  }
  cout << sum;
}


Error: WA

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD = 1e9 + 7, MAX = 1e18, larg = 1e5, INF = -1e9;
long long A, B, C, D, E, F, G, H, I, J, K, L, N, M, O, P, Q, R, S, T, U, V, W;
typedef long long ll;
typedef pair<ll, ll> Pa;

int arr[200000];
int cnt[400000];
int main() {
  cin >> N;
  rep(i, 400000) { cnt[i] = 0; }
  rep(i, N) {
    cin >> arr[i];
    cnt[arr[i] - i + 200000]++;
  }
  ll sum = 0;
  rep(i, N) { sum += cnt[200000 - arr[i] - i]; }
  cout << sum;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#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);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  map<int, int> mp;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int sa = i - a[i];
    ans += mp[sa];
    int wa = a[i] + i;
    mp[wa]++;
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

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 + 1);
  for (int i = 1; i <= n; i++)
    cin >> a[i];

  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int ix = i + a[i] + 1;
    int iy = i - a[i] - 1;
    if (ix <= n) {
      while (ix <= n) {
        if (abs(i - ix) == a[i] + a[ix])
          ans++;
        ix++;
      }
    }
    if (iy >= 1) {
      while (iy >= 1) {
        if (abs(i - iy) == a[i] + a[iy])
          ans++;
        iy--;
      }
    }
  }

  ans /= 2;
  cout << ans << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
long long co[200020] = {0};
int main() {
  int n;
  int a[200020];
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long ans = 0;
  for (int j = 0; j < n; j++) {
    if (j - a[j] > 0) {
      ans += co[j - a[j]];
    }
    if (j + a[j] < 200020) {
      co[j + a[j]]++;
    }
  }
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> vec;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    vec.emplace_back(a);
  }
  long long co = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + vec[i]; j < n; j++) {
      if (vec[i] + vec[j] == abs(i - j)) {
        co++;
      }
    }
  }
  cout << co << endl;
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define sz(x) (int)((x).size())
#define F first
#define S second
#define pb push_back
#define ub upper_bound
#define lb lower_bound
#define all(x) x.begin(), x.end()
#define pqueue priority_queue
#define mset multiset
#define umap unordered_map
#define Speed()                                                                \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define forn(i, n) for (int i = 1; i <= n; ++i)
#define lr(i, l, r)                                                            \\
  for (int i = l; (l > r ? i >= r : i <= r); (l > r ? --i : ++i))

using namespace std;

template <class T> void umin(T &a, T b) { a = min(a, b); }
template <class T> void umax(T &a, T b) { a = max(a, b); }

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int dx[] = {1, 0, -1, 0, -1, -1, 1, 1},
          dy[] = {0, 1, 0, -1, -1, 1, -1, 1};
const int mod = 1e9 + 7;
const ll N = 2e5 + 5;

void solve() {
  ll n, ans = 0;
  cin >> n;
  int a[n + 1];
  forn(i, n) cin >> a[i];
  map<int, int> cnt;
  forn(i, n) {
    ans += cnt[i - a[i]];
    cnt[i + a[i]]++;
  }
  cout << ans;
}

int main() {
  Speed();
  int T = 1;
  // cin>>T;
  while (T--)
    solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define Red                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define rep(i, n) for (int i = 0; i < n; ++i)
#define Rep(i, a, b) for (int i = a; i <= b; i++)
#define pii pair<int, int>
typedef vector<int> vi;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = (int)2e5 + 5;

void solve() {
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (a[i] + a[j] == j - i)
        cnt++;
    }
  }
  cout << cnt;
}

int main() {
  Red;
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}
Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e 2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x & (-x))
#define memset(x, y) memset(x, y, sizeof(x))
#define lson i << 1, l, m
#define rson i << 1 | 1, m + 1, r
#define ll long long
using namespace std;
const int N = 2e5 + 5;
ll arr[N] = {0}, cha[N] = {0};
map<int, int> mp;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("text1.in","r",stdin);
  // freopen("text1.out","w",stdout);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> arr[i];
    mp[arr[i] + i]++;
  }
  ll cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (mp[i - arr[i]])
      cnt += mp[i - arr[i]];
  }
  cout << cnt << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e 2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x & (-x))
#define memset(x, y) memset(x, y, sizeof(x))
#define lson i << 1, l, m
#define rson i << 1 | 1, m + 1, r
#define ll long long
using namespace std;
const int N = 2e5 + 5;
ll arr[N] = {0}, cha[N] = {0};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  // freopen("text1.in","r",stdin);
  // freopen("text1.out","w",stdout);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> arr[i];
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i - arr[i]; j++) {
      if (abs(arr[i] + arr[j]) == i - j) {
        cnt++;
        //				cout<<i<<" "<<j<<endl;
      }
    }
  }
  cout << cnt << endl;
  return 0;
}