alexjercan/bugnet huggingface dataset:
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int N, M;
const int SIZE = 500005;
int c[SIZE];
int A[SIZE];
int Next[SIZE];
int res[2000005];
int show[10000005];
bool fir[SIZE];
struct Q {
  int l, r;
  int pos;
} q[2000005];
int lowbit(int k) { return k & (-k); }
void modify(int n, int v) {
  while (n <= N) {
    c[n] += v;
    n += lowbit(n);
  }
}
int sum(int n) {
  int ans = 0;
  while (n > 0) {
    ans += c[n];
    n -= lowbit(n);
  }
  return ans;
}
int cmp(Q a, Q b) { return a.l < b.l; }
int main() {
  cin >> N >> M;
  for (int i = 1; i <= N; i++)
    scanf("%d", &A[i]);

  for (int i = N; i >= 1; i--) {
    if (!show[A[i]]) {
      show[A[i]] = i;
      fir[i] = true;
    } else {
      Next[i] = show[A[i]];
      fir[Next[i]] = false;
      fir[i] = true;
      show[A[i]] = i;
    }
  }

  for (int i = 1; i <= M; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].pos = i;
  }
  sort(q + 1, q + 1 + M, cmp);

  for (int i = 1; i <= N; i++)
    if (fir[i]) {
      modify(i, 1);
    }
  int qtemp = q[1].l;
  int ptr = 1;
  for (int i = 1; i <= M; i++) {
    for (; ptr < q[i].l; ptr++) {
      if (fir[ptr]) {
        modify(ptr, -1);
        fir[ptr] = false;
        if (Next[ptr]) {
          fir[Next[ptr]] = true;
          modify(Next[ptr], 1);
        }
      }
    }
    ptr = q[i].l;
    qtemp = q[i].l;
    res[q[i].pos] = sum(q[i].r) - sum(q[i].l - 1);
  }
  for (int i = 1; i <= M; i++)
    printf("%d\\n", res[i]);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int N, M;
const int SIZE = 50005;
int c[SIZE];
int A[SIZE];
int Next[SIZE];
int res[200005];
int show[1000005];
bool fir[SIZE];
struct Q {
  int l, r;
  int pos;
} q[200005];
int lowbit(int k) { return k & (-k); }
void modify(int n, int v) {
  while (n <= N) {
    c[n] += v;
    n += lowbit(n);
  }
}
int sum(int n) {
  int ans = 0;
  while (n > 0) {
    ans += c[n];
    n -= lowbit(n);
  }
  return ans;
}
int cmp(Q a, Q b) { return a.l < b.l; }
int main() {
  cin >> N >> M;
  for (int i = 1; i <= N; i++)
    scanf("%d", &A[i]);

  for (int i = N; i >= 1; i--) {
    if (!show[A[i]]) {
      show[A[i]] = i;
      fir[i] = true;
    } else {
      Next[i] = show[A[i]];
      fir[Next[i]] = false;
      fir[i] = true;
      show[A[i]] = i;
    }
  }

  for (int i = 1; i <= M; i++) {
    scanf("%d%d", &q[i].l, &q[i].r);
    q[i].pos = i;
  }
  sort(q + 1, q + 1 + M, cmp);

  for (int i = 1; i <= N; i++)
    if (fir[i]) {
      modify(i, 1);
    }
  int qtemp = q[1].l;
  int ptr = 1;
  for (int i = 1; i <= M; i++) {
    for (; ptr < q[i].l; ptr++) {
      if (fir[ptr]) {
        modify(ptr, -1);
        fir[ptr] = false;
        if (Next[ptr]) {
          fir[Next[ptr]] = true;
          modify(Next[ptr], 1);
        }
      }
    }
    ptr = q[i].l;
    qtemp = q[i].l;
    res[q[i].pos] = sum(q[i].r) - sum(q[i].l - 1);
  }
  for (int i = 1; i <= M; i++)
    printf("%d\\n", res[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define maxn 1000119
int num[maxn], tree[maxn], booll[maxn], nnn[maxn], N, ww;
;
// num数组保存原数列，tree树状数组，nnn保存结果
struct tt {
  int l, r; // 左右边界
  int pos;  // 原位置（因为我们要离线排序后处理）
};
tt ask[maxn];
bool cmp(tt x, tt y) {
  return x.r < y.r;
  // 快排大法好，没有什么是快排解决不来哦
  // 实在不行就加上一个cmp
}
int lowbit(int n) {
  return n & (-n);
  // 树状数组核心操作×1
}
void add(int n, int now) {
  while (n <= N) {
    tree[n] += now;
    n += lowbit(n);
  }
  // 树状数组核心操作×2-->更新操作
}
int sum(int n) {
  int ans = 0;
  while (n != 0) {
    ans += tree[n];
    n -= lowbit(n);
  }
  return ans;
  // 树状数组核心操作×3———>查询操作
}
int main() {
  scanf("%d", &N);
  scanf("%d", &ww);
  for (int i = 1; i <= N; i++)
    scanf("%d", &num[i]);
  for (int i = 1; i <= ww; i++) {
    scanf("%d%d", &ask[i].l, &ask[i].r);
    ask[i].pos = i; // 存储初始位置
  }
  sort(ask + 1, ask + 1 + ww, cmp); // 按r排序
  int next = 1;
  for (int i = 1; i <= ww; i++) {
    for (int j = next; j <= ask[i].r; j++) {
      if (booll[num[j]])
        add(booll[num[j]], -1);
      // 之前打过标记，在之前的位置加上-1，保证无重复
      add(j, 1);
      booll[num[j]] = j;
    }
    next = ask[i].r + 1;
    // 更新下一次查询的位置
    nnn[ask[i].pos] = sum(ask[i].r) - sum(ask[i].l - 1);
    // 按询问编号存储每组询问的结果
  }
  for (int i = 1; i <= ww; i++)
    cout << nnn[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#define il inline
#define rg register
#define vd void
#define sta static
typedef long long ll;
il int gi() {
  rg int x = 0, f = 1;
  rg char ch = getchar();
  while (ch < '0' || ch > '9')
    f = ch == '-' ? -1 : f, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = x * 10 + ch - '0', ch = getchar();
  return x * f;
}
const int maxn = 50011;
int n;
namespace BITSegtree {
#define mid ((l + r) >> 1)
int rt[maxn], ls[maxn * 100], rs[maxn * 100], sum[maxn * 100], index;
il vd _update(int &x, int l, int r, const int &p, const int &k) {
  if (!x)
    x = ++index;
  sum[x] += k;
  if (l == r)
    return;
  if (p <= mid)
    _update(ls[x], l, mid, p, k);
  else
    _update(rs[x], mid + 1, r, p, k);
}
il int _query(int x, int l, int r, const int &L, const int &R) {
  if (!x || r < L || R < l)
    return 0;
  if (L <= l && r <= R)
    return sum[x];
  return _query(ls[x], l, mid, L, R) + _query(rs[x], mid + 1, r, L, R);
}
il int lb(const int &x) { return x & -x; }
il vd Update(int x, int y) {
  while (x <= n)
    _update(rt[x], 1, n + 1, y + 1, 1), x += lb(x);
}
il int Query(int l, int r, int x, int y) {
  sta int ret;
  ret = 0;
  while (r)
    ret += _query(rt[r], 1, n + 1, x + 1, y + 1), r -= lb(r);
  --l;
  while (l)
    ret -= _query(rt[l], 1, n + 1, x + 1, y + 1), l -= lb(l);
  return ret;
}
} // namespace BITSegtree
int lst[1000001];
int main() {
  using namespace BITSegtree;
  n = gi();
  int k, m = gi();
  for (rg int i = 1; i <= n; ++i)
    k = gi(), Update(i, lst[k]), lst[k] = i;
  int l, r;
  while (m--)
    l = gi(), r = gi(), printf("%d\\n", Query(l, r, 0, l - 1));
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

vector<int> BIT; // 1-indexed

void bitadd(int faddend, int findex) { // call by reference
  for (int i = findex + 1; i < BIT.size(); i += (i & -i))
    BIT[i] += faddend;
  return;
}

int bitsum(int fright) {
  int res = 0;
  for (int i = fright + 1; i > 0; i -= (i & -i))
    res += BIT[i];
  return res;
}

int main(void) {
  int N, Q, cbuf, lbuf, rbuf;
  vector<int> c;
  cin >> N >> Q;
  BIT.assign(N + 1, 0);
  vector<int> Colors(N, -1);
  vector<int> colFormer(N, -1);
  vector<int> res(Q);
  vector<vector<pair<int, int>>> query(N);
  for (int i = 0; i < N; i++) {
    cin >> cbuf;
    cbuf--;
    c.push_back(cbuf);
    colFormer[i] = Colors[cbuf];
    Colors[cbuf] = i;
  }
  for (int iquery = 0; iquery < Q; iquery++) {
    cin >> lbuf >> rbuf;
    lbuf--;
    rbuf--;
    query[rbuf].push_back(pair<int, int>(iquery, lbuf));
  }
  int iquery_sorted = 0;
  for (int curR = 0; curR < N; curR++) {
    if (colFormer[curR] >= 0)
      bitadd(-1, colFormer[curR]);
    bitadd(1, curR);
    int rightsum = 0;
    int querysize = query[curR].size();
    if (querysize > 0)
      rightsum = bitsum(curR);
    for (int i = 0; i < querysize; i++) {
      res[query[curR][i].first] = rightsum - bitsum(query[curR][i].second - 1);
    }
  }
  for (int iquery = 0; iquery < Q; iquery++)
    cout << res[iquery] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

void bitadd(vector<int> &FBIT, int faddend, int findex) { // call by reference
  for (int i = findex + 1; i < FBIT.size(); i += (i & -i))
    FBIT[i] += faddend;
  return;
}

int bitsum(vector<int> FBIT, int fright) {
  int res = 0;
  for (int i = fright + 1; i > 0; i -= (i & -i))
    res += FBIT[i];
  return res;
}

int main(void) {
  int N, Q, cbuf, lbuf, rbuf;
  vector<int> c;
  cin >> N >> Q;
  vector<int> Colors(N, -1);
  vector<int> colFormer(N, -1);
  vector<int> res(Q);
  vector<int> BIT(N + 1, 0); // 1-indexed
  vector<vector<pair<int, int>>> query(N);
  for (int i = 0; i < N; i++) {
    cin >> cbuf;
    cbuf--;
    c.push_back(cbuf);
    colFormer[i] = Colors[cbuf];
    Colors[cbuf] = i;
  }
  for (int iquery = 0; iquery < Q; iquery++) {
    cin >> lbuf >> rbuf;
    lbuf--;
    rbuf--;
    query[rbuf].push_back(pair<int, int>(iquery, lbuf));
  }
  int iquery_sorted = 0;
  for (int curR = 0; curR < N; curR++) {
    if (colFormer[curR] >= 0)
      bitadd(BIT, -1, colFormer[curR]);
    bitadd(BIT, 1, curR);
    int rightsum = 0;
    int querysize = query[curR].size();
    if (querysize > 0)
      rightsum = bitsum(BIT, curR);
    for (int i = 0; i < querysize; i++) {
      res[query[curR][i].first] =
          rightsum - bitsum(BIT, query[curR][i].second - 1);
    }
  }
  for (int iquery = 0; iquery < Q; iquery++)
    cout << res[iquery] << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, s, n) for (int i = (s); i <= (int)(n); i++)

int main() {
  int N;
  int Q;
  int p_l;
  int r;
  int ret;
  int retl;

  cin >> N >> Q;
  vector<int> c(N + 1);    // 色玉
  vector<int> d(N + 1);    // 色玉の良い位置
  vector<int> data(N + 1); // 良い位置には1、悪い位置は0を代入
  vector<int> q_l(Q + 1);  // 問題の左側
  vector<vector<int>> qi(N + 1); // 問題の番号管理
  vector<int> ans(Q + 1);        // 答え

  rep3(i, 1, N) cin >> c.at(i);
  rep3(i, 1, Q) {
    cin >> q_l.at(i) >> r;
    qi.at(r).push_back(i);
  }
  rep3(i, 1, N) {
    p_l = d.at(c.at(i));
    if (p_l > 0)
      for (int x = p_l; x <= N; x += x & -x)
        data.at(x)--;
    for (int x = i; x <= N; x += x & -x)
      data.at(x)++;
    d.at(c.at(i)) = i;
    if (qi.at(i).size() > 0) {
      ret = 0;
      for (int x = i; x > 0; x -= -x & x)
        ret += data.at(x);
      rep(j, qi.at(i).size()) {
        retl = 0;
        for (int x = q_l.at(qi.at(i).at(j)) - 1; x > 0; x -= -x & x)
          retl += data.at(x);
        ans.at(qi.at(i).at(j)) = ret - retl;
      }
    }
  }
  rep3(i, 1, Q) cout << ans.at(i) << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define rep3(i, s, n) for (int i = (s); i <= (int)(n); i++)

int main() {
  int N;
  int Q;
  int p_l;
  int r;
  int ret;
  int retl;

  cin >> N >> Q;
  vector<int> c(N + 1);    // 色玉
  vector<int> d(N + 1);    // 色玉の良い位置
  vector<int> data(N + 1); // 良い位置には1、悪い位置は0を代入
  vector<int> q_l(Q + 1);  // 問題の左側
  vector<vector<int>> qi(N + 1); // 問題の番号管理
  vector<int> ans(N + 1);        // 答え

  rep3(i, 1, N) cin >> c.at(i);
  rep3(i, 1, Q) {
    cin >> q_l.at(i) >> r;
    qi.at(r).push_back(i);
  }
  rep3(i, 1, N) {
    p_l = d.at(c.at(i));
    if (p_l > 0)
      for (int x = p_l; x <= N; x += x & -x)
        data.at(x)--;
    for (int x = i; x <= N; x += x & -x)
      data.at(x)++;
    d.at(c.at(i)) = i;
    if (qi.at(i).size() > 0) {
      ret = 0;
      for (int x = i; x > 0; x -= -x & x)
        ret += data.at(x);
      rep(j, qi.at(i).size()) {
        retl = 0;
        for (int x = q_l.at(qi.at(i).at(j)) - 1; x > 0; x -= -x & x)
          retl += data.at(x);
        ans.at(qi.at(i).at(j)) = ret - retl;
      }
    }
  }
  rep3(i, 1, N) cout << ans.at(i) << endl;
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

// Binary Indexed Tree (Fenwick Tree)
// https://youtu.be/lyHk98daDJo?t=7960
template <typename T> struct BIT {
  int n;
  vector<T> d;
  BIT(int n = 0) : n(n), d(n + 1) {}
  void add(int i, T x = 1) {
    for (i++; i <= n; i += i & -i) {
      d[i] += x;
    }
  }
  T sum(int i) {
    T x = 0;
    for (i++; i; i -= i & -i) {
      x += d[i];
    }
    return x;
  }
  T sum(int l, int r) { return sum(r - 1) - sum(l - 1); }
};

int main() {
  int n, Q;
  cin >> n >> Q;
  int ans[Q];
  vector<int> c(n), prev(n + 1, -1);
  vector<vector<int>> p(n);
  vector<vector<P>> q(n);
  rep(i, n) {
    cin >> c[i];
    if (prev[c[i]] != -1) {
      p[prev[c[i]]].push_back(i);
    }
    prev[c[i]] = i;
  }

  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    q[l].emplace_back(r, i);
  }

  BIT<int> bit(n);
  for (int i = n - 1; i >= 0; i--) {
    for (int y : p[i]) {
      bit.add(y);
    }
    for (P query : q[i]) {

      ans[query.second] = query.first - i + 1 - bit.sum(query.first);
    }
  }

  rep(i, Q) { cout << ans[i] << endl; }

  /*rep(i, p.size()){
    cout << p[i].first << " " << p[i].second << endl;
  }*/

  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int n, Q;
  cin >> n >> Q;
  int ans[Q];
  vector<int> c(n), prev(n + 1, -1);
  vector<vector<int>> p(n);
  vector<vector<P>> q(n);
  rep(i, n) {
    cin >> c[i];
    if (prev[c[i]] != -1) {
      p[prev[c[i]]].push_back(i);
    }
    prev[c[i]] = i;
  }

  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    q[l].emplace_back(r, i);
  }

  multiset<int> st;
  int id = 0, idq = 0;
  for (int i = n - 1; i >= 0; i--) {
    for (int y : p[i]) {
      st.insert(y);
    }
    for (P query : q[i]) {
      auto iter_upper = upper_bound(st.begin(), st.end(), query.first);
      int idx_upper = distance(st.begin(), iter_upper);
      ans[query.second] = query.first - i + 1 - idx_upper;
    }
  }

  rep(i, Q) { cout << ans[i] << endl; }

  /*rep(i, p.size()){
    cout << p[i].first << " " << p[i].second << endl;
  }*/

  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void update(vector<int> &bit, int i, int x) {
  int n = bit.size();
  while (i < n) {
    bit[i] += x;
    i += i & -i;
  }
}

int sum(vector<int> &bit, int i) {
  int res = 0;
  while (i > 0) {
    res += bit[i];
    i -= i & -i;
  }
  return res;
}
int sum(vector<int> &bit, int i, int j) {
  return sum(bit, j) - sum(bit, i - 1);
}
int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> pos_col(N + 1, 0); // position of the rightmost color[i]
  vector<int> c(N + 1);
  for (int i = 1; i <= N; i++) {
    cin >> c[i];
  }
  vector<tuple<int, int, int>> queries(Q);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    get<0>(queries[i]) = r;
    get<1>(queries[i]) = l;
    get<2>(queries[i]) = i;
  }
  sort(queries.begin(), queries.end());
  vector<int> bit(N + 1, 0);
  vector<int> res(Q, 0);
  int cp = 1; // current position
  for (int i = 0; i < Q; i++) {
    while (cp <= get<0>(queries[i])) {
      int cc = c[cp]; // current color
      if (pos_col[cc] != 0)
        update(bit, pos_col[cc], -1);
      update(bit, cp, 1);
      pos_col[cc] = cp;
      cp++;
    }
    res[get<2>(queries[i])] = sum(bit, get<1>(queries[i]), get<0>(queries[i]));
  }
  for (int i = 0; i < Q; i++) {
    cout << res[i] << endl;
  }
}

Error: TLE

Fixed submission:


#include <bits/stdc++.h>
using namespace std;
using ll = long long;

void init(vector<vector<int>> &seg, vector<int> c, int n) {
  int N = c.size();
  for (int i = 0; i < N; i++)
    seg[i + n - 1].push_back(c[i]);
  for (int i = n - 2; i >= 0; i--)
    set_union(seg[2 * i + 1].begin(), seg[2 * i + 1].end(),
              seg[2 * i + 2].begin(), seg[2 * i + 2].end(),
              inserter(seg[i], end(seg[i])));
}
vector<int> query(vector<vector<int>> &seg, int n, int a, int b, int k = 0,
                  int l = 0, int r = -1) {
  vector<int> emp;
  if (r < 0)
    r = n;
  if (r <= a || b <= l)
    return emp;
  if (a <= l && r <= b)
    return seg[k];
  vector<int> res1 = query(seg, n, a, b, 2 * k + 1, l, (l + r) / 2);
  vector<int> res2 = query(seg, n, a, b, 2 * k + 2, (l + r) / 2, r);
  vector<int> res;
  set_union(res1.begin(), res1.end(), res2.begin(), res2.end(),
            inserter(res, end(res)));
  return res;
}
int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> c(N);
  for (int i = 0; i < N; i++) {
    cin >> c[i];
  }
  int n = 1;
  while (n < N)
    n *= 2;
  vector<vector<int>> seg(2 * n - 1);
  init(seg, c, n);
  for (int i = 0; i < Q; i++) {
    int l, r;
    cin >> l >> r;
    //[l,r) (0-indexed)であることに注意
    cout << query(seg, n, l - 1, r).size() << endl;
  }
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
struct node {
  int st, en, id, ans;
} q[500010];
int cmp(struct node a, struct node b) { return a.en < b.en; }
int cmp1(struct node a, struct node b) { return a.id < b.id; }
int cal[500010];
int lowbit(int x) { return x & (-x); }
int getsum(int x) {
  int s = 0;
  for (; x > 0; x -= lowbit(x))
    s += cal[x];
  return s;
}
void update(int x, int value) {
  for (; x <= 500005; x += lowbit(x))
    cal[x] += value;
}
int n, Q;
int a[500010];
int main() {
  map<int, int> mp;
  int i;
  scanf("%d%d", &n, &Q);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 1; i <= Q; i++) {
    scanf("%d%d", &q[i].st, &q[i].en);
    q[i].id = i;
  }
  sort(q + 1, q + Q + 1, cmp);
  int temp = 1;
  for (i = 1; i <= Q; i++) {
    while (temp <= q[i].en) {
      if (mp[a[temp]] != 0) {
        update(mp[a[temp]], -1);
      }
      mp[a[temp]] = temp;
      update(temp, 1);
      temp++;
    }
    q[i].ans = getsum(q[i].en) - getsum(q[i].st - 1);
  }
  sort(q + 1, q + 1 + Q, cmp1);
  for (i = 1; i <= Q; i++) {
    printf("%d\\n", q[i].ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
using namespace std;
struct node {
  int st, en, id, ans;
} q[100010];
int cmp(struct node a, struct node b) { return a.en < b.en; }
int cmp1(struct node a, struct node b) { return a.id < b.id; }
int cal[100010];
int lowbit(int x) { return x & (-x); }
int getsum(int x) {
  int s = 0;
  for (; x > 0; x -= lowbit(x))
    s += cal[x];
  return s;
}
void update(int x, int value) {
  for (; x <= 100010; x += lowbit(x))
    cal[x] += value;
}
int n, Q;
int a[100010];
int main() {
  map<int, int> mp;
  int i;
  scanf("%d%d", &n, &Q);
  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (i = 1; i <= Q; i++) {
    scanf("%d%d", &q[i].st, &q[i].en);
    q[i].id = i;
  }
  sort(q + 1, q + Q + 1, cmp);
  int temp = 1;
  for (i = 1; i <= Q; i++) {
    while (temp <= q[i].en) {
      if (mp[a[temp]] != 0) {
        update(mp[a[temp]], -1);
      }
      mp[a[temp]] = temp;
      update(temp, 1);
      temp++;
    }
    q[i].ans = getsum(q[i].en) - getsum(q[i].st - 1);
  }
  sort(q + 1, q + 1 + Q, cmp1);
  for (i = 1; i <= Q; i++) {
    printf("%d\\n", q[i].ans);
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <vector>

std::vector<int> BIT; // その位置までの良い石の数の区間和
std::vector<int> color;
std::vector<int> right; // 色番号ごとの良い石の場所

int RegionSum(int l, int r);
int getSum(int r);
void addBIT(int idx, int value);

int main() {
  int N, Q;
  std::cin >> N >> Q;
  BIT.resize(N + 1, 0);
  color.resize(N + 1, 0);
  right.resize(N + 1, -1);

  for (int i = 1; i <= N; i++) {
    std::cin >> color[i];
  }

  std::vector<std::vector<int>> QueryL_(N + 1);
  std::vector<std::vector<int>> QueryID(N + 1);

  for (int i = 0; i < Q; i++) {
    int l, r;
    std::cin >> l >> r;
    QueryID[r].push_back(i);
    QueryL_[r].push_back(l);
  }

  std::vector<int> ans(Q);

  // BITの更新と答えの取得。BITは左から順番にi番目を更新していき更新するごとにiを右端とするクエリの答えを取得する
  for (int i = 1; i <= N; i++) {
    // BITの更新
    if (right[color[i]] != -1)
      addBIT(
          right[color[i]],
          -1); // すでにcolor[i]の良い石がある場合は前回までの良い石の位置までのBITを1減らす
    right[color[i]] = i;
    addBIT(i, 1);
    // クエリごとの答えの取得
    for (int j = 0; j < QueryL_[i].size(); j++) {
      ans[QueryID[i][j]] = RegionSum(
          QueryL_[i][j],
          i); // QueryL_[i][j]:区間右側をiとするクエリ集合のj番目のクエリのl
    }
  }

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

  return 0;
}

int RegionSum(int l, int r) { return getSum(r) - getSum(l - 1); }

int getSum(int r) {
  int Sum = 0;
  for (int i = r; i > 0; i -= i & (-i)) {
    Sum += BIT[i];
  }
  return Sum;
}

void addBIT(int idx, int value) {
  for (int i = idx; i <= BIT.size() - 1; i += i & (-i)) {
    BIT[i] += value;
  }
  return;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

void refreshSegTree(int idx);
int getColorCode(int l, int r, int segIdx, int segL, int segR);
std::vector<int> ColorSegTree;
int main() {
  int N, Q;
  std::cin >> N >> Q;
  int TreeSize = 1;
  while (TreeSize / 2 < N) {
    TreeSize = 2 * TreeSize;
  }
  TreeSize--;
  ColorSegTree.resize(TreeSize, 0);
  for (int i = 0; i < N; i++) {
    int ColorNum;
    std::cin >> ColorNum;
    ColorSegTree[i + (TreeSize - 1) / 2] = 1 << ColorNum - 1;
    refreshSegTree(i + (TreeSize - 1) / 2);
  }

  for (int i = 0; i < Q; i++) {
    int colorCode = 0;
    int l, r;
    std::cin >> l >> r;
    l--;
    r--;
    colorCode = getColorCode(l, r, 0, 0, (TreeSize - 1) / 2);
    int ans = 0;
    while (colorCode != 0) {
      if (colorCode & 1) {
        ans++;
      }
      colorCode = colorCode >> 1;
    }
    std::cout << ans << std::endl;
  }
  return 0;
}

void refreshSegTree(int idx) {
  if (idx == 0) {
    return;
  }
  int parent = (idx - 1) / 2;
  ColorSegTree[parent] = ColorSegTree[parent] | ColorSegTree[idx];
  refreshSegTree(parent);
  return;
}

int getColorCode(int l, int r, int segIdx, int segL, int segR) {
  if (r < segL || l > segR) {
    return 0;
  }
  if (l <= segL && segR <= r) {
    return ColorSegTree[segIdx];
  }
  int colorCodeL = 0, colorCodeR = 0;
  colorCodeL = getColorCode(l, r, 2 * segIdx + 1, segL, (segL + segR) / 2);
  colorCodeR = getColorCode(l, r, 2 * segIdx + 2, (segL + segR) / 2 + 1, segR);
  return colorCodeL | colorCodeR;
}