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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define int long long
using P = pair<int, int>;

#define LOG(variable) cout << #variable ":\\t" << (variable) << endl
#define LOGCON(i, container)                                                   \\
  for (int(i) = 0; (i) < (container).size(); ++(i))                            \\
  cout << (i) << ":\\t" << (container)[(i)] << endl
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)       // from n to 0
#define REPRS(i, n, r) for (int i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj)                                                       \\
  for (auto itr = (obj).begin(); itr != (obj).end(); ++itr)
#define REPROBJ(itr, obj)                                                      \\
  for (auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
int in() {
  int x;
  cin >> x;
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
/***** MAIN *****/
signed main() {
  int n = in();
  int weis[n];
  REP(i, n) { cin >> weis[i]; }
  vector<int> left_v(n), right_v(n);
  REP(i, n) {
    left_v[i] = i - weis[i];
    right_v[i] = i + weis[i];
  }
  SORT(left_v);
  SORT(right_v);
  int ans = 0;
  REP(i, n) {
    int right = right_v[i];
    auto itr = lower_bound(ALL(left_v), right);
    if (itr == left_v.end())
      continue;
    auto itr2 = upper_bound(itr, left_v.end(), right);
    int counter = distance(itr, itr2);
    // int index = itr - left_v.begin();
    // int counter = 0;
    // REPS(j, index, n) {
    //   if(left_v[j] != right) break;
    //   ++counter;
    // }
    ans += counter;
  }
  cout << ans;
  cout << "\\n";
  return 0;
}
/***** MAIN *****/


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
// #define int long long
using P = pair<int, int>;

#define LOG(variable) cout << #variable ":\\t" << (variable) << endl
#define LOGCON(i, container)                                                   \\
  for (int(i) = 0; (i) < (container).size(); ++(i))                            \\
  cout << (i) << ":\\t" << (container)[(i)] << endl
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPS(i, r, n) for (int i = (r); i < (n); ++i)
#define REPR(i, n) for (int i = (n); i >= 0; --i)       // from n to 0
#define REPRS(i, n, r) for (int i = (n); i >= (r); --i) // from n to r
#define REPOBJ(itr, obj)                                                       \\
  for (auto itr = (obj).begin(); itr != (obj).end(); ++itr)
#define REPROBJ(itr, obj)                                                      \\
  for (auto itr = (obj).rbegin(), e = (obj).rend(); itr != e; ++itr)
#define COUTB(x) cout << (x) << "\\n"
#define COUTS(x) cout << (x) << " "
#define PB push_back
#define SORT(obj) sort((obj).begin(), (obj).end())
#define SORTR(obj) sort((obj).begin(), (obj).end(), greater<>())
#define ALL(obj) (obj).begin(), (obj).end()
#define MOD 1000000007
#define PI (acos(-1))
struct mint {
  ll x; // typedef long long ll;
  mint(ll x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  mint &operator+=(const mint a) {
    if ((x += a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator-=(const mint a) {
    if ((x += MOD - a.x) >= MOD)
      x -= MOD;
    return *this;
  }
  mint &operator*=(const mint a) {
    (x *= a.x) %= MOD;
    return *this;
  }
  mint operator+(const mint a) const { return mint(*this) += a; }
  mint operator-(const mint a) const { return mint(*this) -= a; }
  mint operator*(const mint a) const { return mint(*this) *= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1)
      a *= *this;
    return a;
  }

  // for prime MOD
  mint inv() const { return pow(MOD - 2); }
  mint &operator/=(const mint a) { return *this *= a.inv(); }
  mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream &operator>>(istream &is, const mint &a) { return is >> a.x; }
ostream &operator<<(ostream &os, const mint &a) { return os << a.x; }
int in() {
  int x;
  cin >> x;
  return x;
}
string stin() {
  string s;
  cin >> s;
  return s;
}
/***** MAIN *****/
signed main() {
  int n = in();
  int weis[n];
  REP(i, n) { scanf("%d", &weis[i]); }
  ll ans = 0;
  REP(i, n) {
    int counter = 0;
    REPS(j, i + weis[i] + 1, n) {
      if (j - i == weis[i] + weis[j])
        ++counter;
    }
    ans += counter;
    // if(counter > 1) {
    //   // ans += counter * (counter - 1) / 2;
    //   ans += counter;
    // }
  }
  cout << ans;
  cout << "\\n";
  return 0;
}
/***** MAIN *****/