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

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

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#define itrout(x)                                                              \\
  for (int i = 0; i < x.size(); i++) {                                         \\
    cout << x[i] << (i == x.size() - 1 ? "\\n" : " ");                          \\
  }
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define debugbit(x, n)                                                         \\
  cerr << #x << " = " << bitset<n>(x) << " (L" << __LINE__ << ")"              \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define debugbit(x, n)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

class SegmentTree {
  vector<int> nodes_;
  int size_;
  inline static constexpr int INIT = 0;

  int query(const int left, const int right, const int index, const int l,
            const int r) const {
    if (r <= left || right <= l)
      return INIT;
    if (left <= l && r <= right)
      return nodes_[index];

    return query(left, right, 2 * index + 1, l, (l + r) / 2) +
           query(left, right, 2 * index + 2, (l + r) / 2, r);
  }

public:
  SegmentTree(const int maxSize) {
    size_ = 1;
    while (size_ < maxSize)
      size_ *= 2;
    nodes_ = vector<int>(2 * size_, INIT);
  };

  void update(int index, const int value) {
    index += size_ - 1;
    nodes_[index] += value;
    while (index > 0) {
      index = (index - 1) / 2;
      nodes_[index] = nodes_[2 * index + 1] + nodes_[2 * index + 2];
    }
  }

  // get value in [left, right)
  int get(const int left, const int right) const {
    return query(left, right, 0, 0, size_);
  }
};

void solve(long long N, long long Q, vector<ll> c, std::vector<long long> l,
           std::vector<long long> r) {
  vector<int> posOfLast(N + 1, -1);
  vector<int> answer(Q);
  vector<vector<int>> queries(N + 1);
  rep(i, 0, Q) { queries[r[i]].push_back(i); }
  SegmentTree seg(N + 1);
  rep(i, 0, N) {
    if (posOfLast[c[i]] != -1)
      seg.update(posOfLast[c[i]], -1);
    posOfLast[c[i]] = i + 1;
    seg.update(posOfLast[c[i]], 1);

    for (auto j : queries[i + 1]) {
      answer[j] += seg.get(l[j], r[j]);
    }
  }
  rep(i, 0, Q) cout << answer[i] + 1 << endl;
}

signed main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  long long N;
  scanf("%lld", &N);
  long long Q;
  scanf("%lld", &Q);
  vector<ll> c(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
  }
  std::vector<long long> l(Q);
  std::vector<long long> r(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &l[i]);
    scanf("%lld", &r[i]);
  }
  solve(N, Q, move(c), std::move(l), std::move(r));
  return 0;
}


Error: 0

Fixed submission:

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

#define rep(i, x, y) for (ll i = (x); i < (y); i++)
#define rrep(i, x, y) for (ll i = (ll)(y)-1; i >= (x); i--)
#define all(x) (x).begin(), (x).end()
#define itrout(x)                                                              \\
  for (int i = 0; i < x.size(); i++) {                                         \\
    cout << x[i] << (i == x.size() - 1 ? "\\n" : " ");                          \\
  }
#ifdef LOCAL
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl
#define debugbit(x, n)                                                         \\
  cerr << #x << " = " << bitset<n>(x) << " (L" << __LINE__ << ")"              \\
       << " " << __FILE__ << endl
#define itrdebug(x)                                                            \\
  cerr << #x << " ";                                                           \\
  for (auto &el : (x)) {                                                       \\
    cerr << (el) << " ";                                                       \\
  }                                                                            \\
  cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define debugbit(x, n)
#define itrdebug(x)
#define dassert(...)
#endif
// #define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

// Each segment of the segment tree would be a set
// to maintain distinct elements
set<int> *segment;

// Build the segment tree
// i denotes current node, s denotes start and
// e denotes the end of range for current node
void build(int i, int s, int e, int arr[]) {

  // If start is equal to end then
  // insert the array element
  if (s == e) {
    segment[i].insert(arr[s]);
    return;
  }

  // Else divide the range into two halves
  // (start to mid) and (mid+1 to end)
  // first half will be the left node
  // and the second half will be the right node
  build(2 * i, s, (s + e) / 2, arr);
  build(1 + 2 * i, 1 + (s + e) / 2, e, arr);

  // Insert the sets of right and left
  // node of the segment tree
  segment[i].insert(segment[2 * i].begin(), segment[2 * i].end());

  segment[i].insert(segment[2 * i + 1].begin(), segment[2 * i + 1].end());
}

// Query in an range a to b
set<int> query(int node, int l, int r, int a, int b) {
  set<int> left, right, result;

  // If the range is out of the bounds
  // of this segment
  if (b < l || a > r)
    return result;

  // If the range lies in this segment
  if (a <= l && r <= b)
    return segment[node];

  // Else query for the right and left
  // leaf node of this subtree
  // and insert them into the set
  left = query(2 * node, l, (l + r) / 2, a, b);
  result.insert(left.begin(), left.end());

  right = query(1 + 2 * node, 1 + (l + r) / 2, r, a, b);
  result.insert(right.begin(), right.end());

  // Return the result
  return result;
}

// Initialize the segment tree
void init(int n) {
  // Get the height of the segment tree
  int h = (int)ceil(log2(n));
  h = (2 * (pow(2, h))) - 1;

  // Initialize the segment tree
  segment = new set<int>[h];
}

// Function to get the result for the
// subarray from arr[l] to arr[r]
void getDistinct(int l, int r, int n) {
  // Query for the range set
  set<int> ans = query(1, 0, n - 1, l, r);

  cout << ans.size() << endl;
}

void solve(long long N, long long Q, int c[], std::vector<long long> l,
           std::vector<long long> r) {
  int n = N;

  init(n);

  // Bulid the segment tree
  build(1, 0, n - 1, c);

  rep(i, 0, Q) { getDistinct(l[i] - 1, r[i] - 1, n); }
}

signed main() {
  // ios_base::sync_with_stdio(false);
  // cin.tie(NULL);
  long long N;
  scanf("%lld", &N);
  long long Q;
  scanf("%lld", &Q);
  int c[N];
  for (int i = 0; i < N; i++) {
    cin >> c[i];
  }
  std::vector<long long> l(Q);
  std::vector<long long> r(Q);
  for (int i = 0; i < Q; i++) {
    scanf("%lld", &l[i]);
    scanf("%lld", &r[i]);
  }
  solve(N, Q, c, std::move(l), std::move(r));
  return 0;
}