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

// #pragma GCC optimize("Ofast")

#pragma GCC optimize("O3")
// #pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
constexpr auto INF = 9223372036854775807;
typedef long long int ll;
typedef unsigned long long int ull;
typedef unsigned long int ul;
#define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i += 1)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i -= 1)
#define endl '\\n'
#define N 1000000007 // prime modulo value
#define C 1000000001
#define M 998244353
#define all(x) x.begin(), x.end()
using namespace std;
// From Geeksforgeeks
inline ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// Power function took from geeksforgeeks
ll power(ll x, ll y) {
  ll res = 1;

  x = x;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
double dist(ll x1, ll y1, ll x2, ll y2) {
  double ans = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll n;
  cin >> n;
  vector<ll> arr(n, 0);
  unordered_map<ll, ll> mp;
  f(i, 0, n) {
    cin >> arr[i];
    if (mp.find(arr[i] + i) == mp.end())
      mp[arr[i] + i] = 1;
    else
      mp[arr[i] + i] += 1;
  }
  ll ans = 0;
  rf(j, n - 1, 0) {
    if (mp.find(arr[j] + j) != mp.end() && mp[arr[j] + j] > 0)
      mp[arr[j] + j] -= 1;
    if (j - arr[j] > 0)
      ans += mp[j - arr[j]];
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")

// #pragma GCC optimize("O3")
#pragma GCC target("avx,avx2,fma")
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
constexpr auto INF = 9223372036854775807;
typedef long long int ll;
typedef unsigned long long int ull;
typedef unsigned long int ul;
#define f(i, a, b) for (ll i = (ll)a; i < (ll)b; i += 1)
#define rf(i, a, b) for (ll i = (ll)a; i >= (ll)b; i -= 1)
#define endl '\\n'
#define N 1000000007 // prime modulo value
#define M 998244353
#define all(x) x.begin(), x.end()
using namespace std;
// From Geeksforgeeks
inline ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// Power function took from geeksforgeeks
ll power(ll x, ll y) {
  ll res = 1;

  x = x % N;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % N;
    y = y >> 1;
    x = (x * x) % N;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ll n;
  cin >> n;
  vector<ll> arr(n, 0);
  f(i, 0, n) { cin >> arr[i]; }
  ll ans = 0;
  f(i, 0, n) {
    f(j, i + arr[i] + 1, n) {
      if ((j < n) && abs(j - i) == arr[i] + arr[j])
        ans += 1;
    }
  }
  cout << ans;
  return 0;
}