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

/*********************************************************************|
|---------|    /\\  ....                  ....                         |
     |        /  \\   \\                    /  /------| ----- |-------| |
     |       /    \\   \\                  /  /           |   |         |
     |      /      \\   \\       /\\       /  |            |   |         |
     |     /        \\   \\     /  \\     /   |---|        |   |----|    |
     |    /__________\\   \\   /    \\   /        |---|    |   |         |
     |   /            \\   \\ /      \\ /              |   |   |         |
     |  /              \\   |        |               /   |   |         |
     _ __              __ ...      ...      |------/  ----- _         |
                                                                      |
        Md Tawsif Rahman                                              |
        Institute Of Information Technology                           |
        Jahangirnagar University                                      |
                                                                      |
**********************************************************************/
#include <bits/stdc++.h>

using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define pii pair<int, int>
#define xx first
#define yy second
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pii>
#define inf 100000000
#define f(i, c, n) for (i = c; i < n; i++)
#define fe(i, c, n) for (i = c; i <= n; i++)
#define fr(i, n, c) for (i = n - 1; i >= c; i--)
#define vbe(a) a.begin(), a.end()
#define vsort(a) sort(a.begin(), a.end())
#define mem(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define srti(a, n) sort(a, a + n)
#define srtd(a, n) sort(a, a + n, greater<int>())
#define LB(a, x) (lower_bound(vbe(a), x) - a.begin())
#define UB(a, x) (upper_bound(vbe(a), x) - a.begin())

#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)

#define fast                                                                   \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
int i, j, temp;
// int convert_str_to_int(string s) {
//     stringstream str(s);
//     int x = 0;
//     str >> x;
//     return x;
// }

/********************|
                     |
   START HERE        |
                     |
*********************/
map<int, int> m1, m2;

void solve() {
  int n;
  cin >> n;
  int arr[n + 10];
  f(i, 1, n + 1) { cin >> arr[i]; }
  int val[n + 10];

  f(i, 1, n + 1) {
    val[i] = i + arr[i];
    m1[i + arr[i]]++;
    m2[i - arr[i]]++;
  }
  ll cnt = 0;
  f(i, 1, n + 1) { cnt += m2[val[i]]; }
  cout << cnt << endl;
}

int main() {
  fast;
  // int t; cin >> t; while(t--)
  solve();
}

Error: TLE

Fixed submission:

/*********************************************************************|
|---------|    /\\  ....                  ....                         |
     |        /  \\   \\                    /  /------| ----- |-------| |
     |       /    \\   \\                  /  /           |   |         |
     |      /      \\   \\       /\\       /  |            |   |         |
     |     /        \\   \\     /  \\     /   |---|        |   |----|    |
     |    /__________\\   \\   /    \\   /        |---|    |   |         |
     |   /            \\   \\ /      \\ /              |   |   |         |
     |  /              \\   |        |               /   |   |         |
     _ __              __ ...      ...      |------/  ----- _         |
                                                                      |
        Md Tawsif Rahman                                              |
        Institute Of Information Technology                           |
        Jahangirnagar University                                      |
                                                                      |
**********************************************************************/
#include <bits/stdc++.h>

using namespace std;
#define ll long long int
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define pii pair<int, int>
#define xx first
#define yy second
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pii>
#define inf 100000000
#define f(i, c, n) for (i = c; i < n; i++)
#define fe(i, c, n) for (i = c; i <= n; i++)
#define fr(i, n, c) for (i = n - 1; i >= c; i--)
#define vbe(a) a.begin(), a.end()
#define vsort(a) sort(a.begin(), a.end())
#define mem(a) memset(a, 0, sizeof(a))
#define memn(a) memset(a, -1, sizeof(a))
#define srti(a, n) sort(a, a + n)
#define srtd(a, n) sort(a, a + n, greater<int>())
#define LB(a, x) (lower_bound(vbe(a), x) - a.begin())
#define UB(a, x) (upper_bound(vbe(a), x) - a.begin())

#define si(a) scanf("%d", &a)
#define sii(a, b) scanf("%d%d", &a, &b)
#define siii(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define sl(a) scanf("%lld", &a)
#define sll(a, b) scanf("%lld%lld", &a, &b)
#define slll(a, b, c) scanf("%lld%lld%lld", &a, &b, &c)

#define fast                                                                   \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
int i, j, temp;
// int convert_str_to_int(string s) {
//     stringstream str(s);
//     int x = 0;
//     str >> x;
//     return x;
// }

/********************|
                     |
   START HERE        |
                     |
*********************/

void solve() {
  int n;
  cin >> n;
  int arr[n + 10];
  f(i, 0, n) { cin >> arr[i]; }
  int cnt = 0;
  f(i, 0, n) {
    f(j, i + 1, n) {
      if (abs(j - i) == arr[i] + arr[j]) {

        cnt++;
      }
    }
  }
  cout << cnt << endl;
}

int main() {
  fast;
  // int t; cin >> t; while(t--)
  solve();
}