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

/*
    author:@bhaskar_8998
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long long i64;
typedef vector<int> vi;
typedef vector<long> vl;
typedef pair<int, int> pii;
#define all(A) (A).begin(), (A).end()
#define pb push_back
#define MOD 1000000007
#define fastio()                                                               \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  fastio();
  clock_t beg = clock();
  int t = 1;
  // cin>>t;
  while (t--) {
    i64 n;
    ;
    cin >> n;
    i64 h[n];
    unordered_map<i64, i64> m1, m2;
    m1.clear();
    m2.clear();
    for (int i = 0; i < n; i++) {
      cin >> h[i];
      m1[i + 1 - h[i]] += 1;
      m2[i + 1 + h[i]] += 1;
    }
    i64 ans = 0;
    for (auto i : m1)
      if (m2[i.first])
        ans += i.second * m2[i.first];
    cout << ans;
  }

  clock_t end = clock();
  fprintf(stderr, "\\n%lf sec\\n", (double)(end - beg) / (CLOCKS_PER_SEC));
  return 0;
}


Error: TLE

Fixed submission:

/*
    author:@bhaskar_8998
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
typedef long long i64;
typedef vector<int> vi;
typedef vector<long> vl;
typedef pair<int, int> pii;
#define all(A) (A).begin(), (A).end()
#define pb push_back
#define MOD 1000000007
#define fastio()                                                               \\
  {                                                                            \\
    ios_base::sync_with_stdio(false);                                          \\
    cin.tie(NULL);                                                             \\
  }

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
  freopen("error.txt", "w", stderr);
#endif
  fastio();
  i64 ss[31];
  for (int i = 1, ts = 2; i <= 30; i++) {
    ss[i] = ts;
    ts *= 2;
  }
  clock_t beg = clock();
  int t = 1;
  // cin>>t;
  while (t--) {
    int n;
    ;
    cin >> n;
    int h[n];
    for (int i = 0; i < n; i++)
      cin >> h[i];
    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (abs(h[i] + h[j]) == abs(i - j) && i != j)
          ans += 1;
      }
    }
    cout << ans / 2;
  }

  clock_t end = clock();
  fprintf(stderr, "\\n%lf sec\\n", (double)(end - beg) / (CLOCKS_PER_SEC));
  return 0;
}