alexjercan/bugnet huggingface dataset:
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct Query {
  int e, s, i;

  bool operator<(const Query &rhs) const { return e < rhs.e; }
};

const int MAX = 500100;
int n, m;
int nums[MAX];
int answer[MAX];
int last[500100];
vector<int> tree;
vector<Query> queries;

void add(int p, int val) {
  while (p < tree.size()) {
    tree[p] += val;
    p += (p & -p);
  }
}

int sum(int p) {
  int ret = 0;
  while (p > 0) {
    ret += tree[p];
    p -= (p & -p);
  }

  return ret;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> nums[i];
  }

  tree.resize(n + 1);

  queries.resize(m);
  for (int j = 0; j < m; ++j) {
    queries[j].i = j;
    cin >> queries[j].s >> queries[j].e;
  }

  sort(queries.begin(), queries.end());

  int pos = 1;
  for (int k = 0; k < m; ++k) {
    while (pos <= queries[k].e) {
      if (last[nums[pos]]) {
        add(last[nums[pos]], -1);
      }

      add((last[nums[pos]] = pos), 1);
      pos++;
    }

    answer[queries[k].i] = sum(queries[k].e) - sum(queries[k].s - 1);
  }

  for (int l = 0; l < m; ++l) {
    cout << answer[l] << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;
const int MAX_N = (int)5e5 + 10, MAX_NUM = (int)5e5 + 10;
int N, q, sqrtN, cnt = 0,
                 visited[MAX_NUM] =
                     {
                         0,
},
                 ans[MAX_N];
int queryLeft, queryRight;
vector<int> arr;

struct MOS_QUERY {
  int left, right, idx;
  MOS_QUERY() { ; }
  MOS_QUERY(int left, int right, int idx) {
    this->left = left, this->right = right, this->idx = idx;
  }
};
vector<MOS_QUERY> query;

bool compare(MOS_QUERY &a, MOS_QUERY &b) {
  if (a.left / sqrtN != b.left / sqrtN)
    return a.left / sqrtN < b.left / sqrtN;
  else
    return a.right < b.right;
}

void getFirst() {
  queryLeft = query[0].left, queryRight = query[0].right;
  for (int i = queryLeft; i < queryRight; i++)
    if (visited[arr[i]]++ == 0)
      cnt += 1;
  ans[query[0].idx] = cnt;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> N >> q;
  sqrtN = sqrt(N);
  arr.resize(N);
  for (int i = 0; i < N; i++)
    cin >> arr[i];
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    query.push_back(MOS_QUERY(a - 1, b, i));
  }
  sort(query.begin(), query.end(), compare);
  getFirst();
  for (int i = 1; i < q; i++) {
    MOS_QUERY now = query[i];
    while (now.left < queryLeft)
      if (visited[arr[--queryLeft]]++ == 0)
        cnt += 1;
    while (now.left > queryLeft)
      if (--visited[arr[queryLeft++]] == 0)
        cnt -= 1;
    while (queryRight < now.right)
      if (visited[arr[queryRight++]]++ == 0)
        cnt += 1;
    while (queryRight > now.right)
      if (--visited[arr[--queryRight]] == 0)
        cnt -= 1;
    ans[now.idx] = cnt;
  }

  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <set>

int cnt[500005];
int a[500005];
int block;

struct A {
  int l, r;
  int idx;
  int ans;
} q[500005];

bool cmp(A a, A b) {
  if (a.l / block != b.l / block)
    return a.l / block < b.l / block;
  return a.r < b.r;
}

bool cmp2(A a, A b) { return a.idx < b.idx; }

int main() {
  int N, Q;
  int i;
  scanf("%d%d", &N, &Q);
  for (i = 1; i <= N; ++i)
    scanf("%d", &a[i]);
  for (i = 0; i < Q; ++i) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].idx = i;
  }
  block = sqrt(N);
  std::sort(q, q + Q, cmp);
  // std::set<int> s;
  int curL = q[0].l;
  int curR = q[0].l;
  // s.insert(a[curL]);
  int num = 1;
  // s.insert(a[curL]);
  cnt[a[curL]] = 1;
  for (i = 0; i < Q; ++i) {
    while (curR < q[i].r) {
      ++curR;
      if (cnt[a[curR]] == 0) {
        ++num;
        cnt[a[curR]] = 1;
      } else {
        ++cnt[a[curR]];
      }
    }
    while (curR > q[i].r) {
      if (cnt[a[curR]] == 1) {
        cnt[a[curR]] = 0;
        --num;
      } else {
        --cnt[a[curR]];
      }
      --curR;
    }
    while (curL < q[i].l) {
      if (cnt[a[curL]] == 1) {
        cnt[a[curL]] = 0;
        --num;
      } else {
        --cnt[a[curL]];
      }
      ++curL;
    }
    while (curL > q[i].l) {
      --curL;
      if (cnt[a[curL]] == 0) {
        ++num;
      }
      ++cnt[a[curL]];
    }
    q[i].ans = num;
  }
  std::sort(q, q + Q, cmp2);
  for (i = 0; i < Q; ++i)
    printf("%d\\n", q[i].ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <set>

using namespace std;

int f[500005];
int a[500005];

struct A {
  int l, r;
  int idx;
  int ans;
} q[500005];

bool cmp(A a, A b) { return a.r < b.r; }

bool cmp2(A a, A b) { return a.idx < b.idx; }

int main() {
  int N, Q;
  int i;
  scanf("%d%d", &N, &Q);
  for (i = 1; i <= N; ++i)
    scanf("%d", &a[i]);
  for (i = 0; i < Q; ++i) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].idx = i;
  }
  sort(q, q + Q, cmp);
  set<int> s;
  int curL = q[0].l;
  int curR = q[0].l;
  // s.insert(a[curL]);
  // f[a[curL]] = curL;
  for (i = 0; i < Q; ++i) {
    while (curR <= q[i].r) {
      s.insert(a[curR]);
      f[a[curR]] = curR;
      ++curR;
    }
    while (curL < q[i].l) {
      if (f[a[curL]] != curL) {
        ++curL;
      } else {
        s.erase(a[curL]);
        ++curL;
      }
    }
    while (curL > q[i].l) {
      --curL;
      s.insert(a[curL]);
    }
    q[i].ans = s.size();
  }
  sort(q, q + Q, cmp2);
  for (i = 0; i < Q; ++i)
    printf("%d\\n", q[i].ans);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <time.h>
using namespace std;
#define rep_1(i, m, n) for (int i = m; i <= n; i++)
#define mem(st) memset(st, 0, sizeof st)
inline int read() {
  int num = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch)) {
    num = (num << 3) + (num << 1) + (ch ^ 48);
    ch = getchar();
  }
  return w ? -num : num;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef unsigned long long ull;
typedef pair<double, double> pdd;
const int inf = 0x3f3f3f3f;
const int N = 2e6 + 10;
int a[N];
int n, m;
int pre[N];
struct node {
  int l, r;
  int id;
} e[N];
int tr[N];
int lowbit(int x) { return x & -x; }
void add(int x, int k) {
  for (int i = x; i <= n; i += lowbit(i))
    tr[i] += k;
}
int query(int x) {
  int res = 0;
  for (int i = x; i; i -= lowbit(i))
    res += tr[i];
  return res;
}
bool cmp(node a, node b) {
  if (a.r != b.r)
    return a.r < b.r;
  else
    return a.l < b.l;
}
int ans[N];
void solve() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++) {
    cin >> e[i].l >> e[i].r;
    e[i].id = i;
  }
  sort(e + 1, e + 1 + m, cmp);
  int next = 1;
  for (int i = 1; i <= m; i++) {
    for (int j = next; j <= e[i].r; j++) {
      if (pre[a[j]])
        add(pre[a[j]], -1);
      add(j, 1);
      pre[a[j]] = j;
    }
    next = e[i].r + 1;
    ans[e[i].id] = query(e[i].r) - query(e[i].l - 1);
  }
  for (int i = 1; i <= m; i++)
    cout << ans[i] << endl;
}
signed main() {
  int t = 1;
  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 5e6 + 10;
int a[N], belong[N];
int n, q, k;
inline int read() {
  int k = 0;
  char c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    k = (k << 3) + (k << 1) + c - '0';
    c = getchar();
  }
  return k;
}
struct node {
  int l, r;
  int id;
} e[N];
int cnt[N];
int Ans;
int ans[N];
inline bool cmp(node a, node b) {
  if (belong[a.l] == belong[b.l])
    return belong[a.r] < belong[b.r];
  return belong[a.l] < belong[b.l];
}
inline void add(int x) {
  if (cnt[a[x]] == 0)
    Ans++;
  cnt[a[x]]++;
}
inline void del(int x) {
  if (cnt[a[x]] == 1)
    Ans--;
  cnt[a[x]]--;
}
int main() {
  n = read(), q = read();
  int block = pow(n, 0.5);
  for (register int i = 1; i <= n; ++i) {
    a[i] = read();
    belong[i] = (i - 1) / block + 1;
  }
  for (register int i = 1; i <= q; i++) {
    e[i].l = read();
    e[i].r = read();
    e[i].id = i;
  }
  sort(e + 1, e + 1 + q, cmp);
  int l = 1, r = 0;
  for (register int i = 1; i <= q; ++i) {
    while (l < e[i].l)
      del(l++);
    while (l > e[i].l)
      add(--l);
    while (r < e[i].r)
      add(++r);
    while (r > e[i].r)
      del(r--);
    ans[e[i].id] = Ans;
  }
  for (register int i = 1; i <= q; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef vector<vector<int>> vv;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;

// input
int N, Q;
vector<int> c, l, r;

void input() {
  cin >> N >> Q;
  c = vector<int>(N);
  l = r = vector<int>(Q);
  rep(i, N) cin >> c[i];
  rep(i, Q) cin >> l[i] >> r[i];
}

struct BIT {
  int n;
  vector<ll> dat;

  BIT(int n_) {
    n = n_;
    dat = vector<ll>(n + 1, 0);
  }

  void add(int i, int x) {
    i++;
    while (i <= n) {
      dat[i] += x;
      i += i & -i;
    }
  }

  ll sum(int i) {
    if (i == 0)
      return 0;
    return dat[i] + sum(i - (i & -i));
  }
};

vector<P> create_arrow_list() {
  vector<P> ret;
  vector<int> w(N, -1);
  rep(i, N) {
    int v = c[i] - 1;
    if (w[v] != -1)
      ret.push_back(make_pair(w[v], i));
    w[v] = i;
  }
  sort(ret.rbegin(), ret.rend());
  return ret;
}

vector<T> create_query_list() {
  vector<T> ret;
  rep(i, Q) ret.push_back(make_tuple(l[i] - 1, r[i] - 1, i));
  sort(ret.rbegin(), ret.rend());
  return ret;
}

int main() {
  input();
  vector<P> arrow_list = create_arrow_list();
  int an = arrow_list.size();
  vector<T> query_list = create_query_list();
  vector<int> ans(Q);
  BIT bit = BIT(N);
  int i = 0;
  int j = 0;
  int x, y, v;
  while (i != an || j != Q) {
    if (i == an) {
      tie(x, y, v) = query_list[j];
      ans[v] = y - x + 1 - bit.sum(y + 1);
      j++;
    } else if (j == Q) {
      bit.add(arrow_list[i].second, 1);
      i++;
    } else if (get<0>(query_list[j]) <= arrow_list[i].first) {
      bit.add(arrow_list[i].second, 1);
      i++;
    } else {
      tie(x, y, v) = query_list[j];
      ans[v] = y - x + 1 - bit.sum(y + 1);
      j++;
    }
  }
  rep(i, Q) cout << ans[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;
typedef vector<vector<int>> vv;
typedef pair<int, int> P;

// input
int N, Q;
vector<int> c, l, r;

vector<int> w;
vector<set<int>> s;

void input() {
  cin >> N >> Q;
  c = vector<int>(N);
  l = r = vector<int>(Q);
  rep(i, N) cin >> c[i];
  rep(i, Q) cin >> l[i] >> r[i];
}

int main() {
  input();
  w = vector<int>(N + 1, -1);
  set<int> st;
  rep(i, N) {
    if (w[c[i]] != -1)
      st.erase(w[c[i]]);
    st.insert(i);
    w[c[i]] = i;
    s.push_back(st);
  }
  rep(i, Q) {
    set<int> st = s[r[i] - 1];
    int ans = st.size() - distance(st.begin(), st.lower_bound(l[i] - 1));
    printf("%d\\n", ans);
  }
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
mt19937 rnd;

const int maxn = 5e6, maxk = 5e6 + 1;
int root[maxn], L[16 * maxn], R[16 * maxn], sum[16 * maxn];
int rt = 1, sz = 1;
int lpos[maxk];

int copy(int v, int &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}

void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}

void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
  sum[v] += x;
  if (r - l == 1)
    return;
  int m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

int get(int a, int b, int v, int l = 0, int r = maxn) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  int m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << "\\n";
  }

#ifdef LOCAL
  cerr << "\\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
#define F first
#define S second
#define pb push_back
mt19937 rnd;

const int maxn = 1e6, maxk = 1e6 + 1;
int root[maxn], L[16 * maxn], R[16 * maxn], sum[16 * maxn];
int rt = 1, sz = 1;
int lpos[maxk];

int copy(int v, int &u) {
  L[sz] = L[v];
  R[sz] = R[v];
  sum[sz] = sum[v];
  return u = sz++;
}

void make_root() {
  copy(root[rt - 1], root[rt]);
  rt++;
}

void add(int pos, int x, int v = root[rt - 1], int l = 0, int r = maxn) {
  sum[v] += x;
  if (r - l == 1)
    return;
  int m = (l + r) / 2;
  if (pos < m)
    add(pos, x, copy(L[v], L[v]), l, m);
  else
    add(pos, x, copy(R[v], R[v]), m, r);
}

int get(int a, int b, int v, int l = 0, int r = maxn) {
  if (a <= l && r <= b)
    return sum[v];
  if (r <= a || b <= l)
    return 0;
  int m = (l + r) / 2;
  return get(a, b, L[v], l, m) + get(a, b, R[v], m, r);
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif

  int n, q;
  cin >> n >> q;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    make_root();
    add(lpos[t], -1);
    lpos[t] = i;
    add(lpos[t], 1);
  }
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << get(l, r + 1, root[r]) << "\\n";
  }

#ifdef LOCAL
  cerr << "\\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\\n";
#endif
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int MAX = 5 * 1e5 + 10;
typedef pair<long long, long long> P;
bool comp(P &p1, P &p2) { return p1.second < p2.second; }
struct BIT {
  vector<long long> num;
  int N;
  BIT(int n) {
    N = n;
    num.resize(N + 1, 0);
  }
  long long sum(long long t) {
    long long res = 0;
    while (t > 0) {
      res += num[t];
      t -= t & -t;
    }
    return res;
  }
  void add(int ind, long long t) {
    while (ind <= N) {
      num[ind] += t;
      ind += ind & -ind;
    }
  }
};
int main() {
  int N, Q;
  cin >> N >> Q;
  BIT BIT(N);
  vector<long long> c(N);
  vector<long long> ans(Q);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
  }
  vector<long long> l(Q), r(Q);
  vector<int> order(Q);
  for (int i = 0; i < Q; i++) {
    cin >> l[i] >> r[i];
    order[i] = i;
  }
  sort(order.begin(), order.end(), [&](int x, int y) { return r[x] < r[y]; });
  vector<long long> num(500010, -1);
  int ind = 0;
  for (int i = 0; i < N; i++) {
    if (ind == Q)
      break;
    if (num[c[i]] != -1) {
      BIT.add(num[c[i]], -1);
      num[c[i]] = i + 1;
      BIT.add(num[c[i]], 1);
    } else {
      num[c[i]] = i + 1;
      BIT.add(num[c[i]], 1);
    }
    while (i + 1 == r[order[ind]]) {
      long long temp = BIT.sum(i + 1) - BIT.sum(l[order[ind]] - 1);
      ans[order[ind]] = temp;
      ind++;
      if (ind == Q)
        break;
    }
  }
  for (int i = 0; i < Q; i++) {
    cout << ans[i] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAX = 5 * 1e5 + 10;
typedef pair<long long, long long> P;
bool comp(P &p1, P &p2) { return p1.second < p2.second; }
struct BIT {
  vector<long long> num;
  int N;
  BIT(int n) {
    N = n;
    num.resize(N + 1, 0);
  }
  long long sum(long long t) {
    long long res = 0;
    while (t > 0) {
      res += num[t];
      t -= t & -t;
    }
    return res;
  }
  void add(int ind, long long t) {
    while (ind <= N) {
      num[ind] += t;
      ind += ind & -ind;
    }
  }
};
int main() {
  int N, Q;
  cin >> N >> Q;
  BIT BIT(N);
  vector<long long> c(N);
  map<long long, P> m;
  map<P, long long> ans;
  for (int i = 0; i < N; i++) {
    cin >> c[i];
  }
  vector<P> qu(Q);
  for (int i = 0; i < Q; i++) {
    long long l, r;
    cin >> l >> r;
    m[i] = P(l, r);
    qu[i] = P(l, r);
  }
  sort(qu.begin(), qu.end(), comp);
  vector<long long> num(500010, -1);
  int ind = 0;
  for (int i = 0; i < N; i++) {
    if (ind == Q)
      break;
    if (num[c[i]] != -1) {
      BIT.add(num[c[i]], -1);
      num[c[i]] = i + 1;
      BIT.add(num[c[i]], 1);
    } else {
      num[c[i]] = i + 1;
      BIT.add(num[c[i]], 1);
    }
    while (i + 1 == qu[ind].second) {
      long long temp = BIT.sum(i + 1) - BIT.sum(qu[ind].first - 1);
      ans[qu[ind]] = temp;
      ind++;
      if (ind == Q)
        break;
    }
  }
  for (auto v : m) {
    P u = v.second;
    cout << ans[u] << endl;
  }
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int MAX = 5 * 1e5 + 10;
typedef pair<long long, long long> P;
bool comp(P &p1, P &p2) { return p1.second < p2.second; }
struct BIT {
  vector<long long> num;
  int N;
  BIT(int n) {
    N = n;
    num.resize(N + 1, 0);
  }
  long long sum(long long t) {
    long long res = 0;
    while (t > 0) {
      res += num[t];
      t -= t & -t;
    }
    return res;
  }
  void add(int ind, long long t) {
    while (ind <= N) {
      num[ind] += t;
      ind += ind & -ind;
    }
  }
};
int main() {
  int N, Q;
  cin >> N >> Q;
  BIT BIT(N);
  vector<long long> c(N);
  map<long long, P> m;
  map<P, long long> ans;
  for (int i = 0; i < N; i++) {
    cin >> c[i];
  }
  vector<P> qu(Q);
  for (int i = 0; i < Q; i++) {
    long long l, r;
    cin >> l >> r;
    m[i] = P(l, r);
    qu[i] = P(l, r);
  }
  sort(qu.begin(), qu.end(), comp);
  vector<long long> num(500010, -1);
  int ind = 0;
  for (int i = 0; i < N; i++) {
    if (ind == Q)
      break;
    if (num[c[i]] != -1) {
      BIT.add(num[c[i]], -1);
      num[c[i]] = i + 1;
      BIT.add(num[c[i]], 1);
    } else {
      num[c[i]] = i + 1;
      BIT.add(num[c[i]], 1);
    }
    while (i + 1 == qu[ind].second) {
      long long temp = BIT.sum(i + 1) - BIT.sum(qu[ind].first - 1);
      ans[qu[ind]] = temp;
      ind++;
      if (ind == Q)
        break;
    }
  }
  for (auto v : m) {
    cout << ans[v.second] << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int MAX = 5 * 1e5 + 10;
struct seg {
  long long n;
  vector<bitset<MAX>> dat;
  seg(long long n_) {
    n = 1;
    while (n < n_)
      n *= 2;
    dat.resize(2 * n - 1);
  }
  void update(long long k, long long t) {
    k += n - 1;
    if (!dat[k].test(t))
      dat[k].set(t, 1);
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = dat[k * 2 + 1] | dat[k * 2 + 2];
    }
  }
  bitset<MAX> query(long long a, long long b, long long k, long long l,
                    long long r) {
    if (r <= a || b <= l) {
      bitset<MAX> bit;
      return bit;
    }
    if (a <= l && r <= b)
      return dat[k];
    else {
      bitset<MAX> vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      bitset<MAX> vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return vl | vr;
    }
  }
};
int main() {
  long long N, Q;
  cin >> N >> Q;
  vector<long long> c(N);
  seg segK(N);
  for (int i = 0; i < N; i++)
    cin >> c[i];
  for (int i = 0; i < N; i++)
    segK.update(i, c[i] - 1);
  for (int i = 0; i < Q; i++) {
    long long l, r;
    cin >> l >> r;
    bitset<MAX> bit = segK.query(l - 1, r, 0, 0, segK.n);
    cout << bit.count() << endl;
  }
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

int N, Q, C[500000], L[5000000], R[500000];
int bit[500001];

int sum(int i) {
  int s = 0;
  while (i > 0) {
    s += bit[i];
    i -= i & -i;
  }
  return s;
}

void add(int i, int x) {
  while (i <= N) {
    bit[i] += x;
    i += i & -i;
  }
}

int main() {
  cin >> N >> Q;
  REP(i, N) cin >> C[i];

  map<int, int> index;
  vector<pii> range;
  RREP(i, N) {
    if (index.count(C[i])) {
      range.push_back({i, index[C[i]]});
    }
    index[C[i]] = i;
  }
  vector<pii> qrange;
  REP(i, Q) {
    cin >> L[i] >> R[i];
    L[i]--;
    R[i]--;
    qrange.push_back({L[i], R[i]});
  }
  sort(ALL(qrange), greater<pii>());

  map<pii, int> ans;
  int ri = 0, qri = 0;
  while (ri < range.size() || qri < qrange.size()) {
    if (ri < range.size() && range[ri].first >= qrange[qri].first) {
      add(range[ri++].second, 1);
    } else {
      pii qr = qrange[qri++];
      int duplicate_count = sum(qr.second);
      ans[qr] = qr.second - qr.first + 1 - duplicate_count;
    }
  }
  REP(i, Q) cout << ans[{L[i], R[i]}] << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

int N;
set<int> dat[2 * 500000 - 1];
map<pii, set<int>> memo;

void update(int k, int n) {
  k += N - 1;
  dat[k].insert(n);
  while (k > 0) {
    k = (k - 1) / 2;
    dat[k].insert(n);
  }
}

set<int> query(int a, int b, int k, int l, int r) {
  if (memo.count({a, b}) > 0)
    return memo[{a, b}];
  if (r <= a || b <= l)
    return set<int>();

  if (a <= l && r <= b)
    return dat[k];
  else {
    set<int> v1 = query(a, b, k * 2 + 1, l, (l + r) / 2);
    set<int> v2 = query(a, b, k * 2 + 2, (l + r) / 2, r);
    set<int> ret;
    set_union(ALL(v1), ALL(v2), inserter(ret, ret.end()));
    memo[{a, b}] = ret;
    return ret;
  }
}

int main() {
  int Q, size;
  cin >> N >> Q;
  size = N;
  int n = 1;
  while (n < N)
    n *= 2;
  N = n;

  REP(i, size) {
    int c;
    cin >> c;
    update(i, c);
  }

  REP(i, Q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    set<int> ans = query(l, r + 1, 0, 0, N);
    memo[{l, r + 1}] = ans;
    cout << ans.size() << endl;
  }
  return 0;
}