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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int sz = 5e5 + 10;
int bt[sz], res[sz];
vector<vector<pair<int, int>>> qr(sz);
int n, q;

void upd(int p, int x) {
  for (int i = p; i <= n; i += i & -i) {
    bt[i] += x;
  }
}

int query(int p) {
  int res = 0;
  for (int i = p; i; i -= i & -i) {
    res += bt[i];
  }
  return res;
}

int main() {
  cin >> n >> q;
  vector<int> a(n);
  unordered_map<int, int> mp;
  for (auto &x : a)
    cin >> x;
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    l--, r--;
    qr[r].push_back({l, i});
  }
  for (int i = 0; i < n; i++) {
    if (mp.find(a[i]) != mp.end()) {
      upd(mp[a[i]] + 1, -1);
    }
    upd(i + 1, 1);
    mp[a[i]] = i;
    for (auto x : qr[i]) {
      int l = x.first + 1;
      int r = i + 1;
      int idx = x.second;
      if (l == 1) {
        res[idx] = query(r);
      } else {
        res[idx] = query(r) - query(l - 1);
      }
    }
  }
  for (int i = 0; i < q; i++) {
    cout << res[i] << "\\n";
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

struct seg_tree {
  int n;
  vector<int> a;
  vector<set<int>> st;
  seg_tree(vector<int> &_a) {
    n = _a.size();
    a = _a;
    st.assign(4 * n, {});
    build(1, 0, n);
  }
  int left(int p) { return p << 1; }
  int right(int p) { return (p << 1) + 1; }
  void build(int p, int l, int r) {
    if (r - l < 2) {
      st[p].insert(a[l]);
      return;
    }
    int mid = (l + r) / 2;
    build(left(p), l, mid);
    build(right(p), mid, r);
    for (int x : st[left(p)]) {
      st[p].insert(x);
    }
    for (int x : st[right(p)]) {
      st[p].insert(x);
    }
  }
  set<int> query(int i, int j, int p, int l, int r) {
    if (i >= r || j <= l)
      return {};
    if (l >= i && r <= j)
      return st[p];
    int mid = (l + r) / 2;
    auto s1 = query(i, j, left(p), l, mid);
    auto s2 = query(i, j, right(p), mid, r);
    set<int> tmp;
    for (int x : s1) {
      tmp.insert(x);
    }
    for (int x : s2) {
      tmp.insert(x);
    }
    return tmp;
  }
  int un(int i, int j) {
    auto s = query(i, j, 1, 0, n);
    return s.size();
  }
};

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  for (auto &x : a)
    cin >> x;
  seg_tree st(a);
  // for(int x : st.st[1]) {
  //     cout << x << " ";
  // }
  // cout <<"\\n";
  while (q--) {
    int i, j;
    cin >> i >> j;
    i--;
    cout << st.un(i, j) << "\\n";
  }

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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
using namespace std;
using ll = long long;
using PL = pair<ll, ll>;
using P = pair<int, int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007; // = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

// Mo's algorithum

int main() {
  int N, Q;
  scanf("%d %d", &N, &Q);
  vector<int> C(N);
  rep(i, N) scanf("%d", &C[i]);
  vector<int> L(Q), R(Q), idx(Q);
  rep(i, Q) {
    int l, r;
    scanf("%d %d", &l, &r);
    --l;
    L[i] = l;
    R[i] = r;
    idx[i] = i;
  }

  int sq = sqrt(N);
  sort(idx.begin(), idx.end(), [&](int a, int b) {
    return L[a] / sq == L[b] / sq ? R[a] < R[b] : L[a] < L[b];
  });

  vector<int> color(N + 1, 0);
  vector<int> ans(Q);
  int ret = 0;
  auto add = [&](int i) {
    color[C[i]]++;
    if (color[C[i]] == 1)
      ++ret;
  };
  auto del = [&](int i) {
    color[C[i]]--;
    if (color[C[i]] == 0)
      --ret;
  };

  int l = 0, r = 0; //[l,r)
  for (int i : idx) {
    while (l > L[i])
      --l, add(l);
    while (r < R[i])
      add(r), ++r;
    while (l < L[i])
      del(l), ++l;
    while (r > R[i])
      --r, del(r);
    ans[i] = ret;
  }
  rep(i, Q) printf("%d\\n", ans[i]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define irep(i, n) for (int i = (int)(n)-1; i >= 0; --i)
using namespace std;
using ll = long long;
using PL = pair<ll, ll>;
using P = pair<int, int>;
constexpr int INF = 1000000000;
constexpr long long HINF = 1000000000000000;
constexpr long long MOD = 1000000007; // = 998244353;
constexpr double EPS = 1e-4;
constexpr double PI = 3.14159265358979;

// Mo's algorithum

int main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> C(N);
  rep(i, N) cin >> C[i];
  vector<int> L(Q), R(Q), idx(Q);
  rep(i, Q) {
    int l, r;
    cin >> l >> r;
    --l;
    L[i] = l;
    R[i] = r;
    idx[i] = i;
  }

  int sq = sqrt(N);
  sort(idx.begin(), idx.end(), [&](int a, int b) {
    return L[a] / sq == L[b] / sq ? R[a] < R[b] : L[a] < L[b];
  });

  vector<int> color(N + 1, 0);
  vector<int> ans(Q);
  int ret = 0;
  auto add = [&](int i) {
    color[C[i]]++;
    if (color[C[i]] == 1)
      ++ret;
  };
  auto del = [&](int i) {
    color[C[i]]--;
    if (color[C[i]] == 0)
      --ret;
  };

  int l = 0, r = 0; //[l,r)
  for (int i : idx) {
    while (l > L[i])
      --l, add(l);
    while (r < R[i])
      add(r), ++r;
    while (l < L[i])
      del(l), ++l;
    while (r > R[i])
      --r, del(r);
    ans[i] = ret;
  }
  rep(i, Q) cout << ans[i] << '\\n';
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[500005];
int n, m, a[500005], ans[500005];
int st[25 * 100001], used[5000005];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define f first
#define s second

using namespace std;

pair<int, pair<int, int>> q[200001];
int n, m, a[100001], ans[200001];
int st[5 * 100001], used[1000001];
int cmp(pair<int, pair<int, int>> a, pair<int, pair<int, int>> b) {
  if (a.s.f != b.s.f)
    return a.s.f < b.s.f;
  return a.f < b.f;
}
void update(int v, int l, int r, int x, int d) {
  if (l == r) {
    st[v] = d;
    return;
  }
  int mid = (l + r) / 2;
  if (x <= mid)
    update(v * 2, l, mid, x, d);
  else
    update(v * 2 + 1, mid + 1, r, x, d);
  st[v] = st[v * 2] + st[v * 2 + 1];
}
int Find(int v, int A, int B, int l, int r) {
  if (l > B || r < A)
    return 0;
  if (A == l && B == r)
    return st[v];
  int mid = (A + B) / 2;
  return Find(v * 2, A, mid, l, min(mid, r)) +
         Find(v * 2 + 1, mid + 1, B, max(mid + 1, l), r);
}

int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);

  for (int i = 1; i <= m; i++)
    scanf("%d %d", &q[i].f, &q[i].s.f), q[i].s.s = i;
  sort(q + 1, q + m + 1, cmp);
  int j = 1;
  for (int i = 1; i <= m; i++) {
    while (j <= q[i].s.f) {
      if (!used[a[j]]) {
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      } else {
        update(1, 1, n, used[a[j]], 0);
        update(1, 1, n, j, 1);
        used[a[j]] = j;
      }
      j++;
    }
    ans[q[i].s.s] = Find(1, 1, n, q[i].f, q[i].s.f);
  }
  for (int i = 1; i <= m; i++)
    printf("%d\\n", ans[i]);

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

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 5;
int bit[N], p[N], ans[N], a[N];
pair<int, pair<int, int>> pr[N];

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

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

int main() {
  int n, q, x;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 0; i < q; ++i) {
    cin >> pr[i].second.first >> pr[i].first;
    pr[i].second.second = i;
  }
  sort(pr, pr + q);
  memset(p, -1, sizeof(p));
  memset(bit, 0, sizeof(bit));
  x = 0;
  for (int i = 1; i <= n; ++i) {
    if (p[a[i]] != -1)
      update(p[a[i]], -1);
    p[a[i]] = i;
    update(i, 1);
    while (x < q and pr[x].first == i) {
      ans[pr[x].second.second] =
          read(pr[x].first) - read(pr[x].second.first - 1);
      x++;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int bit[N], p[N], ans[N], a[N];
pair<int, pair<int, int>> pr[N];

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

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

int main() {
  int n, q, x;
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  for (int i = 0; i < q; ++i) {
    cin >> pr[i].second.first >> pr[i].first;
    pr[i].second.second = i;
  }
  sort(pr, pr + q);
  memset(p, -1, sizeof(p));
  memset(bit, 0, sizeof(bit));
  x = 0;
  for (int i = 1; i <= n; ++i) {
    if (p[a[i]] != -1)
      update(p[a[i]], -1);
    p[a[i]] = i;
    update(i, 1);
    while (x < q and pr[x].first == i) {
      ans[pr[x].second.second] =
          read(pr[x].first) - read(pr[x].second.first - 1);
      x++;
    }
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << "\\n";
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define int long long
using namespace std;
struct SegmentTree {
  int dat[10000000]{}, n = 1;
  SegmentTree(int N) {
    while (n < N)
      n *= 2;
  }
  void update(int x, int a) {
    x += n - 1;
    dat[x] += a;
    while (x > 0) {
      x = (x - 1) / 2;
      dat[x] += a;
    }
  }
  int query2(int a, int b, int x, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return dat[x];
    return query2(a, b, x * 2 + 1, l, (l + r) / 2) +
           query2(a, b, x * 2 + 2, (l + r) / 2, r);
  }
  int query(int a, int b) { return query2(a, b + 1, 0, 0, n); }
};
signed main() {
  int N, Q, A[1000000];
  cin >> N >> Q;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    A[i]--;
  }
  pair<pair<int, int>, int> B[1000000];
  for (int i = 0; i < Q; i++) {
    cin >> B[i].first.second >> B[i].first.first;
    B[i].first.first--, B[i].first.second--;
    B[i].second = i;
  }
  sort(B, B + Q);
  int C[1000000], D[1000000];
  for (int i = 0; i < N; i++)
    C[i] = -1;
  SegmentTree S(N);
  int n = 0;
  for (int i = 0; i < N; i++) {
    if (C[A[i]] != -1)
      S.update(C[A[i]], -1);
    S.update(i, 1);
    C[A[i]] = i;
    for (; n < Q && B[n].first.first == i; n++)
      D[B[n].second] = S.query(B[n].first.second, B[n].first.first);
  }
  for (int i = 0; i < Q; i++)
    cout << D[i] << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
struct SegmentTree {
  int dat[10000000]{}, n = 1;
  SegmentTree(int N) {
    while (n < N)
      n *= 2;
  }
  void update(int x, int a) {
    x += n - 1;
    dat[x] += a;
    while (x > 0) {
      x = (x - 1) / 2;
      dat[x] += a;
    }
  }
  int query2(int a, int b, int x, int l, int r) {
    if (r <= a || b <= l)
      return 0;
    if (a <= l && r <= b)
      return dat[x];
    return query2(a, b, x * 2 + 1, l, (l + r) / 2) +
           query2(a, b, x * 2 + 2, (l + r) / 2, r);
  }
  int query(int a, int b) { return query2(a, b + 1, 0, 0, n); }
};
signed main() {
  int N, Q;
  cin >> N >> Q;
  vector<int> A(N);
  for (int &i : A) {
    cin >> i;
    i--;
  }
  vector<pair<pair<int, int>, int>> B(Q);
  for (int i = 0; i < Q; i++) {
    cin >> B[i].first.second >> B[i].first.first;
    B[i].first.first--, B[i].first.second--;
    B[i].second = i;
  }
  sort(B.begin(), B.end());
  vector<int> C(N, -1), D(N);
  SegmentTree S(N);
  int n = 0;
  for (int i = 0; i < N; i++) {
    if (C[A[i]] != -1)
      S.update(C[A[i]], -1);
    S.update(i, 1);
    C[A[i]] = i;
    for (; n < Q && B[n].first.first == i; n++)
      D[B[n].second] = S.query(B[n].first.second, B[n].first.first);
  }
  for (int i = 0; i < Q; i++)
    cout << D[i] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
const int maxn = 5e5 + 1;
const int maxq = 5e5 + 1;

struct TQuery {
  int l, r, id;
  inline bool operator<(const TQuery &other) { return r < other.r; }
};

int n, m, a[maxn], bit[maxn], res[maxq];
TQuery q[maxq];
map<int, int> pre; // vi tri xuat hien truoc do cua a[i]

inline void update(int x, const int &val) {
  for (; x <= n; x += x & -x)
    bit[x] += val;
}

inline int get(int x) {
  int sum = 0;
  for (; x; x -= x & -x)
    sum += bit[x];
  return sum;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= m; i++)
    cin >> q[i].l >> q[i].r, q[i].id = i;
  sort(q + 1, q + m + 1);
  for (int i = 1, x = 1; i <= m; i++) {
    for (; x <= q[i].r; x++) {
      update(x, 1);
      int cur = a[x]; // tang vi tri x len 1
      if (pre[cur])
        update(
            pre[cur],
            get(pre[cur] - 1) -
                get(pre[cur])); // neu a[x] da xuat hien thi xoa cho truoc do di
      pre[cur] = x;             // cap nhat lai pre
    }
    res[q[i].id] = get(q[i].r) - get(q[i].l - 1); // lay tong tu l -> r
  }
  for (int i = 1; i <= m; i++)
    cout << res[i] << '\\n';
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
const int maxn = 3e4 + 1;
const int maxq = 2e5 + 1;

struct TQuery {
  int l, r, id;
  inline bool operator<(const TQuery &other) { return r < other.r; }
};

int n, m, a[maxn], bit[maxn], res[maxq];
TQuery q[maxq];
map<int, int> pre; // vi tri xuat hien truoc do cua a[i]

inline void update(int x, const int &val) {
  for (; x <= n; x += x & -x)
    bit[x] += val;
}

inline int get(int x) {
  int sum = 0;
  for (; x; x -= x & -x)
    sum += bit[x];
  return sum;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  cin >> m;
  for (int i = 1; i <= m; i++)
    cin >> q[i].l >> q[i].r, q[i].id = i;
  sort(q + 1, q + m + 1);
  for (int i = 1, x = 1; i <= m; i++) {
    for (; x <= q[i].r; x++) {
      update(x, 1);
      int cur = a[x]; // tang vi tri x len 1
      if (pre[cur])
        update(
            pre[cur],
            get(pre[cur] - 1) -
                get(pre[cur])); // neu a[x] da xuat hien thi xoa cho truoc do di
      pre[cur] = x;             // cap nhat lai pre
    }
    res[q[i].id] = get(q[i].r) - get(q[i].l - 1); // lay tong tu l -> r
  }
  for (int i = 1; i <= m; i++)
    cout << res[i] << '\\n';
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>

using namespace std;

struct req {
  int l, r, val, idx;
  bool t;
};

bool operator<(req a, req b) {
  if (a.r == b.r) {
    return a.t < b.t;
  }
  return a.r < b.r;
}

int bit[500001];

void add(int y, int val) {
  while (y <= 500000) {
    bit[y] += val;
    y += (y & -y);
  }
}

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

bool visited[2000001];
req arr[1000005];
int main() {
  int N, Q, i;
  int *prev = new int[2000001];
  for (i = 0; i <= 2000000; ++i) {
    visited[i] = false;
    prev[i] = 0;
  }
  scanf("%d", &N);
  scanf("%d", &Q);
  for (i = 1; i <= N; ++i) {
    add(i, 1);
  }
  for (i = 0; i < N; ++i) {
    scanf("%d", &arr[i].val);
    arr[i].idx = i + 1;
    arr[i].t = 0;
    arr[i].r = i + 1;
  }
  for (i = N; i < N + Q; ++i) {
    scanf("%d%d", &arr[i].l, &arr[i].r);
    arr[i].idx = i - N;
    arr[i].t = 1;
  }
  sort(arr, arr + N + Q);
  int ans[Q];
  for (i = 0; i < N + Q; ++i) {
    if (arr[i].t) {
      ans[arr[i].idx] = sum(arr[i].r) - sum(arr[i].l - 1);
    } else {
      if (visited[arr[i].val]) {
        if (prev[arr[i].val] != 0) {
          add(prev[arr[i].val], -1);
        }
      }
      visited[arr[i].val] = true;
      prev[arr[i].val] = arr[i].idx;
    }
  }
  for (i = 0; i < Q; ++i) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>

using namespace std;

struct req {
  int l, r, val, idx;
  bool t;
};

bool operator<(req a, req b) {
  if (a.r == b.r) {
    return a.t < b.t;
  }
  return a.r < b.r;
}

int bit[500001];

void add(int y, int val) {
  while (y <= 500000) {
    bit[y] += val;
    y += (y & -y);
  }
}

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

bool visited[2000001];
req arr[600000];
int main() {
  int N, Q, i;
  int *prev = new int[2000001];
  for (i = 0; i <= 2000000; ++i) {
    visited[i] = false;
    prev[i] = 0;
  }
  scanf("%d", &N);
  scanf("%d", &Q);
  for (i = 1; i <= N; ++i) {
    add(i, 1);
  }
  for (i = 0; i < N; ++i) {
    scanf("%d", &arr[i].val);
    arr[i].idx = i + 1;
    arr[i].t = 0;
    arr[i].r = i + 1;
  }
  for (i = N; i < N + Q; ++i) {
    scanf("%d%d", &arr[i].l, &arr[i].r);
    arr[i].idx = i - N;
    arr[i].t = 1;
  }
  sort(arr, arr + N + Q);
  int ans[Q];
  for (i = 0; i < N + Q; ++i) {
    if (arr[i].t) {
      ans[arr[i].idx] = sum(arr[i].r) - sum(arr[i].l - 1);
    } else {
      if (visited[arr[i].val]) {
        if (prev[arr[i].val] != 0) {
          add(prev[arr[i].val], -1);
        }
      }
      visited[arr[i].val] = true;
      prev[arr[i].val] = arr[i].idx;
    }
  }
  for (i = 0; i < Q; ++i) {
    printf("%d\\n", ans[i]);
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;
  VI c(n);
  rep(i, n) cin >> c[i];
  vector<pair<int, int>> lr(q);
  rep(i, q) cin >> lr[i].first >> lr[i].second;
  rep(i, q) lr[i].first--;

  const int sq = 700;
  vector<tuple<int, int, int>> lri(q);
  rep(i, q) lri[i] = make_tuple(lr[i].first / sq, lr[i].second, i);
  sort(lri.begin(), lri.end());

  VI ans(q);

  VI cnt(n + 1);
  int prev = -1;
  int lx, rx;
  lx = rx = get<0>(lri[0]);
  int sum = 0;
  VI changed;
  for (auto [ls, r, i] : lri) {

    // ここのコメントを外すとhandmade03-05, random06がWAになる

    // if (ls != prev) {
    //   for (auto cx : changed) cnt[cx] = 0;
    //   changed.clear();
    //   lx = rx = r;
    //   prev = ls;
    // }

    int l = lr[i].first;
    assert(r == lr[i].second); // assert(rx <= r);
    int cx;
    while (lx > l)
      if (cnt[cx = c[--lx]]++ == 0)
        sum += 1; //, changed.push_back(cx);
    while (lx < l)
      if (--cnt[c[lx++]] == 0)
        sum -= 1;
    while (rx > r)
      if (--cnt[c[--rx]] == 0)
        sum -= 1;
    while (rx < r)
      if (cnt[cx = c[rx++]]++ == 0)
        sum += 1; //, changed.push_back(cx);
    ans[i] = sum;
  }
  rep(i, q) cout << ans[i] << "\\n";
}


Error: -6
9e68d89d-1efb-48cc-b5c5-69ce9c713c1b.out: /home/alex/Documents/bug-detection/input/Project_CodeNet/data/p02599/C++/s459893746.cpp:49: int main(): Assertion `cnt[c[lx-1]] >= 1' failed.


Fixed submission:

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

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);

  int n, q;
  cin >> n >> q;
  VI c(n);
  rep(i, n) cin >> c[i];
  vector<pair<int, int>> lr(q);
  rep(i, q) cin >> lr[i].first >> lr[i].second;
  rep(i, q) lr[i].first--;

  const int sq = 700;
  vector<tuple<int, int, int>> lri(q);
  rep(i, q) lri[i] = make_tuple(lr[i].first / sq, lr[i].second, i);
  sort(lri.begin(), lri.end());

  VI ans(q);

  VI cnt(n + 1);
  int prev = -1;
  int lx, rx;
  lx = rx = get<0>(lri[0]);
  int sum = 0;
  VI changed;
  for (auto [ls, r, i] : lri) {

    // ここのコメントを外すとhandmade03-05, random06がWAになる

    if (ls != prev) {
      for (auto cx : changed)
        cnt[cx] = 0;
      changed.clear();
      lx = rx = r;
      prev = ls;
    }

    int l = lr[i].first;
    assert(r == lr[i].second);
    assert(rx <= r);
    int cx;
    while (rx < r) {
      assert(cnt[c[rx]] >= 0);
      if (cnt[cx = c[rx++]]++ == 0)
        sum += 1, changed.push_back(cx);
    }
    while (lx > l) {
      assert(cnt[c[lx - 1]] >= 1);
      if (cnt[cx = c[--lx]]++ == 0)
        sum += 1, changed.push_back(cx);
    }
    while (lx < l) {
      assert(cnt[c[lx]] >= 0);
      if (--cnt[c[lx++]] == 0)
        sum -= 1;
    }
    ans[i] = sum;
  }
  rep(i, q) cout << ans[i] << "\\n";
}