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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n, m;
struct Tree {
  int ls, rs, val;
} tre[maxn * 50];
int idx = 0, root[maxn];
int build(int l, int r) {
  int nod = ++idx;
  tre[nod].val = 0;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  tre[nod].ls = build(l, mid);
  tre[nod].rs = build(mid + 1, r);
  return nod;
}

int update(int pre, int l, int r, int pos, int v) {
  int nod = ++idx;
  tre[nod] = tre[pre];
  tre[nod].val += v;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    tre[nod].ls = update(tre[pre].ls, l, mid, pos, v);
  else
    tre[nod].rs = update(tre[pre].rs, mid + 1, r, pos, v);
  return nod;
}

int query(int id, int l, int r, int pos) {
  if (l == r)
    return tre[id].val;
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    return tre[tre[id].rs].val + query(tre[id].ls, l, mid, pos);
  } else
    return query(tre[id].rs, mid + 1, r, pos);
}

int a[maxn], vis[maxn];
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  root[0] = build(1, n);
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]])
      root[i] = update(root[i - 1], 1, n, i, 1);
    else {
      int t = update(root[i - 1], 1, n, vis[a[i]], -1);
      root[i] = update(t, 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    int tmp = query(root[r], 1, n, l);
    printf("%d\\n", tmp);
  }
}

int main() {
  solve();
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5 + 5;
int n, m;
struct Tree {
  int ls, rs, val;
} tre[maxn * 40];
int idx = 0, root[maxn];
int build(int l, int r) {
  int nod = ++idx;
  tre[nod].val = 0;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  tre[nod].ls = build(l, mid);
  tre[nod].rs = build(mid + 1, r);
  return nod;
}

int update(int pre, int l, int r, int pos, int v) {
  int nod = ++idx;
  tre[nod] = tre[pre];
  tre[nod].val += v;
  if (l == r)
    return nod;
  int mid = (l + r) >> 1;
  if (pos <= mid)
    tre[nod].ls = update(tre[pre].ls, l, mid, pos, v);
  else
    tre[nod].rs = update(tre[pre].rs, mid + 1, r, pos, v);
  return nod;
}

int query(int id, int l, int r, int pos) {
  if (l == r)
    return tre[id].val;
  int mid = (l + r) >> 1;
  if (pos <= mid) {
    return tre[tre[id].rs].val + query(tre[id].ls, l, mid, pos);
  } else
    return query(tre[id].rs, mid + 1, r, pos);
}

int a[maxn], vis[maxn];
void solve() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
  }
  root[0] = build(1, n);
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]])
      root[i] = update(root[i - 1], 1, n, i, 1);
    else {
      int t = update(root[i - 1], 1, n, vis[a[i]], -1);
      root[i] = update(t, 1, n, i, 1);
    }
    vis[a[i]] = i;
  }
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    int tmp = query(root[r], 1, n, l);
    printf("%d\\n", tmp);
  }
}

int main() {
  solve();
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

// x の最も下の立っているビットは，x & -x で取り 出せる
template <class T> class BIT {
public:
  vector<T> array;
  const int n;

public:
  // constractor
  BIT(int _n) : array(_n + 1, 0), n(_n) {}

  // sum [0,i]
  T sum(int i) {
    T s = 0;
    while (i > 0) {
      s += array[i];
      i -= i & -i;
    }
    return s;
  }

  // sum [i,j]
  T sum(int i, int j) {
    T ret_i = sum(i - 1);
    T ret_j = sum(j);
    return ret_j - ret_i;
  }

  // 1-index
  void add(int i, T x) {
    while (i <= n) {
      array[i] += x;
      i += i & -i;
    }
  }
};

int N = 500100;

int main() {
  int n, Q;
  cin >> n >> Q;
  BIT<int> bit(N);

  vector<int> a(N + 1, 0);
  vector<vector<int>> ql(N + 1);
  vector<vector<int>> qid(N + 1);
  vector<int> ans(N + 1, 0);

  vector<int> C(N + 1);
  for (int i = 1; i <= n; ++i) {
    cin >> C[i];
  }

  for (int i = 1; i <= Q; ++i) {
    int l, r;
    cin >> l >> r;
    ql[r].push_back(l);
    qid[r].push_back(i);
  }

  for (int i = 1; i <= n; ++i) {
    if (a[C[i]] > 0)
      bit.add(a[C[i]], -1);
    bit.add(i, +1);
    a[C[i]] = i;
    for (int j = 0; j < ql[i].size(); ++j) {
      ans[qid[i][j]] = bit.sum(ql[i][j], i);
    }
  }

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


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>

using namespace std;

// x の最も下の立っているビットは，x & -x で取り 出せる
template <class T> class BIT {
public:
  vector<T> array;
  const int n;

public:
  // constractor
  BIT(int _n) : array(_n + 1, 0), n(_n) {}

  // sum [0,i]
  T sum(int i) {
    T s = 0;
    while (i > 0) {
      s += array[i];
      i -= i & -i;
    }
    return s;
  }

  // sum [i,j]
  T sum(int i, int j) {
    T ret_i = sum(i - 1);
    T ret_j = sum(j);
    return ret_j - ret_i;
  }

  // 1-index
  void add(int i, T x) {
    while (i <= n) {
      array[i] += x;
      i += i & -i;
    }
  }
};

int main() {
  int N, Q;
  cin >> N >> Q;
  BIT<int> bit(N);

  vector<int> a(N + 1, 0);
  vector<vector<int>> ql(N + 1);
  vector<vector<int>> qid(N + 1);
  vector<int> ans(N + 1, 0);

  vector<int> C(N + 1);
  for (int i = 1; i <= N; ++i) {
    cin >> C[i];
  }

  for (int i = 1; i <= Q; ++i) {
    int l, r;
    cin >> l >> r;
    ql[r].push_back(l);
    qid[r].push_back(i);
  }

  for (int i = 1; i <= N; ++i) {
    if (a[C[i]] > 0)
      bit.add(a[C[i]], -1);
    bit.add(i, +1);
    a[C[i]] = i;
    for (int j = 0; j < ql[i].size(); ++j) {
      ans[qid[i][j]] = bit.sum(ql[i][j], i);
    }
  }

  for (int i = 1; i <= Q; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 500005;
const int maxq = 500005;
const int maxm = 500005;
int n, sqr, Q;
int a[maxn];
struct query {
  int l, r, lb, pos;
} q[maxq];
bool cmp(query a, query b) {
  if (a.lb == b.lb)
    return a.lb & 1 ? a.r > b.r : a.r < b.r;
  return a.lb < b.lb;
}
int ans[maxq], num[maxm], cnt;
inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x;
}
inline void write(int x) {
  if (x / 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
void solve() {
  int l = 1, r = 0;
  for (int i = 1; i <= Q; ++i) {
    while (r < q[i].r)
      cnt += !num[a[++r]]++;
    while (r > q[i].r)
      cnt -= !--num[a[r--]];
    while (l > q[i].l)
      cnt += !num[a[--l]]++;
    while (l < q[i].l)
      cnt -= !--num[a[l++]];
    ans[q[i].pos] = cnt;
  }
  for (int i = 1; i <= Q; ++i)
    write(ans[i]), puts("");
}
int main() {
  n = read();
  Q = read();
  for (int i = 0; i < n; ++i)
    a[i] = read();
  sqr = sqrt(n) + 1;
  for (int i = 1; i <= Q; ++i) {
    q[i].l = read();
    q[i].r = read();
    --q[i].l;
    --q[i].r;
    q[i].pos = i;
    q[i].lb = q[i].l / sqr;
  }
  sort(q + 1, q + Q + 1, cmp);
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
const int maxn = 30005;
const int maxq = 200005;
const int maxm = 1000005;
int n, sqr, Q;
int a[maxn];
struct query {
  int l, r, lb, pos;
} q[maxq];
bool cmp(query a, query b) {
  if (a.lb == b.lb)
    return a.lb & 1 ? a.r > b.r : a.r < b.r;
  return a.lb < b.lb;
}
int ans[maxq], num[maxm], cnt;
inline int read() {
  int x = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x;
}
inline void write(int x) {
  if (x / 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
void solve() {
  int l = 1, r = 0;
  for (int i = 1; i <= Q; ++i) {
    while (r < q[i].r)
      cnt += !num[a[++r]]++;
    while (r > q[i].r)
      cnt -= !--num[a[r--]];
    while (l > q[i].l)
      cnt += !num[a[--l]]++;
    while (l < q[i].l)
      cnt -= !--num[a[l++]];
    ans[q[i].pos] = cnt;
  }
  for (int i = 1; i <= Q; ++i)
    write(ans[i]), puts("");
}
int main() {
  n = read();
  Q = read();
  for (int i = 0; i < n; ++i)
    a[i] = read();
  sqr = sqrt(n) + 1;
  for (int i = 1; i <= Q; ++i) {
    q[i].l = read();
    q[i].r = read();
    --q[i].l;
    --q[i].r;
    q[i].pos = i;
    q[i].lb = q[i].l / sqr;
  }
  sort(q + 1, q + Q + 1, cmp);
  solve();
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007

struct mo {
  int l, r;
  bool operator<(const mo &o) const { return r < o.r; }
};

int main() {

  const int sq = 708;
  int n, Q;
  cin >> n >> Q;
  vector<int> c(n);
  rep(i, n) cin >> c[i];
  rep(i, n) c[i]--;
  vector<vector<pair<mo, int>>> q(sq);
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    int pos = l / sq;
    mo o = {l, r};
    q[pos].push_back(make_pair(o, i));
  }
  rep(i, sq) sort(ALL(q[i]));
  vector<int> res(Q, 0);
  rep(i, sq) {
    if (q[i].empty())
      continue;
    int l = q[i][0].first.l, r = q[i][0].first.r;
    vector<int> cnt(n, 0);
    int sum = 0;
    for (int j = l; j <= r; j++) {
      cnt[c[j]]++;
      if (cnt[c[j]] == 1)
        sum++;
    }
    for (auto nq : q[i]) {
      {
        if (nq.first.l < l) {
          for (l = l - 1; l >= nq.first.l; l--) {
            cnt[c[l]]++;
            if (cnt[c[l]] == 1)
              sum++;
          }
          l = nq.first.l;
        }
        for (r = r + 1; r <= nq.first.r; r++) {
          cnt[c[r]]++;
          if (cnt[c[r]] == 1)
            sum++;
        }
        r = nq.first.r;
      }
      {
        if (nq.first.l > l) {
          for (; l < nq.first.l; l++) {
            cnt[c[l]]--;
            if (cnt[c[l]] == 0)
              sum--;
          }
        }
      }
      res[nq.second] = sum;
    }
  }
  rep(i, Q) cout << res[i] << "\\n";

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define ALL(x) (x).begin(), (x).end()
#define MOD 1000000007

int main() {

  int n, q;
  scanf("%d %d", &n, &q);
  vector<int> v(n);
  rep(i, n) scanf("%d", &v[i]);
  int sq = 708;
  vector<set<int>> bg(sq);
  rep(i, n) {
    int pos = i / sq;
    bg[pos].insert(v[i]);
  }
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    l--, r--;
    set<int> st;
    for (int i = l / sq; i <= r / sq; i++) {
      if (l <= i * sq && r >= (i + 1) * sq - 1)
        for (auto iter = bg[i].begin(); iter != bg[i].end(); iter++)
          st.insert(*iter);
      else {
        for (int j = max(l, i * sq); j <= min(r, (i + 1) * sq - 1); j++) {
          st.insert(v[j]);
        }
      }
    }
    cout << st.size() << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 10;

struct Q {
  int l, r, idx;
};

Q queries[N];
int ans[N], vis[N], a[N], bit[N];

bool cmp(Q x, Q y) { return x.r < y.r; }

void update(int i, int val, int n) {
  while (i <= n) {
    bit[i] += val;
    i += i & -i;
  }
}

int query(int i, int n) {
  int sum = 0;
  while (i) {
    sum += bit[i];
    i -= i & -i;
  }
  return sum;
}

int main() {
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    vis[i + 1] = -1;
  }
  for (int i = 0; i < q; ++i) {
    int x, y;
    cin >> x >> y;
    queries[i].l = x - 1;
    queries[i].r = y - 1;
    queries[i].idx = i;
  }
  sort(queries, queries + q, cmp);
  int c = 0;
  for (int i = 0; i < n; i++) {
    if (vis[a[i]] != -1) {
      update(vis[a[i]] + 1, -1, n);
    }
    vis[a[i]] = i;
    update(i + 1, 1, n);
    while (c < q && queries[c].r == i) {
      ans[queries[c].idx] = query(queries[c].r + 1, n);
      ans[queries[c].idx] -= query(queries[c].l, n);
      ++c;
    }
  }
  for (int i = 0; i < q; i++) {
    cout << ans[i] << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int N = 5e5 + 10;

int c[N];
set<int> seg[N << 2];

void build(int i, int l, int r) {
  if (l == r) {
    seg[i].insert(c[l]);
    return;
  }
  build(i << 1, l, (l + r) >> 1);
  build(i << 1 | 1, ((l + r) >> 1) + 1, r);
  seg[i].insert(seg[i << 1].begin(), seg[i << 1].end());
  seg[i].insert(seg[i << 1 | 1].begin(), seg[i << 1 | 1].end());
}

set<int> query(int n, int l, int r, int a, int b) {
  set<int> left, right, res;
  if (b < l || a > r) {
    return res;
  }
  if (a <= l && r <= b) {
    return seg[n];
  }
  left = query(n << 1, l, (l + r) >> 1, a, b);
  res.insert(left.begin(), left.end());
  right = query(n << 1 | 1, ((l + r) >> 1) + 1, r, a, b);
  res.insert(right.begin(), right.end());
  return res;
}

int res(int l, int r, int n) {
  set<int> ans = query(1, 0, n - 1, l, r);
  return ans.size();
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; ++i) {
    cin >> c[i];
  }
  build(1, 0, n - 1);
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << res(l - 1, r - 1, n) << '\\n';
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    o_set;

struct qs {
  int l, r, id;
  qs(int l, int r, int id) {
    this->l = l;
    this->r = r;
    this->id = id;
  }
  bool operator<(qs b) {
    if (r != b.r)
      return r < b.r;
    return l < b.l;
  }
};

int n, q, i, nr, l, r;
vector<qs> qv;
o_set s;
int a[500005];
int last[500005];
int ans[500005];

int main() {
  cin >> n >> q;
  for (i = 1; i <= n; i++)
    cin >> a[i];

  for (i = 1; i <= q; i++) {
    cin >> l >> r;
    qv.push_back(qs(l, r, i));
  }
  sort(qv.begin(), qv.end());

  r = 0;
  for (auto it : qv) {
    while (r < it.r) {
      r++;
      if (last[a[r]])
        s.erase(last[a[r]]);
      last[a[r]] = r;
      s.insert(r);
    }

    nr = s.order_of_key(it.l) - 1;
    nr = s.size() - nr - 1;

    ans[it.id] = nr;
  }

  for (i = 1; i <= q; i++)
    cout << ans[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

using namespace std;
int sz;
struct qs {
  int l, r, id;
  qs(int l, int r, int id) {
    this->l = l;
    this->r = r;
    this->id = id;
  }
  bool operator<(qs b) {
    if (l / sz != b.l / sz)
      return l / sz < b.l / sz;
    return r < b.r;
  }
};
int n, q, i, l, r;
int nr;
vector<qs> qv;
int a[500005];
int ans[500005];
int fr[500005];
void add(int x) {
  fr[x]++;
  if (fr[x] == 1)
    nr++;
}
void del(int x) {
  fr[x]--;
  if (!fr[x])
    nr--;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
  }

  sz = sqrt(n);
  for (i = 1; i <= q; i++) {
    cin >> l >> r;
    qv.push_back(qs(l, r, i));
  }
  sort(qv.begin(), qv.end());

  l = 1;
  r = 0;
  for (auto it : qv) {
    while (r < it.r) {
      r++;
      add(a[r]);
    }
    while (l > it.l) {
      l--;
      add(a[l]);
    }
    while (r > it.r) {
      del(a[r]);
      r--;
    }
    while (l < it.l) {
      del(a[l]);
      l++;
    }

    ans[it.id] = nr;
  }

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

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

const int maxn = 500004, sq = 707;
int n, q, a[maxn], cnt[maxn];
pair<pair<int, int>, int> query[maxn];
int ans[maxn];

bool cmp(pair<pair<int, int>, int> aa, pair<pair<int, int>, int> bb) {
  if (aa.first.first / sq < bb.first.first / sq)
    return true;
  if (aa.first.first / sq > bb.first.first / sq)
    return false;
  return aa.first.second < bb.first.second;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < q; i++) {
    cin >> query[i].first.first >> query[i].first.second;
    query[i].second = i;
  }
  sort(query, query + q, cmp);
  memset(cnt, 0, sizeof cnt);
  cnt[a[0]] = 1;
  int l = 0, r = 0, tmp = 1;
  for (int i = 0; i < q; i++) {
    int ll = query[i].first.first - 1, rr = query[i].first.second - 1;
    if (r < rr) {
      for (int j = r + 1; j <= rr; j++) {
        if (cnt[a[j]] == 0)
          tmp++;
        cnt[a[j]]++;
      }
    } else if (r > rr) {
      for (int j = r; j > rr; j--) {
        cnt[a[j]]--;
        if (cnt[a[j]] == 0)
          tmp--;
      }
    }
    if (l < ll) {
      for (int j = l; j < ll; j++) {
        cnt[a[j]]--;
        if (cnt[a[j]] == 0)
          tmp--;
      }
    } else if (l > ll) {
      for (int j = l - 1; j >= ll; j--) {
        if (cnt[a[j]] == 0)
          tmp++;
        cnt[a[j]]++;
      }
    }
    r = rr;
    l = ll;
    //		cout << "(l,r)=(" << l+1 << "," << r+1 << "), ans=" << tmp <<
    //"\\n";
    ans[query[i].second] = tmp;
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";
}

Error: TLE

Fixed submission:

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

const int maxn = 500004;
int n, q, a[maxn];
unordered_set<int> tree[4 * maxn];

void build(int l, int r, int v) {
  if (l == r)
    tree[v].insert(a[l]);
  else {
    int mid = (l + r) / 2;
    build(l, mid, v * 2);
    build(mid + 1, r, v * 2 + 1);
    for (int u : tree[v * 2])
      tree[v].insert(u);
    for (int u : tree[v * 2 + 1])
      tree[v].insert(u);
  }
}

unordered_set<int> query(int l, int r, int v, int lb, int rb) {
  if (r < lb || l > rb)
    return {};
  if (l >= lb && r <= rb)
    return tree[v];
  int mid = (l + r) / 2;
  unordered_set<int> ret, sl = query(l, mid, v * 2, lb, rb),
                          sr = query(mid + 1, r, v * 2 + 1, lb, rb);
  for (int u : sl)
    ret.insert(u);
  for (int u : sr)
    ret.insert(u);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  build(0, n - 1, 1);
  while (q--) {
    int l, r;
    cin >> l >> r;
    cout << query(0, n - 1, 1, l - 1, r - 1).size() << "\\n";
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\\n'
#define FASTINOUT                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const int N = 5e5 + 9;
int a[N], Block, value, m[N];
bool ok[N];
struct query {
  int l, r, i;
};
query q[N];
bool f(query a, query b) {
  if (a.l / Block != b.l / Block)
    return a.l < b.l;
  return a.l / Block % 2 ? a.r < b.r : a.r > b.r;
}
void add(int pos) {
  int val = a[pos];
  m[val]++;
  if (m[val] == 1) {
    value++;
  } else if (!m[val]) {
    value--;
  }
}
void remove(int pos) {
  int val = a[pos];
  m[val]--;
  if (!m[val]) {
    value--;
  }
}
int main() {
  FASTINOUT;
  int x, t;
  scanf("%d%d", &x, &t);
  Block = sqrtl(x);
  for (int i = 0; i < x; i++)
    scanf("%d", &a[i]);
  for (int j = 0; j < t; j++) {
    scanf("%d%d", &q[j].l, &q[j].r);
    q[j].l--;
    q[j].r--;
    q[j].i = j;
  }
  sort(q, q + t, f);
  int start = 0, end = -1;
  vector<int> ans(t);
  for (int i = 0; i < t; i++) {
    int L = q[i].l, R = q[i].r;

    while (start > L)
      start--, add(start);

    while (end < R)
      end++, add(end);

    while (start < L)
      remove(start), start++;

    while (end > R)
      remove(end), end--;

    ans[q[i].i] = value;
  }
  for (int j : ans)
    printf("%d\\n", j);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\\n'
#define FASTINOUT                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
const int N = 5e5 + 9;
int a[N], Block, value, m[N];
bool ok[N];
struct query {
  int l, r, i;
};
query q[N];
bool f(query q1, query q2) {
  if (q1.l / Block != q2.l / Block)
    return q1.l / Block < q2.l / Block;

  return q1.r < q2.r;
}
void add(int pos) {
  int val = a[pos];
  m[val]++;
  if (m[val] == 1) {
    value++;
  } else if (!m[val]) {
    value--;
  }
}
void remove(int pos) {
  int val = a[pos];
  m[val]--;
  if (!m[val]) {
    value--;
  }
}
int main() {
  FASTINOUT;
  int x, t;
  scanf("%d%d", &x, &t);
  Block = sqrtl(x);
  for (int i = 0; i < x; i++)
    scanf("%d", &a[i]);
  for (int j = 0; j < t; j++) {
    scanf("%d%d", &q[j].l, &q[j].r);
    q[j].l--;
    q[j].r--;
    q[j].i = j;
  }
  sort(q, q + t, f);
  int start = 0, end = -1;
  vector<int> ans(t);
  for (int i = 0; i < t; i++) {
    int L = q[i].l, R = q[i].r;

    while (start > L)
      start--, add(start);

    while (end < R)
      end++, add(end);

    while (start < L)
      remove(start), start++;

    while (end > R)
      remove(end), end--;

    ans[q[i].i] = value;
  }
  for (int j : ans)
    printf("%d\\n", j);
  return 0;
}