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

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++) // REP(i, 5) cout<<i;
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define MOD 1000000007 // 10^9+7
// #define MOD 1

ll gcd(ll a, ll b) {
  ll tmp;
  if (b > a) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a % b != 0) {
    tmp = b;
    b = a % b;
    a = tmp;
  }
  return b;
}

const int MAX = 1000;

ll table[MAX][MAX];

// テーブルを作る前処理
void COMinit() {
  table[0][0] = table[0][1] = 1;
  FOR(i, 1, MAX - 1) {
    FOR(j, 0, i + 1) {
      if (j == 0)
        table[i][j] = 1;
      else if (j == i + 1)
        table[i][j] = 1;
      else
        table[i][j] = table[i - 1][j - 1] + table[i - 1][j];
    }
  }
}

// 二項係数計算
ll COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return table[n - 1][k];
}

void sosuu(ll n, vector<ll> &v) {

  vector<ll> num(n, 0);

  FOR(i, 1, sqrt(n)) {
    if (num[i] == 0) {
      FOR(j, 0, n / i) { num[i + (j * i)] = 1; }
    }
  }
  REP(i, n) {
    if (num[i] == 0)
      v.push_back(i + 1);
  }
}

ll SORT(int n, vector<pair<ll, ll>> &v, int start) {
  sort(v.begin() + start, v.end());
  return v[n - 1].first;
}

int main(void) {

  ll n;
  cin >> n;
  map<ll, ll> m;
  vector<ll> x(n), y(n);

  REP(i, n) {
    ll a;
    cin >> a;
    x[i] = i + a;
    y[i] = i - a;
  }

  ll ans = 0;

  REP(i, n) m[x[i]]++;
  REP(i, n) {
    if (m[y[i]] != 0)
      ans += m[y[i]];
  }

  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
using ll = long long int;

#define REP(i, n) for (ll i = 0; i < (ll)(n); i++) // REP(i, 5) cout<<i;
#define REPD(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define FOR(i, a, b) for (ll i = (a); i <= (b); i++)
#define MOD 1000000007 // 10^9+7
// #define MOD 1

ll gcd(ll a, ll b) {
  ll tmp;
  if (b > a) {
    tmp = a;
    a = b;
    b = tmp;
  }
  while (a % b != 0) {
    tmp = b;
    b = a % b;
    a = tmp;
  }
  return b;
}

const int MAX = 1000;

ll table[MAX][MAX];

// テーブルを作る前処理
void COMinit() {
  table[0][0] = table[0][1] = 1;
  FOR(i, 1, MAX - 1) {
    FOR(j, 0, i + 1) {
      if (j == 0)
        table[i][j] = 1;
      else if (j == i + 1)
        table[i][j] = 1;
      else
        table[i][j] = table[i - 1][j - 1] + table[i - 1][j];
    }
  }
}

// 二項係数計算
ll COM(ll n, ll k) {
  if (n < k)
    return 0;
  if (n < 0 || k < 0)
    return 0;
  return table[n - 1][k];
}

void sosuu(ll n, vector<ll> &v) {

  vector<ll> num(n, 0);

  FOR(i, 1, sqrt(n)) {
    if (num[i] == 0) {
      FOR(j, 0, n / i) { num[i + (j * i)] = 1; }
    }
  }
  REP(i, n) {
    if (num[i] == 0)
      v.push_back(i + 1);
  }
}

ll SORT(int n, vector<pair<ll, ll>> &v, int start) {
  sort(v.begin() + start, v.end());
  return v[n - 1].first;
}

int main(void) {

  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<pair<ll, ll>> v(n);

  REP(i, n) {
    ll x;
    cin >> x;
    a[i] = x;
    v[i] = make_pair(x, i);
  }

  ll ans = 0;

  REP(i, n) {
    ll start, end;
    start = i + a[i];
    if (start < n) {
      ll ma = SORT(n, v, start) + a[i];
      end = start + ma;
      // cout<<ma<<endl;
      if (end >= n)
        end = n - 1;
      // cout<<"a: "<<start<<" "<<end<<endl;

      FOR(j, start, end) {
        if ((a[i] + a[j]) == abs(i - j)) {
          ans++;
          // cout<<i<<" "<<j<<endl;
        }
      }
    }
  }

  cout << ans << endl;
  return 0;
}