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

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define fr1(i, a, b) for (ll i = a; i >= b; i--)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define viii vector<pair<ll, pair<ll, ll>>>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define mp(i, j) make_pair(i, j)
#define fi first
#define se second
#define input(a, n) fr(i, 0, n) cin >> a[i]
#define output(a, n) fr(i, 0, n) cout << a[i] << endl
#define dbg cout << "hurr" << endl;
#define dbg2 cout << "hurr2" << endl;
#define md 1000000007
#define inf 999999999999999999
#define sp << " " <<
#define endl "\\n"
#define pi 3.14159265358979323846
#define fixd(x) cout << fixed << setprecision(x);
#define sz size()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define tr(container, it)                                                      \\
  for (typeof(container.begin()) it = container.begin();                       \\
       it != container.end(); it++)
bool compare(pair<ll, string> p1, pair<ll, string> p2) { return p2.fi > p1.fi; }
void solve() {
  ll n;
  cin >> n;
  ll a[n];
  map<ll, ll> ma, mb;
  ll ans = 0;
  fr(i, 0, n) {
    cin >> a[i];
    ma[i + a[i]]++;
    mb[i - a[i]]++;
  }
  tr(ma, it) {
    // cout<<it->se<<" "<<mb[it->fi]<<endl;
    ans += (mb[it->fi]) * (it->se);
  }
  cout << ans << endl;
}
int main() {
  fast;
  ll t = 1;
  // cin>>t;
  fr(zz, 1, t + 1) { solve(); }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define fr(i, a, b) for (ll i = a; i < b; i++)
#define fr1(i, a, b) for (ll i = a; i >= b; i--)
#define ll long long int
#define ld long double
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define viii vector<pair<ll, pair<ll, ll>>>
#define pb push_back
#define all(v) (v).begin(), (v).end()
#define mp(i, j) make_pair(i, j)
#define fi first
#define se second
#define input(a, n) fr(i, 0, n) cin >> a[i]
#define output(a, n) fr(i, 0, n) cout << a[i] << endl
#define dbg cout << "hurr" << endl;
#define dbg2 cout << "hurr2" << endl;
#define md 1000000007
#define inf 999999999999999999
#define sp << " " <<
#define endl "\\n"
#define pi 3.14159265358979323846
#define fixd(x) cout << fixed << setprecision(x);
#define sz size()
#define yes cout << "Yes" << endl
#define no cout << "No" << endl
#define tr(container, it)                                                      \\
  for (auto it = container.begin(); it != container.end(); it++)
bool compare(pair<ll, string> p1, pair<ll, string> p2) { return p2.fi > p1.fi; }
void solve() {
  ll n;
  cin >> n;
  ll a[n];
  input(a, n);
  ll ans = 0;
  fr(i, 0, n) {
    fr(j, i, n) {
      if (abs(i - j) == a[i] + a[j]) {
        ans++;
      }
    }
  }
  cout << ans << endl;
}
int main() {
  fast;
  ll t = 1;
  // cin>>t;
  fr(zz, 1, t + 1) { solve(); }
}