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

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 2e6 + 5;
int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 20], sum[N * 20], vis[N * 20];

int id;
void build(int &o, int l, int r) {
  o = ++id;
  sum[o] = 0;
  if (l == r)
    return;
  int m = (l + r) >> 1;
  build(ls[o], l, m);
  build(rs[o], m + 1, r);
}

void update(int &o, int l, int r, int last, int pos, int val) {
  o = ++id;
  ls[o] = ls[last];
  rs[o] = rs[last];
  sum[o] = sum[last] + val;
  if (l == r)
    return;
  int m = (l + r) >> 1;
  if (pos <= m)
    update(ls[o], l, m, ls[last], pos, val);
  else
    update(rs[o], m + 1, r, rs[last], pos, val);
}
int query(int pos, int root, int l, int r) {
  if (l == r)
    return sum[root];
  int m = (l + r) >> 1;
  if (m >= pos)
    return sum[rs[root]] + query(pos, ls[root], l, m);
  else
    return query(pos, rs[root], m + 1, r);
}

int main() {
  int n, q, l, r, k;
  memset(vis, 0, sizeof(vis));
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  id = 0;
  build(rt[0], 1, n);
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]]) // 如果a[i]还未出现过
      update(rt[i], 1, n, rt[i - 1], i, 1);
    else { // 如果a[i]已经出现了
      int pos = vis[a[i]];
      int temp;
      update(temp, 1, n, rt[i - 1], pos, -1);
      update(rt[i], 1, n, temp, i, 1);
    }
    vis[a[i]] = i;
  }
  while (q--) {
    scanf("%d%d", &l, &r);
    int num = query(l, rt[r], 1, n);
    printf("%d\\n", num);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>

using namespace std;
const int N = 100010;
int a[N], b[N], rt[N * 20], ls[N * 20], rs[N * 20], sum[N * 20], vis[N * 20];

int id;
void build(int &o, int l, int r) {
  o = ++id;
  sum[o] = 0;
  if (l == r)
    return;
  int m = (l + r) >> 1;
  build(ls[o], l, m);
  build(rs[o], m + 1, r);
}

void update(int &o, int l, int r, int last, int pos, int val) {
  o = ++id;
  ls[o] = ls[last];
  rs[o] = rs[last];
  sum[o] = sum[last] + val;
  if (l == r)
    return;
  int m = (l + r) >> 1;
  if (pos <= m)
    update(ls[o], l, m, ls[last], pos, val);
  else
    update(rs[o], m + 1, r, rs[last], pos, val);
}
// 查询区间[L,R]内小于等于k的个数
int query(int pos, int root, int l, int r) {
  if (l == r)
    return sum[root];
  int m = (l + r) >> 1;
  if (m >= pos)
    return sum[rs[root]] + query(pos, ls[root], l, m);
  else
    return query(pos, rs[root], m + 1, r);
}

int main() {
  int n, q, l, r, k;
  memset(vis, 0, sizeof(vis));
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  id = 0;
  build(rt[0], 1, n);
  for (int i = 1; i <= n; i++) {
    if (!vis[a[i]]) // 如果a[i]还未出现过
      update(rt[i], 1, n, rt[i - 1], i, 1);
    else { // 如果a[i]已经出现了
      int pos = vis[a[i]];
      int temp;
      update(temp, 1, n, rt[i - 1], pos, -1);
      update(rt[i], 1, n, temp, i, 1);
    }
    vis[a[i]] = i;
  }
  while (q--) {
    scanf("%d%d", &l, &r);
    int num = query(l, rt[r], 1, n);
    printf("%d\\n", num);
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, a, b) for (register int i = a; i <= b; i++)

struct qu {
  int l, r, id;
  bool operator<(qu &f) const { return r < f.r; }
} b[500010];

int n, m;
int a[500010], c[500010], vis[1000010];
int ans[500010];

int lowbit(int x) { return x & (-x); }
void update(int i, int k) {
  while (i <= n) {
    c[i] += k;
    i += lowbit(i);
  }
}
int getsum(int i) {
  int res = 0;
  while (i) {
    res += c[i];
    i -= lowbit(i);
  }
  return res;
}

inline void solve(int T) {
  cin >> n >> m;
  rep(i, 1, n) cin >> a[i];
  rep(i, 1, m) {
    b[i].id = i;
    cin >> b[i].l >> b[i].r;
  }
  sort(b + 1, b + m + 1);
  int ind = 1;
  int cnt = 1;
  while (cnt <= m) {
    for (; ind <= b[cnt].r; ind++) {
      if (vis[a[ind]])
        update(vis[a[ind]], -1);
      update(ind, 1);
      vis[a[ind]] = ind;
    }
    ans[b[cnt].id] = getsum(b[cnt].r) - getsum(b[cnt].l - 1);
    cnt++;
  }
  rep(i, 1, m) cout << ans[i] << endl;
}
int main() {
  // ios_base::sync_with_stdio(0);
  // cin.tie(0);
  // cout.tie(0);

  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);

  int T = 1;
  // cin >> T;
  rep(i, 1, T) solve(i);
  system("pause");
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define foreach(it, v)                                                         \\
  for (__typeof(v.begin()) it = v.begin(); it != v.end(); it++)
#define chmax(a, b) a = max(a, b)
int n, Q, bl;
int c[500005];
int ans = 0, cnt[500005];
struct query {
  int l, r, id;
} q[500005];
int qans[500005];
bool cmp(query a, query b) {
  return (a.r / bl) == (b.r / bl) ? a.l < b.l : a.r < b.r;
}
void add(int x) {
  if (!cnt[c[x]]) {
    ans++;
  }
  cnt[c[x]]++;
}
void del(int x) {
  cnt[c[x]]--;
  if (!cnt[c[x]]) {
    ans--;
  }
}
int main() {
  cin >> n >> Q;
  bl = sqrt(n);
  for (int i = 1; i <= n; ++i) {
    cin >> c[i];
  }
  for (int i = 1; i <= Q; ++i) {
    cin >> q[i].l >> q[i].r;
    q[i].id = i;
  }
  sort(q + 1, q + 1 + Q, cmp);
  int l = 0, r = 0;
  for (int i = 1; i <= Q; ++i) {
    int ql = q[i].l, qr = q[i].r;
    //		cout<<ql<<' '<<qr<<'\\n';
    while (l < ql) {
      del(l);
      l++;
    }
    while (l > ql) {
      l--;
      add(l);
    }
    while (r > qr) {
      del(r);
      r--;
    }
    while (r < qr) {
      r++;
      add(r);
    }
    qans[q[i].id] = ans;
  }
  for (int i = 1; i <= Q; ++i) {
    cout << qans[i] << '\\n';
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

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;
  ve<int> a(n);
  rep(i, n) cin >> a[i];
  ve<int> pi(n + 1, -1);
  ve<ve<int>> ps(n);
  rep(i, n) {
    int l = pi[a[i]];
    if (l != -1)
      ps[l].push_back((int)i);
    pi[a[i]] = (int)i;
  }

  ve<ve<P>> qs(n);
  rep(qi, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    qs[l].push_back(P(r, qi));
  }

  ve<int> ans(q);
  BIT<int> d(n);
  for (int x = n - 1; x >= 0; x--) {
    for (int y : ps[x])
      d.add(y, 1);
    for (P query : qs[x]) {
      int r = query.first, i = query.second;
      ans[i] = (r - x + 1) - d.sum(r);
    }
  }
  rep(i, q) cout << ans[i] << endl;
  return 0;
}


Error: 0

Fixed submission:

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

int main() {
  int n, q;
  cin >> n >> q;
  ve<int> a(n);
  rep(i, n) cin >> a[i];
  ve<ve<int>> cn(n + 1, ve<int>(n + 1, 0));
  ve<int> chk(n, -1);
  rep(i, n) {
    if (chk[a[i]] != -1) {
      cn[chk[a[i]] + 1][i + 1]++;
    }
    chk[a[i]] = (int)i;
  }
  ve<ve<int>> sm(n + 1, ve<int>(n + 1, 0));
  rep(i, n + 1) rep(j, n) { sm[i][j + 1] = sm[i][j] + cn[i][j + 1]; }
  rep(i, n + 1) rep(j, n) { sm[j + 1][i] += sm[j][i]; }
  rep(Q, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    int ans = sm[r + 1][r + 1] - sm[r + 1][l] - sm[l][r + 1] + sm[l][l];
    ans = r - l + 1 - ans;
    cout << ans << endl;
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

#define __ ios::sync_with_stdio(0), cin.tie(0);
#define dbg(a) cerr << __LINE__ << ": " << #a << " = " << a << '\\n'

typedef long long ll;

const int N = 5e5 + 5, SZ = 720;
int n, q, a[N], cnt[N], dsnct = 0;

struct Qry {
  int idx, l, r;
  bool operator<(Qry oth) const {
    return make_pair(l / SZ, r) < make_pair(oth.l / SZ, oth.r);
  }
} qry[N];

void add(int idx) {
  cnt[a[idx]]++;
  if (cnt[a[idx]] == 1)
    dsnct++;
}

void remove(int idx) {
  cnt[a[idx]]--;
  if (cnt[a[idx]] == 0)
    dsnct--;
}

int get_ans() { return dsnct; }

vector<int> mos() {
  sort(qry, qry + q);
  int curl = 0, curr = -1;
  vector<int> ans(q);
  for (int i = 0; i < q; ++i) {
    while (curl > qry[i].l) {
      curl--;
      add(curl);
    }
    while (curr < qry[i].r) {
      curr++;
      add(curr);
    }
    while (curl < qry[i].l) {
      remove(curl);
      curl++;
    }
    while (curr > qry[i].r) {
      remove(curr);
      curr--;
    }
    ans[qry[i].idx] = get_ans();
  }
  return ans;
}

int main() {
  __ cin >> n;
  cin >> q;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < q; ++i) {
    qry[i].idx = i;
    cin >> qry[i].l >> qry[i].r;
    qry[i].l--, qry[i].r--;
  }
  vector<int> ans = mos();
  for (int u : ans) {
    printf("%d\\n", u);
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define __ ios::sync_with_stdio(0), cin.tie(0);
#define dbg(a) cerr << __LINE__ << ": " << #a << " = " << a << '\\n'

typedef long long ll;

const int N = 5e4 + 5, SZ = 720;
int n, q, a[N], cnt[1000010], dsnct = 0;

struct Qry {
  int idx, l, r;
  bool operator<(Qry oth) const {
    return make_pair(l / SZ, r) < make_pair(oth.l / SZ, oth.r);
  }
} qry[200010];

void add(int idx) {
  cnt[a[idx]]++;
  if (cnt[a[idx]] == 1)
    dsnct++;
}

void remove(int idx) {
  cnt[a[idx]]--;
  if (cnt[a[idx]] == 0)
    dsnct--;
}

int get_ans() { return dsnct; }

vector<int> mos() {
  sort(qry, qry + q);
  int curl = 0, curr = -1;
  vector<int> ans(q);
  for (int i = 0; i < q; ++i) {
    while (curl > qry[i].l) {
      curl--;
      add(curl);
    }
    while (curr < qry[i].r) {
      curr++;
      add(curr);
    }
    while (curl < qry[i].l) {
      remove(curl);
      curl++;
    }
    while (curr > qry[i].r) {
      remove(curr);
      curr--;
    }
    ans[qry[i].idx] = get_ans();
  }
  return ans;
}

int main() {
  __ cin >> n;
  cin >> q;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < q; ++i) {
    qry[i].idx = i;
    cin >> qry[i].l >> qry[i].r;
    qry[i].l--, qry[i].r--;
  }
  vector<int> ans = mos();
  for (int u : ans) {
    printf("%d\\n", u);
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#ifdef _LOCAL_DEBUG
#include "templates/debug.h"
#else
#define debug(...)
#endif

constexpr int mxn = 5e5 + 5;
int n, q, color[mxn], answer[mxn];
struct BIT {
  int t[mxn];
  void add(int i, int d) {
    for (; i < n + 2; i += i & -i)
      t[i] += d;
  }
  int get(int i) {
    int res = 0;
    for (; i > 0; i -= i & -i)
      res += t[i];
    return res;
  }
} ft;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> color[i], color[i]--;
  vector<int> last_seen(n, n + 1);
  vector<vector<pair<int, int>>> query_at(n);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    query_at[l - 1].emplace_back(r, i);
  }
  for (int l = n - 1; l >= 0; l--) {
    ft.add(last_seen[color[l]], -1);
    ft.add(l + 1, 1);
    last_seen[color[l]] = l + 1;
    for (auto [r, i] : query_at[l])
      answer[i] = ft.get(r);
  }
  for (int i = 0; i < q; i++)
    cout << answer[i] << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

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

#ifdef _LOCAL_DEBUG
#include "templates/debug.h"
#else
#define debug(...)
#endif

constexpr int mxn = 5e5 + 5;
struct BIT {
  int t[mxn];
  void add(int i, int d) {
    for (; i < mxn; i += i & -i)
      t[i] += d;
  }
  int get(int i) {
    int res = 0;
    for (; i > 0; i -= i & -i)
      res += t[i];
    return res;
  }
} ft;
int color[mxn], answer[mxn];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  for (int i = 0; i < n; i++)
    cin >> color[i];
  vector<int> last_seen(n, n + 1);
  vector<vector<pair<int, int>>> query_at(n);
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    query_at[l - 1].emplace_back(r, i);
  }
  for (int l = n - 1; l >= 0; l--) {
    ft.add(last_seen[color[l]], -1);
    ft.add(l + 1, 1);
    last_seen[color[l]] = l + 1;
    for (auto [r, i] : query_at[l])
      answer[i] = ft.get(r);
  }
  for (int i = 0; i < q; i++)
    cout << answer[i] << '\\n';
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#pragma GCC optimize(                                                          \\
    "Ofast,vpt,unroll-loops,shrink-wrap-separate,loop-nest-optimize")
#define mp make_pair
using namespace std;

inline int read() {
  char c = getchar_unlocked();
  int t = 0, f = 1;
  while (!isdigit(c))
    f = (c == '-' ? -1 : 1), c = getchar_unlocked();
  while (isdigit(c))
    t = t * 10 + c - '0', c = getchar_unlocked();
  return t * f;
}

const int N = 500001, len = 705;
int a[N], cnt[N], ans[N];

struct Query {
  int l, r, i;
  inline bool operator<(const Query &q) const {
    return mp(l / len, r) < mp(q.l / len, q.r);
  }
} q[N];

int main() {
  int n = read(), m = read(), l = 1, r = 0, cur = 0;
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 0; i < m; ++i)
    q[i] = {read(), read(), i};
  sort(q, q + m);
  for (int i = 0; i < m; ++i) {
    while (l > q[i].l)
      cur += !cnt[a[--l]]++;
    while (r < q[i].r)
      cur += !cnt[a[++r]]++;
    while (l < q[i].l)
      cur -= !--cnt[a[l++]];
    while (r > q[i].r)
      cur -= !--cnt[a[r--]];
    ans[q[i].i] = cur;
  }
  for (int i = 0; i < m; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize(                                                          \\
    "Ofast,vpt,unroll-loops,shrink-wrap-separate,loop-nest-optimize")
#define mp make_pair
using namespace std;

inline int read() {
  char c = getchar_unlocked();
  int t = 0, f = 1;
  while (!isdigit(c))
    f = (c == '-' ? -1 : 1), c = getchar_unlocked();
  while (isdigit(c))
    t = t * 10 + c - '0', c = getchar_unlocked();
  return t * f;
}

const int N = 500001, M = 100000000, len = 705;
int a[N], cnt[M], ans[N];

struct Query {
  int l, r, i;
  inline bool operator<(const Query &q) const {
    return mp(l / len, r) < mp(q.l / len, q.r);
  }
} q[N];

int main() {
  int n = read(), m = read(), l = 1, r = 0, cur = 0;
  for (int i = 1; i <= n; ++i)
    a[i] = read();
  for (int i = 0; i < m; ++i)
    q[i] = {read(), read(), i};
  sort(q, q + m);
  for (int i = 0; i < m; ++i) {
    while (l > q[i].l)
      cur += !cnt[a[--l]]++;
    while (r < q[i].r)
      cur += !cnt[a[++r]]++;
    while (l < q[i].l)
      cur -= !--cnt[a[l++]];
    while (r > q[i].r)
      cur -= !--cnt[a[r++]];
    ans[q[i].i] = cur;
  }
  for (int i = 0; i < m; ++i)
    printf("%d\\n", ans[i]);
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#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++)
    printf("%d\\n", nnn[i]);
  return 0;
}

Error: TLE

Fixed submission:

// It is made by XZZ
#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 = 500011;
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: Runtime Error

// F.

#include <algorithm>
#include <iostream>
#include <sstream>
#include <unordered_set>
#include <vector>

using namespace std;

typedef pair<int, int> II;

class BIT {
  std::vector<int> bit;
  int size;

public:
  BIT() {}
  BIT(int sz) { init(sz); }
  void init(int sz) { bit = std::vector<int>((size = sz) + 1); }
  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 <= size) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

int main(int argc, char *argv[]) {
  cout.precision(20);
#ifdef _MSC_VER
  while (true)
#endif
  {
    int n = -1, q, l, r;
    cin >> n >> q;
    if (n <= 0)
      return 0;
    vector<int> col(n), last(n), ans(q);
    for (int i = 0; i < n; ++i) {
      cin >> col[i];
      --col[i];
    }
    vector<pair<II, int>> query(q);
    for (int t = 0; t < q; ++t) {
      query[t].second = t;
      cin >> query[t].first.second >> query[t].first.first;
    }
    sort(query.begin(), query.end());
    int qi = 0;
    BIT bit(n);
    for (int i = 0; i < n; ++i) {
      if (last[col[i]]) {
        bit.add(last[col[i]], -1);
      }
      last[col[i]] = i + 1;
      bit.add(last[col[i]], 1);
      while (qi < q && query[qi].first.first == i + 1) {
        ans[query[qi].second] =
            bit.sum(i + 1) - bit.sum(query[qi].first.second - 1);
        ++qi;
      }
    }
    for (int t = 0; t < q; ++t) {
      cout << ans[t] << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

// F.

#include <algorithm>
#include <iostream>
#include <sstream>
#include <unordered_set>
#include <vector>

using namespace std;

typedef pair<int, int> II;

class BIT {
  std::vector<int> bit;
  int size;

public:
  BIT() {}
  BIT(int sz) { init(sz); }
  void init(int sz) { bit = std::vector<int>((size = sz) + 1); }
  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 <= size) {
      bit[i] += x;
      i += i & -i;
    }
  }
};

int main(int argc, char *argv[]) {
  cout.precision(20);
#ifdef _MSC_VER
  while (true)
#endif
  {
    int n = -1, q, l, r;
    cin >> n >> q;
    if (n <= 0)
      return 0;
    vector<int> col(n), last(n), ans(n);
    for (int i = 0; i < n; ++i) {
      cin >> col[i];
      --col[i];
    }
    vector<pair<II, int>> query(q);
    for (int t = 0; t < q; ++t) {
      query[t].second = t;
      cin >> query[t].first.second >> query[t].first.first;
    }
    sort(query.begin(), query.end());
    int qi = 0;
    BIT bit(n + 1);
    for (int i = 0; i < n; ++i) {
      if (last[col[i]]) {
        bit.add(last[col[i]], -1);
      }
      last[col[i]] = i + 1;
      bit.add(last[col[i]], 1);
      while (qi < q && query[qi].first.first == i + 1) {
        ans[query[qi].second] =
            bit.sum(i + 1) - bit.sum(query[qi].first.second - 1);
        ++qi;
      }
    }
    for (int t = 0; t < q; ++t) {
      cout << ans[t] << endl;
    }
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
vector<int> ql[N], qid[N], tree(N);
int n, q;
void update(int id, int val) {
  while (id <= n) {
    tree[id] += val;
    id += (id & -id);
  }
}
int query(int id) {
  int ans = 0;
  while (id > 0) {
    ans += tree[id];
    id -= (id & -id);
  }
  return ans;
}
int get(int l, int r) { return query(r) - query(l - 1); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  cin >> q;
  int l, r, x;
  int c[n + 1], a[n + 1] = {0}, ans[q];
  for (int i = 1; i <= n; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < q; i++) {
    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)
      update(a[c[i]], -1);
    update(i, 1);
    a[c[i]] = i;
    for (int j = 0; j < ql[i].size(); j++) {
      ans[qid[i][j]] = get(ql[i][j], i);
    }
  }
  for (int i = 0; i < q; i++)
    cout << ans[i] << "\\n";
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
int a[500005], b[500005];
int n, q;
set<int> s;
void inp() {
  cin >> n >> q;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
}
void process() {
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    for (int i = l; i <= r; i++)
      s.insert(a[i]);
    cout << s.size() << '\\n';
    s.clear();
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // freopen("test.inp","r", stdin);
  inp();
  process();
  return 0;
}