Problem p02691 (C++) — buggy submission, verdict: Time Limit Exceeded

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

typedef long long ll;

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

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define ALL(s) (s).begin(), (s).end()
#define ALLn(s, n) s, s + n
#define F first
#define S second
#define pb push_back
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define D1(x)                                                                  \\
  { cerr << " [" << #x << ": " << x << "]\\n"; }
#define D2(x)                                                                  \\
  {                                                                            \\
    cerr << " [" << #x << ": ";                                                \\
    for (auto it : x)                                                          \\
      cerr << it << " ";                                                       \\
    cerr << "]\\n";                                                             \\
  }

const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 7;
const ll INF = INT_MAX;

vi reach[MAXN];

void solve() {
  ll n;
  cin >> n;

  ll a[n];
  F0R(i, n) {
    cin >> a[i];
    ll prev = (i + 1) - a[i];
    ll next = (i + 1) + a[i];
    if (prev > 0)
      reach[prev].pb(-(i + 1));
    if (next <= n)
      reach[next].pb(i + 1);
  }
  ll ans = 0;

  FOR(i, 1, n + 1) {
    ll p = 0, n = 0;
    for (auto x : reach[i]) {
      if (x < 0)
        n++;
      else
        p++;
    }
    ans += (p * n);
  }

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

int32_t main() {
  _
  // tc(t)
  solve();
}


Error: TLE

Fixed submission:

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

typedef long long ll;

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

typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<pii> vpii;
typedef vector<pll> vpll;

#define FOR(i, a, b) for (int i = a; i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)

#define ALL(s) (s).begin(), (s).end()
#define ALLn(s, n) s, s + n
#define F first
#define S second
#define pb push_back
#define tc(t)                                                                  \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define _                                                                      \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);

const ll MOD = 1e9 + 7;
const ll MAXN = 1e6 + 7;
const ll INF = INT_MAX;

void solve() {

  int n;
  cin >> n;

  ll a[n];
  F0R(i, n) cin >> a[i];
  ll cnt = 0;
  F0R(i, n) {
    FOR(j, a[i] + i + 1, n) {
      if (a[i] + a[j] == abs(i - j))
        cnt++;
    }
  }

  cout << cnt << "\\n";
}

int32_t main() {
  _
  // tc(t)
  solve();
}