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

// Catchharsh
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define F first
#define S second
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\\n'
#define see(x) cout << #x << " = " << x << endl
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
// const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<pair<ll, ll>, null_type, less<pair<ll, ll>>, rb_tree_tag,               \\
       tree_order_statistics_node_update>
inline ll add(ll a, ll b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}
inline ll sub(ll a, ll b) {
  a -= b;
  if (a < 0)
    a += MOD;
  return a;
}
inline ll mul(ll a, ll b) { return (a * 1ll * b) % MOD; }
ll power(ll base, ll exponent) {
  ll ans = 1;
  // base%=MOD;
  // exponent%=(MOD-1);
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base);
    base = (1LL * base * base);
    exponent >>= 1;
  }
  return ans;
}
int32_t main() {
  IOS;

  ll n, m, i, j, k;
  cin >> n;
  vector<ll> v(n);
  for (i = 0; i < n; i++)
    cin >> v[i];
  ordered_set pre, suf;
  pre.insert({1 + v[0], 1});
  ll ans = 0;
  for (i = 1; i < n; i++) {
    ll val = i + 1 - v[i];
    ll lef, rr;
    lef = pre.order_of_key({val, INF});
    rr = pre.order_of_key({val, -INF});
    ans += abs(lef - rr);
    // cout<<lef<<" "<<rr<<" "<<i<<endl;
    pre.insert({i + 1 + v[i], i + 1});
  }
  suf.insert({n - v[n - 1], i + 1});
  for (i = n - 2; i >= 0; i--) {
    ll val = i + 1 + v[i];
    ll lef, rr;
    lef = suf.order_of_key({val, INF});
    rr = suf.order_of_key({val, -INF});
    ans += abs(lef - rr);
    suf.insert({i + 1 - v[i], i + 1});
  }
  ans /= 2;
  cout << ans;

  cerr << endl
       << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\\n';
}

Error: TLE

Fixed submission:

// Catchharsh
#include <bits/stdc++.h>
#define ll int
#define ld long double
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define F first
#define S second
#define sz(x) x.size()
#define all(x) x.begin(), x.end()
#define pb push_back
#define endl '\\n'
#define see(x) cout << #x << " = " << x << endl
const ll MOD = 1e9 + 7;
const ll INF = 1011111111;
// const ll LLINF = 1000111000111000111LL;
const ld EPS = 1e-10;
const ld PI = 3.14159265358979323;
using namespace std;
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
// #define ordered_set tree<ll, null_type,less<ll>,
// rb_tree_tag,tree_order_statistics_node_update>
inline ll add(ll a, ll b) {
  a += b;
  if (a >= MOD)
    a -= MOD;
  return a;
}
inline ll sub(ll a, ll b) {
  a -= b;
  if (a < 0)
    a += MOD;
  return a;
}
inline ll mul(ll a, ll b) { return (a * 1ll * b) % MOD; }
ll power(ll base, ll exponent) {
  ll ans = 1;
  // base%=MOD;
  // exponent%=(MOD-1);
  while (exponent != 0) {
    if (exponent & 1)
      ans = (1LL * ans * base);
    base = (1LL * base * base);
    exponent >>= 1;
  }
  return ans;
}
int32_t main() {
  IOS;

  ll n, m, i, j, k;
  cin >> n;
  vector<ll> v(n);
  for (i = 0; i < n; i++)
    cin >> v[i];
  multiset<ll> pre, suf;
  pre.insert(1 + v[0]);
  ll ans = 0;
  for (i = 1; i < n; i++) {
    ans += pre.count(i + 1 - v[i]);
    pre.insert(i + 1 + v[i]);
  }
  suf.insert(n - v[n - 1]);
  for (i = n - 2; i >= 0; i--) {
    ans += suf.count(i + 1 + v[i]);
    suf.insert(i + 1 - v[i]);
  }
  ans /= 2;
  cout << ans;

  cerr << endl
       << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms"
       << '\\n';
}