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

#include <bits/stdc++.h>
using namespace std;
#define se second
#define fi first
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define ppll pair<pll, pll>
#define pii pair<int, int>
#define pb push_back
#define mod 1000000007
#define N 2000007
ll power(ll a, ll b) {
  ll res = 1ll;
  while (b > 0) {
    if (b % 2 != 0)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res % mod;
}
ll ncr(ll n, ll k) {
  if (k > n - k)
    k = n - k;
  ll pro = 1;
  for (ll i = 0; i < k; i++) {
    pro = (pro * (n - i)) % mod;
    pro /= (i + 1);
  }
  return (pro) % mod;
}

void swap(ll *a, ll *b) {
  ll temp = *a;
  *a = *b;
  *b = temp;
}
vector<ll> prime;
void seive() {
  vector<bool> isprime(1000001, true);
  for (int i = 2; i * i <= 1000000; i++) {
    if (isprime[i]) {
      for (int j = i * i; j <= 1000000; j += i) {
        isprime[j] = false;
      }
    }
  }
  for (int i = 2; i <= 1000000; i++) {
    if (isprime[i])
      prime.push_back(i);
  }
}
vector<ll> parent;
ll find(ll p) {
  if (parent[p] == p)
    return parent[p];
  return parent[p] = find(parent[p]);
}
void unun(ll a, ll b) {
  ll r1 = find(a);
  ll r2 = find(b);
  if (r1 != r2)
    parent[r2] = r1;
}
void solve() {
  ll n;
  cin >> n;
  vector<ll> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  ll cnt = 0;
  map<ll, ll> mp;
  mp[arr[0]]++;
  for (int i = 1; i < n; i++) {
    if (mp.find(i - arr[i]) != mp.end())
      cnt += mp[i - arr[i]];
    mp[i + arr[i]]++;
  }
  cout << cnt;
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
    // cout<<endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define se second
#define fi first
#define ll long long
#define ld long double
#define pll pair<ll, ll>
#define plll pair<ll, pll>
#define ppll pair<pll, pll>
#define pii pair<int, int>
#define pb push_back
#define mod 1000000007
#define N 2000007
ll power(ll a, ll b) {
  ll res = 1ll;
  while (b > 0) {
    if (b % 2 != 0)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b /= 2;
  }
  return res % mod;
}
ll ncr(ll n, ll k) {
  if (k > n - k)
    k = n - k;
  ll pro = 1;
  for (ll i = 0; i < k; i++) {
    pro = (pro * (n - i)) % mod;
    pro /= (i + 1);
  }
  return (pro) % mod;
}

void swap(ll *a, ll *b) {
  ll temp = *a;
  *a = *b;
  *b = temp;
}
vector<ll> prime;
void seive() {
  vector<bool> isprime(1000001, true);
  for (int i = 2; i * i <= 1000000; i++) {
    if (isprime[i]) {
      for (int j = i * i; j <= 1000000; j += i) {
        isprime[j] = false;
      }
    }
  }
  for (int i = 2; i <= 1000000; i++) {
    if (isprime[i])
      prime.push_back(i);
  }
}
vector<ll> parent;
ll find(ll p) {
  if (parent[p] == p)
    return parent[p];
  return parent[p] = find(parent[p]);
}
void unun(ll a, ll b) {
  ll r1 = find(a);
  ll r2 = find(b);
  if (r1 != r2)
    parent[r2] = r1;
}
void solve() {
  ll n;
  cin >> n;
  vector<ll> arr(n);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  ll cnt = 0;
  for (int i = 1; i < n; i++) {
    // if(arr[i]>=i) continue;
    for (int j = 0; j < i; j++) {
      if (abs(i - j) == (arr[i] + arr[j]))
        cnt++;
    }
  }
  cout << cnt;
}

int main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  t = 1;
  // cin>>t;
  while (t--) {
    solve();
    // cout<<endl;
  }
  return 0;
}