Problem p02599 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};

template <typename Monoid> struct Segment_Tree {
  Monoid ope(Monoid a, Monoid b) { return a + b; }
  const Monoid unit;
  int n;
  vector<Monoid> seg;

  Segment_Tree(int N, const Monoid &x) : unit(x) {
    n = 1;
    while (n < N)
      n <<= 1;
    seg.assign(2 * n, unit);
  }

  void change(int i, const Monoid &x) {
    i += n;
    seg[i] = x;
    while (i > 0) {
      i /= 2;
      seg[i] = ope(seg[2 * i], seg[2 * i + 1]);
    }
  }

  Monoid query(int a, int b, int i = 1, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (a >= r || b <= l)
      return unit;
    if (a <= l && r <= b)
      return seg[i];
    Monoid vl = query(a, b, 2 * i, l, (l + r) / 2);
    Monoid vr = query(a, b, 2 * i + 1, (l + r) / 2, r);
    return ope(vl, vr);
  }

  Monoid operator[](int i) const { return seg[n + i]; }

  void clear() { fill(seg.begin(), seg.end(), unit); }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  int c[N];
  rep(i, N) cin >> c[i];
  int pos[N];
  fill(pos, pos + N, -1);
  Segment_Tree<int> seg(N, 0);
  vector<pii> q[N + 1];
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    l--;
    q[r].pb(pii(l, i));
  }
  int ans[Q];
  rep(i, N + 1) {
    for (auto &e : q[i]) {
      ans[e.second] = seg.query(e.first, i);
    }
    if (i == N)
      break;
    if (pos[c[i]] != -1)
      seg.change(pos[c[i]], 0);
    pos[c[i]] = i;
    seg.change(i, 1);
  }
  rep(i, Q) cout << ans[i] << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep2(i, x, n) for (int i = x; i <= n; i++)
#define rep3(i, x, n) for (int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const int inf = (1 << 30) - 1;
const ll INF = (1LL << 60) - 1;
const ld EPS = 1e-10;
template <typename T> bool chmax(T &x, const T &y) {
  return (x < y) ? (x = y, true) : false;
};
template <typename T> bool chmin(T &x, const T &y) {
  return (x > y) ? (x = y, true) : false;
};

template <typename Monoid> struct Segment_Tree {
  Monoid ope(Monoid a, Monoid b) { return a + b; }
  const Monoid unit;
  int n;
  vector<Monoid> seg;

  Segment_Tree(int N, const Monoid &x) : unit(x) {
    n = 1;
    while (n < N)
      n <<= 1;
    seg.assign(2 * n, unit);
  }

  void change(int i, const Monoid &x) {
    i += n;
    seg[i] = x;
    while (i > 0) {
      i /= 2;
      seg[i] = ope(seg[2 * i], seg[2 * i + 1]);
    }
  }

  Monoid query(int a, int b, int i = 1, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    if (a >= r || b <= l)
      return unit;
    if (a <= l && r <= b)
      return seg[i];
    Monoid vl = query(a, b, 2 * i, l, (l + r) / 2);
    Monoid vr = query(a, b, 2 * i + 1, (l + r) / 2, r);
    return ope(vl, vr);
  }

  Monoid operator[](int i) const { return seg[n + i]; }

  void clear() { fill(seg.begin(), seg.end(), unit); }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  int c[N];
  rep(i, N) {
    cin >> c[i];
    c[i]--;
  }
  int pos[N];
  fill(pos, pos + N, -1);
  Segment_Tree<int> seg(N, 0);
  vector<pii> q[N + 1];
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    l--;
    q[r].pb(pii(l, i));
  }
  int ans[Q];
  rep(i, N + 1) {
    for (auto &e : q[i]) {
      ans[e.second] = seg.query(e.first, i);
    }
    if (i == N)
      break;
    if (pos[c[i]] != -1)
      seg.change(pos[c[i]], 0);
    pos[c[i]] = i;
    seg.change(i, 1);
  }
  rep(i, Q) cout << ans[i] << endl;
}