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

// R<3S
#include <bits/stdc++.h>
#define DECIMAL(n)                                                             \\
  std::cout << std::fixed;                                                     \\
  std::cout << std::setprecision(n);
#define FAST                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define inputarr(a, n)                                                         \\
  for (int i = 0; i < n; ++i)                                                  \\
  cin >> a[i]
#define initarr(a, n, x)                                                       \\
  for (int i = 0; i < n; ++i)                                                  \\
  a[i] = x
#define int long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(x) for (auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define invect(data, n)                                                        \\
  for (int i = 0; i < n; i++) {                                                \\
    int tmp;                                                                   \\
    cin >> tmp;                                                                \\
    data.pb(tmp);                                                              \\
  }
#define inset(data, n)                                                         \\
  for (int i = 0; i < n; i++) {                                                \\
    int tmp;                                                                   \\
    cin >> tmp;                                                                \\
    data.insert(tmp);                                                          \\
  }
#define ll long long

#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vii vector<pii>
#define mii map<int, int>
#define mll map<ll, ll>
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \\
       << #f << ": " << f << endl

using namespace std;

void solve() {
  int n;
  cin >> n;
  int a[n + 1];
  rep(i, n) cin >> a[i + 1];
  int cnt = 0;
  mii m1, m2;
  repA(i, 1, n) {
    m1[i + a[i]]++;
    if (i - a[i] > 0)
      m2[i - a[i]]++;
  }
  int sum = 0;
  repA(i, 1, n) { sum += m2[i + a[i]]; }
  cout << sum << endl;
  // trav(m)if(i.se-1)cout<<i.fi<<" "<<i.se<<endl;
}

signed main() {
  FAST int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}


Error: TLE

Fixed submission:

// R<3S
#include <bits/stdc++.h>
#define DECIMAL(n)                                                             \\
  std::cout << std::fixed;                                                     \\
  std::cout << std::setprecision(n);
#define FAST                                                                   \\
  std::ios_base::sync_with_stdio(false);                                       \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define hell 1000000007
#define PI 3.14159265358979323844
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(v) v.begin(), v.end()
#define SORT(v) sort(ALL(v))
#define REVERSE(v) reverse(ALL(v))
#define endl "\\n"
#define vecmax(v) max_element(all(v))
#define vecmin(v) min_element(all(v))
#define GCD(m, n) __gcd(m, n)
#define LCM(m, n) m *(n / GCD(m, n))
#define inputarr(a, n)                                                         \\
  for (int i = 0; i < n; ++i)                                                  \\
  cin >> a[i]
#define initarr(a, n, x)                                                       \\
  for (int i = 0; i < n; ++i)                                                  \\
  a[i] = x
#define int long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define repD(i, a, n) for (int i = a; i >= (n); --i)
#define trav(x) for (auto i : x)
#define sz(a) (int)a.size()
#define sl(a) (int)a.length()
#define invect(data, n)                                                        \\
  for (int i = 0; i < n; i++) {                                                \\
    int tmp;                                                                   \\
    cin >> tmp;                                                                \\
    data.pb(tmp);                                                              \\
  }
#define inset(data, n)                                                         \\
  for (int i = 0; i < n; i++) {                                                \\
    int tmp;                                                                   \\
    cin >> tmp;                                                                \\
    data.insert(tmp);                                                          \\
  }
#define ll long long

#define ld long double
#define pii std::pair<int, int>
#define pll std::pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
#define vii vector<pii>
#define mii map<int, int>
#define mll map<ll, ll>
#define trace1(x) cerr << #x << ": " << x << endl
#define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cerr << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define trace5(a, b, c, d, e)                                                  \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl
#define trace6(a, b, c, d, e, f)                                               \\
  cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | "   \\
       << #f << ": " << f << endl

using namespace std;

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

signed main() {
  FAST int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }

  return 0;
}