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

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

const int N = 500000;

int pre[N + 5];

template <int M, int SZ> struct Persistable_Segment_Tree {
  int tot, p, k, L, R;
  int rt[M + 5], lc[SZ + 5], rc[SZ + 5], cntv[SZ + 5];
  void init() { tot = rt[0] = lc[0] = rc[0] = cntv[0] = 0; }
  int update(int pre, int l, int r, int k) {
    int now = ++tot;
    if (l == r) {
      cntv[now] = cntv[pre] + k;
    } else {
      int m = (l + r) >> 1;
      if (p <= m) {
        lc[now] = update(lc[pre], l, m, k);
        rc[now] = rc[pre];
      } else {
        rc[now] = update(rc[pre], m + 1, r, k);
        lc[now] = lc[pre];
      }
      cntv[now] = cntv[lc[now]] + cntv[rc[now]];
    }
    return now;
  }
  void query(int o, int l, int r) {
    if (L <= l && r <= R) {
      k += cntv[o];
    } else {
      int m = (l + r) >> 1;
      if (L <= m)
        query(lc[o], l, m);
      if (R > m)
        query(rc[o], m + 1, r);
    }
  }
};

Persistable_Segment_Tree<N, N * 40> psegt;

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    int rt = psegt.rt[i - 1];
    if (pre[x]) {
      psegt.p = pre[x];
      rt = psegt.update(rt, 1, n, -1);
    }
    pre[x] = i;
    psegt.p = i;
    rt = psegt.update(rt, 1, n, 1);
    psegt.rt[i] = rt;
  }
  for (int i = 1; i <= q; ++i) {
    int l, r;
    scanf("%d %d", &l, &r);
    psegt.L = l;
    psegt.R = r;
    psegt.k = 0;
    psegt.query(psegt.rt[r], 1, n);
    printf("%d\\n", psegt.k);
  }
  return 0;
}

Error: -11

Fixed submission:

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

const int N = 500000;

int pre[N + 5];

template <int M, int SZ> struct Persistable_Segment_Tree {
  int tot, p, k, L, R;
  int rt[M + 5], lc[SZ + 5], rc[SZ + 5], cntv[SZ + 5];
  void init() { tot = rt[0] = lc[0] = rc[0] = cntv[0] = 0; }
  int update(int pre, int l, int r, int k) {
    int now = ++tot;
    if (l == r) {
      cntv[now] = cntv[pre] + k;
    } else {
      int m = (l + r) >> 1;
      if (p <= m) {
        lc[now] = update(lc[pre], l, m, k);
        rc[now] = rc[pre];
      } else {
        rc[now] = update(rc[pre], m + 1, r, k);
        lc[now] = lc[pre];
      }
      cntv[now] = cntv[lc[now]] + cntv[rc[now]];
    }
    return now;
  }
  void query(int o, int l, int r) {
    if (L <= l && r <= R) {
      k += cntv[o];
    } else {
      int m = (l + r) >> 1;
      if (L <= m)
        query(lc[o], l, m);
      if (R > m)
        query(rc[o], m + 1, r);
    }
  }
};

Persistable_Segment_Tree<N, N * 20> psegt;

int main() {
  int n, q;
  scanf("%d %d", &n, &q);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    int rt = psegt.rt[i - 1];
    if (pre[x]) {
      psegt.p = pre[x];
      rt = psegt.update(rt, 1, n, -1);
    }
    pre[x] = i;
    psegt.p = i;
    rt = psegt.update(rt, 1, n, 1);
    psegt.rt[i] = rt;
  }
  for (int i = 1; i <= q; ++i) {
    int l, r;
    scanf("%d %d", &l, &r);
    psegt.L = l;
    psegt.R = r;
    psegt.k = 0;
    psegt.query(psegt.rt[r], 1, n);
    printf("%d\\n", psegt.k);
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int maxn = 6e5 + 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 * 40];
int main() {
  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);
  }
}

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 * 10];
int main() {
  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);
  }
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
int n, m;
struct Tree {
  int ls, rs;
  ll val; // 左右儿子的编号, 和维护的一个值.
} tre[maxn * 100];
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, ll 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;
}
ll 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);
    ll tmp = query(root[r], 1, n, l);
    printf("%lld\\n", tmp);
  }
}
int main() {
  solve();
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e5 + 5;
int n, m;
struct Tree {
  int ls, rs;
  ll val; // 左右儿子的编号, 和维护的一个值.
} tre[maxn * 20];
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, ll 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;
}
ll 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);
    ll tmp = query(root[r], 1, n, l);
    printf("%lld\\n", tmp);
  }
}
int main() {
  solve();
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

int n;

struct query {
  int L, R, I;
};

int block;

bool cmp(query a, query b) {
  if (a.L / block != b.L / block)
    return a.L / block < b.L / block;
  else
    return a.R < b.R;
}

int main() {
  ios_base::sync_with_stdio(false);
  int m;
  scanf("%i", &n);
  scanf("%i", &m);
  block = sqrt(n);
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    scanf("%i", &a[i]);
  vector<query> kveriz(m);
  vector<int> odg(m);
  for (int i = 0; i < m; i++) {
    scanf("%i", &kveriz[i].L);
    scanf("%i", &kveriz[i].R);
    kveriz[i].L--;
    kveriz[i].R--;
    kveriz[i].I = i;
  }
  sort(kveriz.begin(), kveriz.end(), cmp);
  int curl = 0, curr = -1, bdif = 0;
  vector<int> bb(500001, 0);
  for (int i = 0; i < m; i++) {
    int l = kveriz[i].L, r = kveriz[i].R;
    while (curl < l) {
      bb[a[curl]]--;
      if (!bb[a[curl]])
        bdif--;
      curl++;
    }
    while (curr < r) {
      bb[a[curr + 1]]++;
      if (bb[a[curr + 1]] == 1)
        bdif++;
      curr++;
    }
    while (curr > r) {
      bb[a[curr]]--;
      if (!bb[a[curr]])
        bdif--;
      curr--;
    }
    while (curl > l) {
      bb[a[curl - 1]]++;
      if (bb[a[curl - 1]] == 1)
        bdif++;
      curl--;
    }
    odg[kveriz[i].I] = bdif;
  }
  for (int i = 0; i < m; i++) {
    printf("%i \\n", odg[i]);
  }
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

long long n;

struct query {
  long long L, R, I;
};

long long block;

bool cmp(query a, query b) {
  if (a.L / block != b.L / block)
    return a.L / block < b.L / block;
  else
    return a.R < b.R;
}

int main() {
  ios_base::sync_with_stdio(false);
  long long m;
  scanf("%d", &n);
  scanf("%d", &m);
  block = sqrt(n);
  vector<long long> a(n);
  for (long long i = 0; i < n; i++)
    scanf("%d", &a[i]);
  vector<query> kveriz(m);
  vector<long long> odg(m);
  for (long long i = 0; i < m; i++) {
    scanf("%d", &kveriz[i].L);
    scanf("%d", &kveriz[i].R);
    kveriz[i].L--;
    kveriz[i].R--;
    kveriz[i].I = i;
  }
  sort(kveriz.begin(), kveriz.end(), cmp);
  long long curl = 0, curr = -1, bdif = 0;
  vector<long long> bb(500001, 0);
  for (long long i = 0; i < m; i++) {
    long long l = kveriz[i].L, r = kveriz[i].R;
    while (curl < l) {
      bb[a[curl]]--;
      if (!bb[a[curl]])
        bdif--;
      curl++;
    }
    while (curr < r) {
      bb[a[curr + 1]]++;
      if (bb[a[curr + 1]] == 1)
        bdif++;
      curr++;
    }
    while (curr > r) {
      bb[a[curr]]--;
      if (!bb[a[curr]])
        bdif--;
      curr--;
    }
    while (curl > l) {
      bb[a[curl - 1]]++;
      if (bb[a[curl - 1]] == 1)
        bdif++;
      curl--;
    }
    odg[kveriz[i].I] = bdif;
  }
  for (long long i = 0; i < m; i++) {
    printf("%d \\n", odg[i]);
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
////////////////////////////////////////////////////////////////////
void update(vector<int> &tree, int k, int val) {
  ++k;
  while (k < tree.size()) {
    tree[k] += val;
    k = (k + (k & (-k)));
  }
}
int prefix(vector<int> &tree, int i) {
  if (i == -1)
    return 0;
  ++i;
  int sum = 0;
  while (i > 0) {
    sum += tree[i];
    i = i ^ (i & (-i));
  }
  return sum;
}

//////////////////////////////////////////////////////////////

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    v[i]--;
  }

  vector<pair<int, int>> query(k); // query
  vector<vector<int>> bucket(n);
  vector<int> lastindex(n, -1); // right most index

  for (int i = 0; i < k; i++) {
    cin >> query[i].first >> query[i].second;
    query[i].first -= 1;
    query[i].second -= 1;
    bucket[query[i].first].push_back(i);
  }

  vector<int> result(k);   // result vector
  vector<int> tree(n + 1); // tree
  for (int i = n - 1; i >= 0; i--) {
    int tmp = lastindex[v[i]];
    lastindex[v[i]] = i;
    update(tree, i, 1);
    if (tmp != -1)
      update(tree, tmp, -1);
    for (int j = 0; j < bucket[i].size(); j++) {
      int index = bucket[i][j];
      result[index] = prefix(tree, query[index].second) -
                      prefix(tree, query[index].first - 1);
    }
  }
  for (int i = 0; i < k; i++) {
    cout << result[i] << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

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

void update(vector<int> &tree, int k, int val) {
  ++k;
  while (k < tree.size()) {
    tree[k] += val;
    k = (k + (k & (-k)));
  }
}
vector<int> build(vector<int> &v) {
  int n = v.size();
  vector<int> tree(n + 1);
  for (int i = 1; i <= n; i++)
    update(tree, i - 1, v[i - 1]);
  return tree;
}
int prefix(vector<int> &tree, int i) {
  if (i == -1)
    return 0;
  ++i;
  int sum = 0;
  while (i > 0) {
    sum += tree[i];
    i = i ^ (i & (-i));
  }
  return sum;
}

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
    v[i]--;
  }

  vector<pair<int, int>> query(k); // query
  vector<vector<int>> bucket(n);
  vector<int> lastindex(n, -1); // right most index
  vector<int> freq(n);          // freq array to be built on

  for (int i = 0; i < k; i++) {
    cin >> query[i].first >> query[i].second;
    query[i].first -= 1;
    query[i].second -= 1;
    bucket[query[i].first].push_back(i);
  }

  vector<int> result(k); // result vector

  for (int i = n - 1; i >= 0; i--) {
    int tmp = lastindex[v[i]];
    lastindex[v[i]] = i;
    freq[i] = 1;
    if (tmp != -1)
      freq[tmp] = 0;
    auto tree = build(freq);
    for (int j = 0; j < bucket[i].size(); j++) {
      int index = bucket[i][j];
      result[index] = prefix(tree, query[index].second) -
                      prefix(tree, query[index].first - 1);
    }
  }
  for (int i = 0; i < k; i++) {
    cout << result[i] << endl;
  }
  return 0;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
vector<int> BIT;
void init(int N) {
  for (int i = 0; i <= N; i++)
    BIT[i] = 0;
}
int sumUp(int a) {
  int ret = 0;
  while (a) {
    ret += BIT[a];
    a -= (a & -a);
  }
  return ret;
}
void add(int a, int n, int N) {
  if (a == 0)
    return;
  while (a <= N) {
    BIT[a] += n;
    a += (a & -a);
  }
}
int main() {
  int N;
  cin >> N;
  int Q;
  cin >> Q;
  BIT.resize(N + 5);
  init(N);
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  vector<tuple<int, int, int>> query(Q + 1);
  for (int i = 1; i <= Q; i++) {
    int l, r;
    cin >> l >> r;
    query[i] = make_tuple(r, l, i);
  }
  query[0] = make_tuple(0, 0, 0);
  sort(query.begin(), query.end());
  vector<int> color(N + 1, 0);
  vector<int> ans(Q + 1);
  for (int i = 1; i <= Q; i++) {
    int rp = get<0>(query[i - 1]);
    int l, r, j;
    tie(r, l, j) = query[i];
    for (int i = rp + 1; i <= r; i++) {
      int t = color[A[i]];
      color[A[i]] = i;
      add(t, -1, N);
      add(i, 1, N);
    }
    ans[j] = sumUp(r) - sumUp(l - 1);
    // cout << i << endl;
  }
  for (int i = 1; i <= Q; i++)
    cout << ans[i] << endl;
  // add(0, 1, N);
  // cout << sumUp(0);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
vector<int> BIT;
void init(int N) {
  for (int i = 0; i <= N; i++)
    BIT[i] = 0;
}
int sumUp(int a) {
  int ret = 0;
  while (a) {
    ret += BIT[a];
    a -= (a & -a);
  }
  return ret;
}
void add(int a, int n, int N) {
  while (a <= N) {
    BIT[a] += n;
    a += (a & -a);
  }
}
int main() {
  int N;
  cin >> N;
  int Q;
  cin >> Q;
  BIT.resize(N + 5);
  init(N);
  vector<int> A(N + 1);
  for (int i = 1; i <= N; i++)
    cin >> A[i];
  vector<tuple<int, int, int>> query(Q + 1);
  for (int i = 1; i <= Q; i++) {
    int l, r;
    cin >> l >> r;
    query[i] = make_tuple(r, l, i);
  }
  query[0] = make_tuple(0, 0, 0);
  sort(query.begin(), query.end());
  vector<int> color(N + 1, 0);
  vector<int> ans(Q + 1);
  for (int i = 1; i <= Q; i++) {
    int rp = get<0>(query[i - 1]);
    int l, r, j;
    tie(r, l, j) = query[i];
    for (int i = rp + 1; i <= r; i++) {
      int t = color[A[i]];
      color[A[i]] = i;
      add(t, -1, N);
      add(i, 1, N);
    }
    ans[j] = sumUp(r) - sumUp(l - 1);
  }
  for (int i = 1; i <= Q; i++)
    cout << ans[i] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

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

int val;
bool comp(vector<int> &v1, vector<int> &v2) {
  int l1 = v1[0] / val;
  int l2 = v2[0] / val;
  if (l1 != l2)
    return (l1 < l2);
  return v1[1] < v2[1];
}

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

  vector<vector<int>> query(q, vector<int>(3));
  vector<int> sol(q);

  for (int i = 0; i < q; i++) {
    scanf("%d %d", &query[i][0], &query[i][1]);
    query[i][2] = i;
  }
  val = sqrt(n);
  sort(query.begin(), query.end(), comp);

  int l = 1, r = 0, diff = 0;
  vector<int> fr(1000001, 0);
  for (const auto &x : query) {
    while (r > x[1]) {
      fr[arr[r]]--;

      if (fr[arr[r]] == 0)
        diff--;

      --r;
    }
    while (l < x[0]) {
      fr[arr[l]]--;

      if (fr[arr[l]] == 0)
        diff--;

      l++;
    }
    while (r < x[1]) {
      ++r;
      fr[arr[r]]++;

      if (fr[arr[r]] == 1)
        diff++;
    }
    while (l > x[0]) {
      l--;

      fr[arr[l]]++;

      if (fr[arr[l]] == 1)
        diff++;
    }
    sol[x[2]] = diff;
  }
  for (int x : sol)
    printf("%d\\n", x);
}

Error: 0

Fixed submission:

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

int val;
bool comp(vector<int> &v1, vector<int> &v2) {
  int l1 = v1[0] / val;
  int l2 = v2[0] / val;
  if (l1 != l2)
    return (l1 < l2);
  return v1[1] < v2[1];
}

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

  vector<vector<int>> query(q, vector<int>(3));
  vector<int> sol(q);

  for (int i = 0; i < q; i++) {
    scanf("%d %d", &query[i][0], &query[i][1]);
    query[i][2] = i;
  }
  val = sqrt(n);
  sort(query.begin(), query.end(), comp);

  int l = 1, r = 0, diff = 0;
  vector<int> fr(1000001, 0);
  for (const auto &x : query) {
    while (r > x[1]) {
      fr[arr[r]]--;

      if (fr[arr[r]] == 0)
        diff--;

      --r;
    }
    while (l < x[0]) {
      fr[arr[l]]--;

      if (fr[arr[l]] == 0)
        diff--;

      l++;
    }
    while (r < x[1]) {
      ++r;
      fr[arr[r]]++;

      if (fr[arr[r]] == 1)
        diff++;
    }
    while (l > x[0]) {
      l--;

      fr[arr[l]]++;

      if (fr[arr[l]] == 1)
        diff++;
    }
    sol[x[2]] = diff;
  }
  for (int x : sol)
    printf("%d\\n", x);
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lc(x) pt[x].lc
#define rc(x) pt[x].rc
#define sum(x) pt[x].sum
using namespace std;
const int MAXN = 500010;
int n, m, tmp;
int a[MAXN];
map<int, int> pos;
struct PresidentTree {
  int lc, rc, sum;
} pt[MAXN * 100];
int num, rt[MAXN];
void build(int &k1, int l, int r) {
  k1 = ++num;
  sum(k1) = 0;
  if (l == r)
    return;
  build(lc(k1), l, mid);
  build(rc(k1), mid + 1, r);
}
void modify(int &k1, int pre, int l, int r, int p, int v) {
  k1 = ++num;
  pt[k1] = pt[pre];
  sum(k1) = sum(pre) + v;
  if (l == r)
    return;
  if (p <= mid)
    modify(lc(k1), lc(pre), l, mid, p, v);
  else
    modify(rc(k1), rc(pre), mid + 1, r, p, v);
}
int query(int k1, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr)
    return sum(k1);
  int ans = 0;
  if (ql <= mid)
    ans += query(lc(k1), l, mid, ql, qr);
  if (qr > mid)
    ans += query(rc(k1), mid + 1, r, ql, qr);
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  num = 0;
  pos.clear();
  build(rt[0], 1, n);
  for (int i = 1; i <= n; i++) {
    if (pos[a[i]]) {
      modify(tmp, rt[i - 1], 1, n, pos[a[i]], -1);
      modify(rt[i], tmp, 1, n, i, 1);
    } else {
      modify(rt[i], rt[i - 1], 1, n, i, 1);
    }
    pos[a[i]] = i;
  }
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    tmp = query(rt[r], 1, n, l, r);
    printf("%d\\n", tmp);
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define lc(x) pt[x].lc
#define rc(x) pt[x].rc
#define sum(x) pt[x].sum
using namespace std;
const int MAXN = 500010;
int n, m, tmp;
int a[MAXN], f[MAXN][20][2];
map<int, int> pos;
struct PresidentTree {
  int lc, rc, sum;
} pt[MAXN * 30];
int num, rt[MAXN];
void build(int &k1, int l, int r) {
  k1 = ++num;
  sum(k1) = 0;
  if (l == r)
    return;
  build(lc(k1), l, mid);
  build(rc(k1), mid + 1, r);
}
void modify(int &k1, int pre, int l, int r, int p, int v) {
  k1 = ++num;
  pt[k1] = pt[pre];
  sum(k1) = sum(pre) + v;
  if (l == r)
    return;
  if (p <= mid)
    modify(lc(k1), lc(pre), l, mid, p, v);
  else
    modify(rc(k1), rc(pre), mid + 1, r, p, v);
}
int query(int k1, int l, int r, int ql, int qr) {
  if (ql <= l && r <= qr)
    return sum(k1);
  int ans = 0;
  if (ql <= mid)
    ans += query(lc(k1), l, mid, ql, qr);
  if (qr > mid)
    ans += query(rc(k1), mid + 1, r, ql, qr);
  return ans;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  num = 0;
  pos.clear();
  build(rt[0], 1, n);
  for (int i = 1; i <= n; i++) {
    if (pos[a[i]]) {
      modify(tmp, rt[i - 1], 1, n, pos[a[i]], -1);
      modify(rt[i], tmp, 1, n, i, 1);
    } else {
      modify(rt[i], rt[i - 1], 1, n, i, 1);
    }
    pos[a[i]] = i;
  }
  while (m--) {
    int l, r;
    scanf("%d%d", &l, &r);
    tmp = query(rt[r], 1, n, l, r);
    printf("%d\\n", tmp);
  }
}