Problem p02691 (C++)

Buggy submission (status: Runtime Error):
/*
  Fuad Ashraful Mehmet Islam
  University of Asia Pacific,Bangladesh
  Date:6th May 2020
  ToDo:
  https://codeforces.com/problemset/problem/287/B
  https://atcoder.jp/contests/abc166/tasks/abc166_e
 */
#include <bits/stdc++.h>
#define dbg(x) cerr << __LINE__ << " where " << #x << " = " << x << endl
#define all(x) (x).begin(), (x).end()

using namespace std;
template <class T> inline void read(T &res) {
  res = 0;
  bool bo = 0;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  if (c == '-')
    bo = 1;
  else
    res = c - 48;
  while ((c = getchar()) >= '0' && c <= '9')
    res = (res << 3) + (res << 1) + (c - 48);
  if (bo)
    res = ~res + 1;
}
const int N = 2e5 + 5;
typedef long long ll;
int len, _, n, d, k, m, l, c, cs = 1;
vector<ll> tree[N];
ll a[N], cal[N];
void Solve() {
  read(n);
  for (int i = 1; i <= n; ++i)
    read(a[i]);
} /// end func

void build(int l, int r, int node = 1) {
  if (l == r) {
    tree[node].push_back(a[l] + l);
    return;
  } // base case
  int mid = (l + r) >> 1;
  build(l, mid, node << 1);
  build(mid + 1, r, node << 1 | 1);
  merge(all(tree[node << 1]), all(tree[node << 1 | 1]),
        back_inserter(tree[node]));
} /// end

ll query(int l, int r, int x, int y, ll val, int node = 1) {
  if (l > y or r < x)
    return 0;
  if (l >= x and r <= y) {
    ll lo = lower_bound(all(tree[node]), val) - tree[node].begin();
    ll up = upper_bound(all(tree[node]), val) - tree[node].begin();
    return up - lo;
  } /// overlaped

  int mid = (l + r) >> 1;
  return query(l, mid, x, y, val, node << 1) +
         query(mid + 1, r, x, y, val, node << 1 | 1);
} /// end func
int main() {
  // for(read(_);_;_--)
  Solve();
  build(1, n);
  ll ans = 0LL;

  for (int i = 1; i <= n; ++i) {
    ans += query(1, n, 1, i, i - a[i]);
  }
  cout << ans << "\n";
  return 0;
}

Fixed submission:
/*
  Fuad Ashraful Mehmet Islam
  University of Asia Pacific,Bangladesh
  Date:6th May 2020
  ToDo:
  https://codeforces.com/problemset/problem/287/B
  https://atcoder.jp/contests/abc166/tasks/abc166_e
 */
#include <bits/stdc++.h>
#define dbg(x) cerr << __LINE__ << " where " << #x << " = " << x << endl
#define all(x) (x).begin(), (x).end()

using namespace std;
template <class T> inline void read(T &res) {
  res = 0;
  bool bo = 0;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  if (c == '-')
    bo = 1;
  else
    res = c - 48;
  while ((c = getchar()) >= '0' && c <= '9')
    res = (res << 3) + (res << 1) + (c - 48);
  if (bo)
    res = ~res + 1;
}
const int N = 2e5 + 5;
typedef long long ll;
int len, _, n, d, k, m, l, c, cs = 1;
vector<ll> tree[N * 4];
ll a[N], cal[N];
void Solve() {
  read(n);
  for (int i = 1; i <= n; ++i)
    read(a[i]);
} /// end func

void build(int l, int r, int node = 1) {
  if (l == r) {
    tree[node].push_back(a[l] + l);
    return;
  } // base case
  int mid = (l + r) >> 1;
  build(l, mid, node << 1);
  build(mid + 1, r, node << 1 | 1);
  merge(all(tree[node << 1]), all(tree[node << 1 | 1]),
        back_inserter(tree[node]));
} /// end

ll query(int l, int r, int x, int y, ll val, int node = 1) {
  if (l > y or r < x)
    return 0;
  if (l >= x and r <= y) {
    ll lo = lower_bound(all(tree[node]), val) - tree[node].begin();
    ll up = upper_bound(all(tree[node]), val) - tree[node].begin();
    return up - lo;
  } /// overlaped

  int mid = (l + r) >> 1;
  return query(l, mid, x, y, val, node << 1) +
         query(mid + 1, r, x, y, val, node << 1 | 1);
} /// end func
int main() {
  // for(read(_);_;_--)
  Solve();
  build(1, n);
  ll ans = 0LL;

  for (int i = 1; i <= n; ++i) {
    ans += query(1, n, 1, i, i - a[i]);
  }
  cout << ans << "\n";
  return 0;
}