Problem p02691 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #pragma comment(linker, "/STACK:16777216")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("unswitch-loops")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,abm,mmx,avx,avx2,popcnt,tune=native")

using namespace std;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define ull unsigned long long
// #define double long double
#define int ll
// #define int ull
// #define int short
// #define int unsigned short
// #define int unsigned int
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define hash hash228
const int SZ = 2e5 + 4;
const double EPS = 1e-7;
const int mod = 1e9 + 7;
const int hashpow = 2;
const int inf = 2e9;

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

int n;
int a[SZ];

inline void read() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
}

struct mst {
  int len;
  vector<vector<int>> t;

  mst(vector<int> &a) {
    int y = ceil(log2(a.size()));
    len = (1 << y);
    t.resize(len << 1);

    build(a, 1, 0, len);
  }

  void build(vector<int> &a, int cur, int l, int r) {
    if (l == r - 1) {
      if (l < a.size()) {
        t[cur].push_back(a[l]);
      }
      return;
    }
    build(a, cur * 2, l, (l + r) >> 1);
    build(a, cur * 2 + 1, (l + r) >> 1, r);
    merge(t[cur * 2].begin(), t[cur * 2].end(), t[cur * 2 + 1].begin(),
          t[cur * 2 + 1].end(), back_inserter(t[cur]));
  }

  int get(int k, int l, int r) { return get(k, 1, l, r, 0, len); }

  int get(int k, int cur, int l, int r, int vl, int vr) {
    if (r <= vl || vr <= l) {
      return 0;
    }
    if (l <= vl && vr <= r) {
      int lo = 0, hi = t[cur].size();
      while (hi - lo > 1) {
        int m = lo + ((hi - lo) >> 1);
        if (t[cur][m] <= k) {
          lo = m;
        } else {
          hi = m;
        }
      }

      if (t[cur][lo] != k)
        return 0;
      int rrr = lo;
      lo = -1, hi = (int)t[cur].size() - 1;
      while (hi - lo > 1) {
        int m = lo + ((hi - lo) >> 1);
        if (t[cur][m] >= k) {
          hi = m;
        } else {
          lo = m;
        }
      }

      int lll = hi;

      return rrr - lll + 1;
    }
    int a = get(k, cur * 2, l, r, vl, (vl + vr) >> 1);
    int b = get(k, cur * 2 + 1, l, r, (vl + vr) >> 1, vr);
    return a + b;
  }
};

inline void solve() {
  vector<int> x(n);
  for (int i = 0; i < n; ++i) {
    x[i] = i - a[i];
  }

  mst t(x);

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += t.get(a[i] + i, i + 1, n);
  }

  cout << ans << "\\n";
}

signed main() {

#ifdef __LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif

  //    int t;
  //    cin >> t;
  //    for (int i = 0; i < t; ++i) {
  read();
  solve();
  //    }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #pragma comment(linker, "/STACK:16777216")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("unswitch-loops")
// #pragma GCC
// target("sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,abm,mmx,avx,avx2,popcnt,tune=native")

using namespace std;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

#define ll long long
#define ull unsigned long long
// #define double long double
// #define int ll
// #define int ull
// #define int short
// #define int unsigned short
// #define int unsigned int
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define hash hash228
const int SZ = 1e5 + 4;
const double EPS = 1e-7;
const int mod = 1e9 + 7;
const int hashpow = 2;
const int inf = 2e9;

struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};

int n;
int a[SZ];

inline void read() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
}

struct mst {
  int len;
  vector<vector<int>> t;

  mst(vector<int> &a) {
    int y = ceil(log2(a.size()));
    len = (1 << y);
    t.resize(len << 1);

    build(a, 1, 0, len);
  }

  void build(vector<int> &a, int cur, int l, int r) {
    if (l == r - 1) {
      if (l < a.size()) {
        t[cur].push_back(a[l]);
      }
      return;
    }
    build(a, cur * 2, l, (l + r) >> 1);
    build(a, cur * 2 + 1, (l + r) >> 1, r);
    merge(t[cur * 2].begin(), t[cur * 2].end(), t[cur * 2 + 1].begin(),
          t[cur * 2 + 1].end(), back_inserter(t[cur]));
  }

  int get(int k, int l, int r) { return get(k, 1, l, r, 0, len); }

  int get(int k, int cur, int l, int r, int vl, int vr) {
    if (r <= vl || vr <= l) {
      return 0;
    }
    if (l <= vl && vr <= r) {
      int lo = 0, hi = t[cur].size();
      while (hi - lo > 1) {
        int m = lo + ((hi - lo) >> 1);
        if (t[cur][m] <= k) {
          lo = m;
        } else {
          hi = m;
        }
      }

      if (t[cur][lo] != k)
        return 0;
      int rrr = lo;
      lo = -1, hi = (int)t[cur].size() - 1;
      while (hi - lo > 1) {
        int m = lo + ((hi - lo) >> 1);
        if (t[cur][m] >= k) {
          hi = m;
        } else {
          lo = m;
        }
      }

      int lll = hi;

      return rrr - lll + 1;
    }
    int a = get(k, cur * 2, l, r, vl, (vl + vr) >> 1);
    int b = get(k, cur * 2 + 1, l, r, (vl + vr) >> 1, vr);
    return a + b;
  }
};

inline void solve() {
  vector<int> x(n);
  for (int i = 0; i < n; ++i) {
    x[i] = i - a[i];
  }

  mst t(x);

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans += t.get(a[i] + i, i + 1, n);
  }

  cout << ans << "\\n";
}

signed main() {

#ifdef __LOCAL
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#else
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#endif

  //    int t;
  //    cin >> t;
  //    for (int i = 0; i < t; ++i) {
  read();
  solve();
  //    }

  return 0;
}