Problem p02599 (C++)

Buggy submission (status: Runtime Error):
typedef int ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
template <class Monoid> struct SegTree {
  using Func = function<Monoid(Monoid, Monoid)>;
  const Func F;
  const Monoid UNITY;
  int SIZE_R;
  vector<Monoid> dat;

  SegTree(int n, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
    init(n);
  }
  void init(int n) {
    SIZE_R = 1;
    while (SIZE_R < n)
      SIZE_R *= 2;
    dat.assign(SIZE_R * 2, UNITY);
  }

  /* set, a is 0-indexed */
  void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
  void build() {
    for (int k = SIZE_R - 1; k > 0; --k)
      dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
  }

  /* update a, a is 0-indexed */
  void update(int a, const Monoid &v) {
    int k = a + SIZE_R;
    dat[k] = v;
    while (k >>= 1)
      dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
  }

  /* get [a, b), a and b are 0-indexed */
  Monoid get(int a, int b) {
    Monoid vleft = UNITY, vright = UNITY;
    for (int left = a + SIZE_R, right = b + SIZE_R; left < right;
         left >>= 1, right >>= 1) {
      if (left & 1)
        vleft = F(vleft, dat[left++]);
      if (right & 1)
        vright = F(dat[--right], vright);
    }
    return F(vleft, vright);
  }
  inline Monoid operator[](int a) { return dat[a + SIZE_R]; }

  /* debug */
  void print() {
    for (int i = 0; i < SIZE_R; ++i) {
      cout << (*this)[i];
      if (i != SIZE_R - 1)
        cout << ",";
    }
    cout << endl;
  }
};
int main() {
  ll n, q;
  std::cin >> n >> q;
  vector<ll> c(n + 10);
  for (int i = 0; i < n; i++) {
    std::cin >> c[i];
    c[i]--;
  }
  vector<pair<ll, ll>> ans(n + 10);
  map<pair<ll, ll>, ll> vmp;
  vector<pair<ll, ll>> qs(q + 10);
  for (int i = 0; i < q; i++) {
    ll l, r;
    std::cin >> l >> r;
    l--;
    r--;
    ans[i] = {l, r};
    qs[i] = {l, r};
  }

  SegTree<ll> seg(
      n + 10, [](ll a, ll b) { return a + b; }, 0);

  sort(qs.begin(), qs.end(),
       [](const auto le, const auto ri) { return le.second > ri.second; });
  vector<ll> last(n + 10, -1);
  vector<ll> doko(n + 10, -1);
  for (int i = 0; i < n; i++) {
    last[i] = doko[c[i]];
    doko[c[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    if (doko[c[i]] == i) {
      seg.update(i, 1);
    }
  }
  ll nowr = n - 1;
  for (int i = 0; i < q; i++) {
    for (int j = nowr; j > qs[i].second; j--) {
      seg.update(j, 0);
      if (last[j] != -1) {
        seg.update(last[j], 1);
      }
    }
    nowr = qs[i].second;
    vmp[qs[i]] = seg.get(qs[i].first, qs[i].second + 1);
  }

  for (int i = 0; i < q; i++) {
    std::cout << vmp[ans[i]] << std::endl;
  }
}


Fixed submission:
typedef int ll;
typedef long double ld;
#include <bits/stdc++.h>
using namespace std;
template <class Monoid> struct SegTree {
  using Func = function<Monoid(Monoid, Monoid)>;
  const Func F;
  const Monoid UNITY;
  int SIZE_R;
  vector<Monoid> dat;

  SegTree(int n, const Func f, const Monoid &unity) : F(f), UNITY(unity) {
    init(n);
  }
  void init(int n) {
    SIZE_R = 1;
    while (SIZE_R < n)
      SIZE_R *= 2;
    dat.assign(SIZE_R * 2, UNITY);
  }

  /* set, a is 0-indexed */
  void set(int a, const Monoid &v) { dat[a + SIZE_R] = v; }
  void build() {
    for (int k = SIZE_R - 1; k > 0; --k)
      dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
  }

  /* update a, a is 0-indexed */
  void update(int a, const Monoid &v) {
    int k = a + SIZE_R;
    dat[k] = v;
    while (k >>= 1)
      dat[k] = F(dat[k * 2], dat[k * 2 + 1]);
  }

  /* get [a, b), a and b are 0-indexed */
  Monoid get(int a, int b) {
    Monoid vleft = UNITY, vright = UNITY;
    for (int left = a + SIZE_R, right = b + SIZE_R; left < right;
         left >>= 1, right >>= 1) {
      if (left & 1)
        vleft = F(vleft, dat[left++]);
      if (right & 1)
        vright = F(dat[--right], vright);
    }
    return F(vleft, vright);
  }
  inline Monoid operator[](int a) { return dat[a + SIZE_R]; }

  /* debug */
  void print() {
    for (int i = 0; i < SIZE_R; ++i) {
      cout << (*this)[i];
      if (i != SIZE_R - 1)
        cout << ",";
    }
    cout << endl;
  }
};
int main() {
  ll n, q;
  std::cin >> n >> q;
  vector<ll> c(n + 10);
  for (int i = 0; i < n; i++) {
    std::cin >> c[i];
    c[i]--;
  }
  vector<pair<ll, ll>> ans(q + 10);
  map<pair<ll, ll>, ll> vmp;
  vector<pair<ll, ll>> qs(q + 10);
  for (int i = 0; i < q; i++) {
    ll l, r;
    std::cin >> l >> r;
    l--;
    r--;
    ans[i] = {l, r};
    qs[i] = {l, r};
  }

  SegTree<ll> seg(
      n + 10, [](ll a, ll b) { return a + b; }, 0);

  sort(qs.begin(), qs.end(),
       [](const auto le, const auto ri) { return le.second > ri.second; });
  vector<ll> last(n + 10, -1);
  vector<ll> doko(n + 10, -1);
  for (int i = 0; i < n; i++) {
    last[i] = doko[c[i]];
    doko[c[i]] = i;
  }
  for (int i = 0; i < n; i++) {
    if (doko[c[i]] == i) {
      seg.update(i, 1);
    }
  }
  ll nowr = n - 1;
  for (int i = 0; i < q; i++) {
    for (int j = nowr; j > qs[i].second; j--) {
      seg.update(j, 0);
      if (last[j] != -1) {
        seg.update(last[j], 1);
      }
    }
    nowr = qs[i].second;
    vmp[qs[i]] = seg.get(qs[i].first, qs[i].second + 1);
  }

  for (int i = 0; i < q; i++) {
    std::cout << vmp[ans[i]] << std::endl;
  }
}