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

#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef long double ld;
typedef int32_t i32;

#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define pf push_front

#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define RFOR(i, a, b) for (ll i = a; i >= b; i--)
#define FILL(a, b, c)                                                          \\
  for (ll i = 0; i < c; i++)                                                   \\
    a[i] = b;

#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()

#define endl "\\n"
#define setpri(x) setprecision(x)
#define vp(x) cout << #x << " : " << (x) << " | "
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define forf(i, a, b) for (int i = a; i < b; i++)

#define INF 1e17 + 4
#define MINF
#define MAX_INT 100000
#define MOD 1000000007
ll mod(ll n) {
  n %= MOD;
  if (n < 0)
    return n + MOD;
  else
    return n;
}
vector<int> adj[1000002];
int vis[1000002], dep[1000002], siz[1000002];

// void dfs(ll cur,ll par)
// {
//     vis[cur]=1;siz[cur]=1;
//     for(auto x:adj[cur])
//     {
//         if(vis[x]!=1)
//         {
//             dep[x]=dep[cur]+1;
//             dfs(x,cur);
//             siz[cur]+=siz[x];
//         }
//     }
// }

// bool sortfunc(const pair<int,int> &a, const pair<int,int> &b)
// {
//     return ;
// }

void solve() {
  int n;
  cin >> n;
  int a[n + 1];
  a[0] = 0;
  forf(i, 1, n + 1) cin >> a[i];

  unordered_map<int, int> m;
  forf(i, 1, n + 1) m[i + a[i]]++;

  int ans = 0;
  forf(i, 1, n + 1) { ans += m[(i - a[i])]; }
  cout << ans;
}

signed main() {
  FAST;

  // ll t;cin>>t;while(t--)
  solve();

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);

typedef long long ll;
typedef long double ld;
typedef int32_t i32;

#define int long long
#define mp make_pair
#define ff first
#define ss second
#define pb push_back
#define pf push_front

#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define RFOR(i, a, b) for (ll i = a; i >= b; i--)
#define FILL(a, b, c)                                                          \\
  for (ll i = 0; i < c; i++)                                                   \\
    a[i] = b;

#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define allr(v) (v).rbegin(), (v).rend()

#define endl "\\n"
#define setpri(x) setprecision(x)
#define vp(x) cout << #x << " : " << (x) << " | "
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
#define forf(i, a, b) for (int i = a; i < b; i++)

#define INF 1e17 + 4
#define MINF
#define MAX_INT 100000
#define MOD 1000000007
ll mod(ll n) {
  n %= MOD;
  if (n < 0)
    return n + MOD;
  else
    return n;
}
vector<int> adj[1000002];
int vis[1000002], dep[1000002], siz[1000002];

// void dfs(ll cur,ll par)
// {
//     vis[cur]=1;siz[cur]=1;
//     for(auto x:adj[cur])
//     {
//         if(vis[x]!=1)
//         {
//             dep[x]=dep[cur]+1;
//             dfs(x,cur);
//             siz[cur]+=siz[x];
//         }
//     }
// }

// bool sortfunc(const pair<int,int> &a, const pair<int,int> &b)
// {
//     return ;
// }

void solve() {
  int n;
  cin >> n;
  int a[n + 1];
  a[0] = 0;
  forf(i, 1, n + 1) cin >> a[i];
  int ans = 0;
  forf(i, 1, n) forf(j, i + 1, n + 1) {
    if (abs(i - j) == a[i] + a[j])
      ans++;
  }
  cout << ans;
}

signed main() {
  FAST;

  // ll t;cin>>t;while(t--)
  solve();

  return 0;
}