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

#include <bits/stdc++.h>
using namespace std;
struct ios_accelerate {
  ios_accelerate() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
  }
} CALL;

#define REP(i, n) for (auto(i) = (n)*0; (i) < (n); (i)++)
#define RNG(i, a, b) for (auto(i) = (a); (i) < (b); (i)++)
#define RBF(i, v) for (auto &(i) : (v))
#define ALL(x) (x).begin(), (x).end()

using LL = long long;
using UL = unsigned long;
using ULL = unsigned long long;
template <typename T> using V = vector<T>;
template <typename T> using VV = vector<vector<T>>;
template <typename T> using P = pair<T, T>;

const double PI = 3.141592653589793;
const int MOD = 1000000007;
const LL INF64 = static_cast<LL>(1e18) + 2;
const int INF = static_cast<int>(1e9) + 2;

template <typename T> inline void upsort(vector<T> &x) {
  sort(x.begin(), x.end());
}
template <typename T> inline void downsort(vector<T> &x) {
  sort(x.begin(), x.end(), greater<T>());
}
template <typename T> inline void col(T x) { cout << x << '\\n'; }
template <> inline void col(double x) {
  cout << fixed << setprecision(12) << x << '\\n';
}

signed main() {
  int n;
  cin >> n;
  V<int> wa(n), sa(n);
  REP(i, n) {
    int num = i + 1, a;
    cin >> a;
    wa[i] = num + a;
    sa[i] = num - a;
  }

  upsort(sa);
  LL ans = 0;
  RBF(w, wa) {
    auto lo = lower_bound(ALL(sa), w);
    auto up = upper_bound(ALL(sa), w);
    ans += distance(lo, up);
  }

  col(ans);
  return 0;
}


Error: TLE

Fixed submission:

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rng(i, a, b) for (int(i) = (a); (i) < (b); (i)++)
#define rbf(i, v) for (auto &(i) : (v))
#define All(x) (x).begin(), (x).end()
#define COL(x) cout << x << endl
#define CO(x) cout << x
// #define int long long

using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VS = vector<string>;
using VP = vector<PII>;

const double PI = 3.141592653589793;
const int MOD = 1000000007;
const LL INF64 = INT64_MAX >> 1;
const int INF = INT_MAX >> 1;

inline int sqr(int x) { return (x * x); }
inline void printd(double x) { printf("%.10lf\\n", x); }
inline void upsort(VI &x) { sort((x).begin(), (x).end()); }
inline void downsort(VI &x) { sort((x).begin(), (x).end(), greater<int>()); }
inline int absdiff(int a, int b) {
  return (a - b) * (static_cast<int>(a > b) * 2 - 1);
}
int gcd(int a, int b) { // 最大公約数
  if (a < b)
    return gcd(b, a);
  int r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}
inline int lcm(int a, int b) { return a * b / gcd(a, b); } // 最小公倍数
#pragma endregion

signed main() {
  int N;
  cin >> N;
  VP LR(N);
  int n = 1;
  rbf(lr, LR) {
    int a;
    cin >> a;
    lr = make_pair(n + a, n - a);
    n++;
  }
  sort(All(LR));

  VI L(N);
  rep(i, N) { L[i] = LR[i].first; }

  double ans = 0.0;
  rbf(lr, LR) {
    int R = lr.second;
    int low = distance(L.begin(), lower_bound(All(L), R));
    int high = distance(L.begin(), upper_bound(All(L), R));
    while (low < high) {
      if (lr.first == LR[low++].second)
        ans += 0.5;
      else
        ans += 1.0;
    }
  }

  COL(static_cast<int>(ans));
  return 0;
}