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

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
void _OUT(){};
template <class H, class... T> void _OUT(H &h, T &...t) {
  cout << ' ' << h;
  _OUT(t...);
};
template <class H, class... T> void _P(H &h, T &...t) {
  cout << h;
  _OUT(t...);
};
#define _PN(...)                                                               \\
  _P(__VA_ARGS__);                                                             \\
  cout << endl;
#define EXEC(...)                                                              \\
  void exec() { (__VA_ARGS__ - 1) ? multi() : solve(0); }

void exec();
void solve(int t);
void multi() {
  int TT;
  cin >> TT;
  REP1(t, TT) solve(t);
}
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  exec();
  return 0;
}

/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 523450
#endif

int N, M;
vector<int> C(SIZE);

template <typename T> class ST {
  using F = function<T(T, T)>;
  int size;
  T init;
  F f;
  vector<T> seg;
  int depth = 0;

public:
  ST(int size, T init, F f) : size(size), init(init), f(f) {
    while (1 << depth < size)
      depth++;
    seg.assign(2 << depth, init);
  }

  void update(int k, T t) {
    k += 1 << depth;
    seg[k] = t;
    while (k >>= 1)
      seg[k] = f(seg[2 * k], seg[2 * k + 1]);
  }

  T query(int a, int b) {
    a += 1 << depth;
    b += 1 << depth;
    T l = init;
    T r = init;
    while (a < b) {
      if (a & 1)
        l = f(l, seg[a++]);
      if (b & 1)
        r = f(r, seg[--b]);
      a >>= 1;
      b >>= 1;
    }
    return f(l, r);
  }

  T operator[](int k) { return seg[k + (1 << depth)]; }
};

void solve(int _t) {
  int Q;
  cin >> N >> Q;
  REP(i, N) {
    cin >> C[i];
    C[i]--;
  }
  int l, r;
  vector<pair<int, int>> V, V2;
  REP(i, Q) {
    cin >> l >> r;
    V.push_back({--l, --r});
    V2.push_back({l, r});
  }
  sort(ALL(V));
  vector<int> last(N, -1), prev(N);
  RREP(i, N) {
    prev[i] = last[C[i]];
    last[C[i]] = i;
  }
  auto st = ST<int>(N, 0, [](int a, int b) { return a + b; });
  map<pair<int, int>, int> ans;
  RFOR(i, V[0].first, N) {
    st.update(i, 1);
    if (prev[i] >= 0) {
      st.update(prev[i], 0);
    }
  }
  int s = V[0].first;
  for (auto p : V) {
    while (s < p.first) {
      st.update(prev[s], 1);
      s++;
    }
    ans[p] = st.query(p.first, p.second + 1);
  }
  for (auto p : V2) {
    cout << ans[p] << endl;
  }
}
EXEC(1)


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b - 1); i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n - 1); i >= 0; i--)
#define REP1(i, n) for (int i = 1; i <= (n); i++)
#define RREP1(i, n) for (int i = (n); i >= 1; i--)
#define ALL(a) (a).begin(), (a).end()
#define UNIQUE_SORT(l)                                                         \\
  sort(ALL(l));                                                                \\
  l.erase(unique(ALL(l)), l.end());
#define CONTAIN(a, b) find(ALL(a), (b)) != (a).end()
#define out(...) printf(__VA_ARGS__)
#if DEBUG
#define debug(...) printf(__VA_ARGS__)
#else
#define debug(...) /* ... */
#endif
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}
void _OUT(){};
template <class H, class... T> void _OUT(H &h, T &...t) {
  cout << ' ' << h;
  _OUT(t...);
};
template <class H, class... T> void _P(H &h, T &...t) {
  cout << h;
  _OUT(t...);
};
#define _PN(...)                                                               \\
  _P(__VA_ARGS__);                                                             \\
  cout << endl;
#define EXEC(...)                                                              \\
  void exec() { (__VA_ARGS__ - 1) ? multi() : solve(0); }

void exec();
void solve(int t);
void multi() {
  int TT;
  cin >> TT;
  REP1(t, TT) solve(t);
}
signed main() {
#if DEBUG
  std::ifstream in("input.txt");
  std::cin.rdbuf(in.rdbuf());
#endif
  cin.tie(0);
  ios::sync_with_stdio(false);
  exec();
  return 0;
}

/*================================*/
#if DEBUG
#define SIZE 100
#else
#define SIZE 223450
#endif

int N, M;
vector<int> C(SIZE);

template <typename T> class ST {
  using F = function<T(T, T)>;
  int size;
  T init;
  F f;
  vector<T> seg;
  int depth = 0;

public:
  ST(int size, T init, F f) : size(size), init(init), f(f) {
    while (1 << depth < size)
      depth++;
    seg.assign(2 << depth, init);
  }

  void update(int k, T t) {
    k += 1 << depth;
    seg[k] = t;
    while (k >>= 1)
      seg[k] = f(seg[2 * k], seg[2 * k + 1]);
  }

  T query(int a, int b) {
    a += 1 << depth;
    b += 1 << depth;
    T l = init;
    T r = init;
    while (a < b) {
      if (a & 1)
        l = f(l, seg[a++]);
      if (b & 1)
        r = f(r, seg[--b]);
      a >>= 1;
      b >>= 1;
    }
    return f(l, r);
  }

  T operator[](int k) { return seg[k + (1 << depth)]; }
};

void solve(int _t) {
  int Q;
  cin >> N >> Q;
  REP(i, N) cin >> C[i];
  int l, r;
  vector<pair<int, int>> V, V2;
  REP(i, Q) {
    cin >> l >> r;
    V.push_back({--l, --r});
    V2.push_back({l, r});
  }
  sort(ALL(V));
  vector<int> last(N, -1), prev(N);
  RREP(i, N) {
    prev[i] = last[C[i]];
    last[C[i]] = i;
  }
  auto st = ST<int>(N, 0, [](int a, int b) { return a + b; });
  map<pair<int, int>, int> ans;
  RFOR(i, V[0].first, N) {
    st.update(i, 1);
    if (prev[i] >= 0) {
      st.update(prev[i], 0);
    }
  }
  int s = V[0].first;
  for (auto p : V) {
    while (s < p.first) {
      st.update(prev[s], 1);
      s++;
    }
    ans[p] = st.query(p.first, p.second + 1);
  }
  for (auto p : V2) {
    cout << ans[p] << endl;
  }
}
EXEC(1)