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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

//....................................................................................................................//

using std::cin;
using std::cout;
using std::deque;
using std::get;
using std::ifstream;
using std::ios_base;
using std::list;
using std::lower_bound;
using std::make_pair;
using std::make_tuple;
using std::map;
using std::max;
using std::max_element;
using std::min;
using std::min_element;
using std::multimap;
using std::multiset;
using std::ofstream;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::swap;
using std::tuple;
using std::unordered_map;
using std::unordered_multimap;
using std::unordered_set;
using std::upper_bound;
using std::vector;

//....................................................................................................................//

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned long ul;

[[maybe_unused]] const string endl = "\\n";
[[maybe_unused]] const string sep = " ";
[[maybe_unused]] const string yes = "YES\\n";
[[maybe_unused]] const string no = "NO\\n";
[[maybe_unused]] const string ng = "NG\\n";
[[maybe_unused]] const string ok = "OK\\n";
[[maybe_unused]] const double pi = acos(-1);

//....................................................................................................................//

int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout.precision(10);
  int n;
  cin >> n;
  vector<int> H(n);
  for (int i = 0; i < n; ++i) {
    cin >> H[i];
  }
  map<int, int> M;
  for (int i = 0; i < n; ++i) {
    ++M[i - H[i]];
  }
  ll result = 0;
  for (int i = 0; i < n; ++i) {
    result += M[i + H[i]];
  }
  cout << result;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

//....................................................................................................................//

using std::cin;
using std::cout;
using std::deque;
using std::get;
using std::ifstream;
using std::ios_base;
using std::list;
using std::lower_bound;
using std::make_pair;
using std::make_tuple;
using std::map;
using std::max;
using std::max_element;
using std::min;
using std::min_element;
using std::multimap;
using std::multiset;
using std::ofstream;
using std::pair;
using std::queue;
using std::set;
using std::stack;
using std::string;
using std::swap;
using std::tuple;
using std::unordered_map;
using std::unordered_multimap;
using std::unordered_set;
using std::upper_bound;
using std::vector;

//....................................................................................................................//

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned long ul;

[[maybe_unused]] const string endl = "\\n";
[[maybe_unused]] const string sep = " ";
[[maybe_unused]] const string yes = "YES\\n";
[[maybe_unused]] const string no = "NO\\n";
[[maybe_unused]] const string ng = "NG\\n";
[[maybe_unused]] const string ok = "OK\\n";
[[maybe_unused]] const double pi = acos(-1);

//....................................................................................................................//

int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  cout.precision(10);
  int n;
  cin >> n;
  vector<ll> H(n);
  for (int i = 0; i < n; ++i) {
    ll a;
    cin >> H[i];
  }
  multiset<ll> S;
  for (int i = 0; i < n; ++i) {
    S.insert(i - H[i]);
  }
  ll result = 0;
  for (int i = 0; i < n; ++i) {
    result += S.count(i + H[i]);
  }
  cout << result;
}