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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define repr(i, n) for (int i = n; i >= 0; i--)
#define reprl(i, n) for (ll i = n; i >= 0; i--)
#define all(v) v.begin(), v.end()

const ll mod = 1000000007;

int countBits(int in) {
  int res = 0;
  for (int i = 0; i < 10; i++) {
    int msk = 1 << i;
    if ((in & msk) != 0) {
      res++;
    }
  }
  return res;
}

template <typename T> void show2dVector(vector<vector<T>> &v) {
  for (int i = 0; i < v.size(); i++) {
    int m = v[i].size();
    cout << i << " : ";
    for (int j = 0; i < m; i++) {
      cout << v[i][j] << " ";
    }
    cout << endl;
  }
}

void bfs(const vector<vector<int>> &g, int v, vector<bool> &seen) {
  seen[v] = true;
  cout << v << " ";
  for (auto next : g[v]) {
    if (seen[next]) {
      continue;
    }
    bfs(g, next, seen);
  }
}

bool dfs(vector<vector<int>> &g, int start, int goal, vector<bool> &seen) {
  bool res = false;
  seen[start] = true;
  if (start == goal) {
    return true;
  }
  for (auto next : g[start]) {
    if (seen[next]) {
      continue;
    }
    res = dfs(g, next, goal, seen);
    if (res) {
      break;
    }
  }
  return res;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
bool isLowerCase(char c) { return (c >= 'a' && c <= 'z'); }
const string outputYesNo(bool ans) { return (ans ? "Yes" : "No"); }
int main() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i < n + 1; i++) {
    cin >> a[i];
  }
  ll ans = 0;
  map<int, vector<int>> mp;
  for (int i = 1; i < n + 1; i++) {
    mp[i - a[i]].push_back(i);
  }
  for (int i = 1; i < n + 1; i++) {
    int val = i + a[i];
    ans += mp[val].size();
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;

#define repr(i, n) for (int i = n; i >= 0; i--)
#define reprl(i, n) for (ll i = n; i >= 0; i--)
#define all(v) v.begin(), v.end()

const ll mod = 1000000007;

int countBits(int in) {
  int res = 0;
  for (int i = 0; i < 10; i++) {
    int msk = 1 << i;
    if ((in & msk) != 0) {
      res++;
    }
  }
  return res;
}

template <typename T> void show2dVector(vector<vector<T>> &v) {
  for (int i = 0; i < v.size(); i++) {
    int m = v[i].size();
    cout << i << " : ";
    for (int j = 0; i < m; i++) {
      cout << v[i][j] << " ";
    }
    cout << endl;
  }
}

void bfs(const vector<vector<int>> &g, int v, vector<bool> &seen) {
  seen[v] = true;
  cout << v << " ";
  for (auto next : g[v]) {
    if (seen[next]) {
      continue;
    }
    bfs(g, next, seen);
  }
}

bool dfs(vector<vector<int>> &g, int start, int goal, vector<bool> &seen) {
  bool res = false;
  seen[start] = true;
  if (start == goal) {
    return true;
  }
  for (auto next : g[start]) {
    if (seen[next]) {
      continue;
    }
    res = dfs(g, next, goal, seen);
    if (res) {
      break;
    }
  }
  return res;
}

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
bool isLowerCase(char c) { return (c >= 'a' && c <= 'z'); }
const string outputYesNo(bool ans) { return (ans ? "Yes" : "No"); }
int main() {
  int n;
  cin >> n;
  vector<ll> a(n + 1);
  for (int i = 1; i < n + 1; i++) {
    cin >> a[i];
  }
  ll ans = 0;
  for (int i = 1; i < n; i++) {
    if (n - i <= a[i])
      continue;
    for (int j = i + 1; j < n + 1; j++) {
      if (j - i == a[j] + a[i]) {
        ans++;
      }
    }
  }
  cout << ans << endl;
  return 0;
}