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

#line 1 "/workspaces/compro/lib/segment_tree.hpp"
#include <functional>
#include <vector>
#ifndef SEGMENT_TREE
#define SEGMENT_TREE

template <class T> class SegmentTree {
public:
  SegmentTree(const int n, const T id, std::function<T(T, T)> op,
              std::function<T(T, T)> updateOp)
      : id(id), op(op), updateOp(updateOp) {
    leafNum = 1;
    while (leafNum < n)
      leafNum *= 2;
    data = std::vector<T>(2 * leafNum - 1, id);
  }

  void update(const int index, const T value) {
    int i = leaf(index);
    data[i] = updateOp(data[i], value);
    while (i > 0) {
      i = parent(i);
      data[i] = op(data[rightChildIndex(i)], data[leftChildIndex(i)]);
    }
  }

  T find(const int l, const int r) { return query(l, r, 0, 0, leafNum); }

private:
  int leftChildIndex(const int parent) { return parent * 2 + 1; }

  int rightChildIndex(const int parent) { return parent * 2 + 2; }

  int parent(const int child) { return (child - 1) / 2; }

  int leaf(const int index) { return index + leafNum - 1; }

  T query(const int l, const int r, const int node, const int al,
          const int ar) {
    if (r <= al || ar <= l)
      return id;
    if (l <= al && ar <= r)
      return data[node];
    int mid = (al + ar) / 2;
    T leftValue = query(l, r, leftChildIndex(node), al, mid);
    T rightValue = query(l, r, rightChildIndex(node), mid, ar);
    return op(leftValue, rightValue);
  }

private:
  std::vector<T> data;
  int leafNum;
  T id;
  std::function<T(T, T)> op;
  std::function<T(T, T)> updateOp;
};
#endif
#line 1 "/workspaces/compro/lib/template.hpp"

#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#line 3 "/workspaces/compro/lib/io/vector.hpp"

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T>
std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes, T no) {
  cout << (val ? yes : no) << "\\n";
}

void say(bool val, string yes = "Yes", string no = "No") {
  say<string>(val, yes, no);
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }

// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
  int g = gcd(n, m);
  return n * m / g;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

#line 3 "abc174_f.cpp"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;
  vi c(n);
  cin >> c;
  REP(i, n) { c[i]--; }

  vector<pair<pair<int, int>, int>> query(q);
  REP(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    query[i] = {{l, r}, i};
  }
  sort(ALL(query), [](const auto &lhs, const auto &rhs) -> bool {
    if (lhs.first.second == rhs.first.second) {
      if (lhs.first.first == rhs.first.first) {
        return lhs.second < rhs.second;
      }
      return lhs.first.first < rhs.first.first;
    }
    return lhs.first.second < rhs.first.second;
  });
  vi ans(q);
  vi la(n, -1);
  SegmentTree<int> seg(
      n, 0, [](int a, int b) -> int { return a + b; },
      [](int a, int b) -> int { return a + b; });

  int r = 0;
  REP(i, q) {
    int nr = query[i].first.second;
    while (r < nr) {
      if (la[c[r]] != -1)
        seg.update(la[c[r]], -1);
      la[c[r]] = r;
      seg.update(r, 1);
      r++;
    }

    ans[query[i].second] = seg.find(query[i].first.first, nr);
  }

  REP(i, q) { cout << ans[i] << endl; }
}


Error: 0

Fixed submission:

#line 1 "/workspaces/compro/lib/segment_tree.hpp"
#include <functional>
#include <vector>
#ifndef SEGMENT_TREE
#define SEGMENT_TREE

template <class T> class SegmentTree {
public:
  SegmentTree(const int n, const T id, std::function<T(T, T)> op,
              std::function<T(T, T)> updateOp)
      : id(id), op(op), updateOp(updateOp) {
    leafNum = 1;
    while (leafNum < n)
      leafNum *= 2;
    data = std::vector<T>(2 * leafNum - 1, id);
  }

  void update(const int index, const T value) {
    int i = leaf(index);
    data[i] = updateOp(data[i], value);
    while (i > 0) {
      i = parent(i);
      data[i] = op(data[rightChildIndex(i)], data[leftChildIndex(i)]);
    }
  }

  T find(const int l, const int r) { return query(l, r, 0, 0, leafNum); }

private:
  int leftChildIndex(const int parent) { return parent * 2 + 1; }

  int rightChildIndex(const int parent) { return parent * 2 + 2; }

  int parent(const int child) { return (child - 1) / 2; }

  int leaf(const int index) { return index + leafNum - 1; }

  T query(const int l, const int r, const int node, const int al,
          const int ar) {
    if (r <= al || ar <= l)
      return id;
    if (l <= al && ar <= r)
      return data[node];
    int mid = (al + ar) / 2;
    T leftValue = query(l, r, leftChildIndex(node), al, mid);
    T rightValue = query(l, r, rightChildIndex(node), mid, ar);
    return op(leftValue, rightValue);
  }

private:
  std::vector<T> data;
  int leafNum;
  T id;
  std::function<T(T, T)> op;
  std::function<T(T, T)> updateOp;
};
#endif
#line 1 "/workspaces/compro/lib/template.hpp"

#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#line 3 "/workspaces/compro/lib/io/vector.hpp"

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T>
std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes, T no) {
  cout << (val ? yes : no) << "\\n";
}

void say(bool val, string yes = "Yes", string no = "No") {
  say<string>(val, yes, no);
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }

// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
  int g = gcd(n, m);
  return n * m / g;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

#line 3 "abc174_f.cpp"

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;
  vi c(n);
  cin >> c;
  REP(i, n) { c[i]--; }

  vector<pair<pair<int, int>, int>> query(q);
  REP(i, q) {
    int l, r;
    cin >> l >> r;
    l--;
    query[i] = {{l, r}, i};
  }
  sort(ALL(query), [](const auto &lhs, const auto &rhs) -> bool {
    if (lhs.first.second == rhs.first.second) {
      return lhs.first.first < rhs.first.first;
    }
    return lhs.first.second < rhs.first.second;
  });
  vi ans(n);

  vi la(n, -1);
  SegmentTree<int> seg(
      n, 0, [](int a, int b) -> int { return a + b; },
      [](int a, int b) -> int { return a + b; });

  int r = 0;
  REP(i, q) {
    int nr = query[i].first.second;
    while (r < nr) {
      if (la[c[r]] != -1)
        seg.update(la[c[r]], -1);
      la[c[r]] = r;
      seg.update(r, 1);
      r++;
    }

    ans[query[i].second] = seg.find(query[i].first.first, nr);
  }

  REP(i, q) { cout << ans[i] << endl; }
}