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

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define keta(a) fixed << setprecision(a)
constexpr auto INF = 100000000000;
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
struct edge {
  int to, cost;
};

int main() {
  int n;
  cin >> n;
  map<llint, llint> Z;
  hei(llint) w(n), s(n);
  rep(i, 0, n) {
    llint a;
    cin >> a;
    w[i] = a + i + 1;
    s[i] = i + 1 - a;
    Z[s[i]]++;
  }
  llint ans = 0;
  rep(i, 0, n) { ans += Z[w[i]]; }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define keta(a) fixed << setprecision(a)
constexpr auto INF = 100000000000;
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！
struct edge {
  int to, cost;
};

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