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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define N 100005
ll block = 1300;
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
struct Query {
  ll l;
  ll r;
  ll i;
};
Query Q[500005];
ll ar[500005];
ll check[500005];
ll ans[500005];
ll cnt = 0;
bool comp(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(ll pos) {
  check[ar[pos]]++;
  if (check[ar[pos]] == 1)
    cnt++;
}
void remove(ll pos) {
  check[ar[pos]]--;
  if (check[ar[pos]] == 0)
    cnt--;
}
int main() {
  fastio();
  ll n, x, y, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> ar[i];

  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    Q[i].i = i;
    Q[i].l = --x;
    Q[i].r = --y;
  }

  sort(Q, Q + m, comp);
  ll cur_l = 0;
  ll cur_r = -1;
  for (int i = 0; i < m; i++) {
    ll L = Q[i].l;
    ll R = Q[i].r;
    while (cur_l > L)
      add(cur_l - 1), cur_l--;

    while (cur_r < R)
      add(cur_r + 1), cur_r++;

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

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

    ans[Q[i].i] = cnt;
  }
  for (int i = 0; i < m; i++)
    cout << ans[i] << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define N 100005
void fastio() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
struct Query {
  ll l;
  ll r;
  ll i;
};
Query Q[500005];
ll ar[500005], block;
ll check[500005];
ll ans[500005];
ll cnt = 0;
bool comp(Query a, Query b) {
  if (a.l / block != b.l / block)
    return a.l / block < b.l / block;
  return a.r < b.r;
}
void add(ll pos) {
  check[ar[pos]]++;
  if (check[ar[pos]] == 1)
    cnt++;
}
void remove(ll pos) {
  check[ar[pos]]--;
  if (check[ar[pos]] == 0)
    cnt--;
}
int main() {
  fastio();
  ll n, x, y, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> ar[i];

  for (int i = 0; i < m; i++) {
    cin >> x >> y;
    Q[i].i = i;
    Q[i].l = --x;
    Q[i].r = --y;
  }
  block = (int)sqrt(n);
  sort(Q, Q + m, comp);
  ll cur_l = 0;
  ll cur_r = -1;
  for (int i = 0; i < m; i++) {
    ll L = Q[i].l;
    ll R = Q[i].r;
    while (cur_l > L)
      add(cur_l - 1), cur_l--;

    while (cur_r < R)
      add(cur_r + 1), cur_r++;

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

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

    ans[Q[i].i] = cnt;
  }
  for (int i = 0; i < m; 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 nmax = 5e5 + 3;
int n, q, usu[nmax], sol, v[nmax];
struct Query {
  int l, r, idx;

  inline pair<int, int> toPair() const {

    int ceva;
    if ((int)(l / sqrt(n)) % 2)
      ceva = -r;
    else
      ceva = r;
    return make_pair(l / sqrt(n), ceva);
  }
} t[nmax];

int ap[nmax];

inline bool operator<(const Query &a, const Query &b) {
  return a.toPair() < b.toPair();
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> v[i];
  for (int i = 1; i <= q; ++i) {
    cin >> t[i].l >> t[i].r;
    t[i].idx = i;
  }
  sort(t + 1, t + q + 1);

  int st = 0;
  int dr = 0;

  for (int i = 1; i <= q; ++i) {
    // cout << t[i].l << ' ' << t[i].r << '\\n';

    if (t[i].l > dr || t[i].r < st) {
      for (int j = st; j <= dr; ++j)
        ap[v[j]] = 0;
      st = t[i].l;
      dr = st;
      sol = 1;
      ++ap[v[st]];
    }

    while (dr < t[i].r) {
      ap[v[++dr]]++;
      if (ap[v[dr]] == 1)
        ++sol;
    }

    while (st > t[i].l) {
      ap[v[--st]]++;
      if (ap[v[st]] == 1)
        ++sol;
    }

    while (st < t[i].l) {
      ap[v[st++]]--;
      if (ap[v[st - 1]] == 0)
        --sol;
    }

    while (dr > t[i].r) {
      ap[v[dr--]]--;
      if (ap[v[dr + 1]] == 0)
        --sol;
    }

    usu[t[i].idx] = sol;
  }

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


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int nmax = 5e5 + 3;
int n, q, usu[nmax], sol, v[nmax];
struct Query {
  int l, r, idx;

  inline pair<int, int> toPair() const { return make_pair(l / sqrt(n), r); }
} t[nmax];

int ap[nmax];

inline bool operator<(const Query &a, const Query &b) {
  return a.toPair() < b.toPair();
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> q;
  for (int i = 1; i <= n; ++i)
    cin >> v[i];
  for (int i = 1; i <= q; ++i) {
    cin >> t[i].l >> t[i].r;
    t[i].idx = i;
  }
  sort(t + 1, t + q + 1);

  int st = 0;
  int dr = 0;

  for (int i = 1; i <= q; ++i) {
    // cout << t[i].l << ' ' << t[i].r << '\\n';

    if (t[i].l > dr || t[i].r < st) {
      for (int j = st; j <= dr; ++j)
        ap[v[j]] = 0;
      st = t[i].l;
      dr = st;
      sol = 1;
      ++ap[v[st]];
    }

    while (dr < t[i].r) {
      ap[v[++dr]]++;
      if (ap[v[dr]] == 1)
        ++sol;
    }

    while (st > t[i].l) {
      ap[v[--st]]++;
      if (ap[v[st]] == 1)
        ++sol;
    }

    while (st < t[i].l) {
      ap[v[st++]]--;
      if (ap[v[st - 1]] == 0)
        --sol;
    }

    while (dr > t[i].r) {
      ap[v[dr--]]--;
      if (ap[v[dr + 1]] == 0)
        --sol;
    }

    usu[t[i].idx] = sol;
  }

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

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define ll long long
#define mod 1000000007

const int sz = 5e5 + 5;
ll inf = 1e15;

ll ar[sz], vis[sz], nxt[sz];
vector<int> tree[4 * sz];

void build(int st, int en, int no) {
  if (st == en) {
    tree[no].push_back(nxt[st]);
    return;
  }
  int mid = (st + en) / 2;
  build(st, mid, 2 * no + 1);
  build(mid + 1, en, 2 * no + 2);
  merge(tree[2 * no + 1].begin(), tree[2 * no + 1].end(),
        tree[2 * no + 2].begin(), tree[2 * no + 2].end(),
        back_inserter(tree[no]));
}

int query(int st, int en, int no, int l, int r, int k) {
  if (l > r or st > en or en < l or st > r)
    return 0;
  if (l <= st and r >= en) {
    return tree[no].size() - (upper_bound(tree[no].begin(), tree[no].end(), k) -
                              tree[no].begin());
  }
  int mid = (st + en) / 2;
  return query(st, mid, 2 * no + 1, l, r, k) +
         query(mid + 1, en, 2 * no + 2, l, r, k);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    ll n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
      cin >> ar[i];
      nxt[i] = n + 1;
    }
    for (int i = n; i >= 1; i--) {
      if (vis[ar[i]] != 0) {
        nxt[i] = vis[ar[i]];
      }
      vis[ar[i]] = i;

      // cout<<nxt[i]<<" ";
    }

    build(1, n, 0);

    while (q--) {
      int l, r;
      cin >> l >> r;
      cout << query(1, n, 0, l, r, r) << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl "\\n"
#define ll long long
#define mod 1000000007

const int sz = 5e5 + 5;
ll inf = 1e15;

vector<set<ll>> tree(4 * sz);

ll ar[sz];

void build(int st, int en, int no) {
  if (st == en) {
    tree[no].insert(ar[st]);
    return;
  }
  ll mid = st + (en - st) / 2;
  build(st, mid, 2 * no + 1);
  build(mid + 1, en, 2 * no + 2);
  vector<ll> v;
  merge(tree[2 * no + 1].begin(), tree[2 * no + 1].end(),
        tree[2 * no + 2].begin(), tree[2 * no + 2].end(),
        inserter(tree[no], tree[no].end()));
}

set<ll> query(int st, int en, int no, int l, int r) {
  set<ll> s;
  if (l > r or st > en or en < l or r < st)
    return s;
  if (l <= st and r >= en)
    return tree[no];
  ll mid = st + (en - st) / 2;
  set<ll> a = query(st, mid, 2 * no + 1, l, r);
  set<ll> b = query(mid + 1, en, 2 * no + 2, l, r);
  set<ll> m;
  merge(a.begin(), a.end(), b.begin(), b.end(), inserter(m, m.end()));

  return m;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;

  while (t--) {
    ll n, q;
    cin >> n >> q;
    for (int i = 1; i <= n; i++) {
      cin >> ar[i];
    }
    build(1, n, 0);
    // int q,x;
    // cin>>q;
    while (q--) {
      int l, r;
      cin >> l >> r;
      set<ll> x = query(1, n, 0, l, r);

      cout << x.size() << 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 REP(i, n) for (int64_t i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()
#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) {}
  // add x to d[i+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;
  }
  // Reconsider how useful this is.
  //
  // T sum(int r, int l){
  //    return sum(l-1)-sum(r-1);
  //}
};

// How to use
// Initiate BIT, to write "BIT <int> d(n);"

int main() {
  int n, q;
  cin >> n >> q;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  vector<int> col(n + 1, -1);
  vector<vector<int>> che(n);
  rep(i, n) {
    if (col[a[i]] != -1) {
      che[col[a[i]]].push_back(i);
    }
    col[a[i]] = i;
  }

  vector<vector<P>> inp(n);
  rep(i, q) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    inp[a].emplace_back(b, i);
  }
  vector<int> ans(q);
  BIT<int> d(n);
  for (int i = n - 1; i >= 0; --i) {
    for (auto &v : che[i]) {
      d.add(v, 1);
    }
    for (auto &v : inp[i]) {
      ans[v.second] = v.first - i + 1 - d.sum(v.first);
    }
  }
  rep(i, q) { cout << ans[i] << endl; }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define REP(i, n) for (int64_t i = 0; i < (n); ++i)
#define P pair<int, int>
using ll = int64_t;
using namespace std;
#define ketasuu(n) fixed << setprecision(n)
#define btoe(p) p.begin(), p.end()
#define etob(p) p.rbegin(), p.rend()

int main() {
  int n, q;
  cin >> n >> q;
  vector<map<int, int>> mp(n);
  int c;
  cin >> c;
  mp[0][c]++;
  rep(i, n - 1) {
    mp[i + 1] = mp[i];
    int cc;
    cin >> cc;
    mp[i + 1][cc]++;
  }
  rep(iiiiiiiii, q) {
    int l, r;
    cin >> l >> r;
    l--;
    r--;
    if (l != 0) {
      l--;
      int res = 0;
      for (auto &v : mp[r]) {
        if (v.second - mp[l][v.first] > 0)
          res++;
      }
      cout << res << endl;
    } else {
      cout << mp[r].size() << endl;
    }
  }
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 1;

int n, p, q, c[N], g[N], h[N], l[N], r[N], ans[N];

bool compare(int i, int j) {
  int u = l[i] / p, v = l[j] / p;
  return u != v ? (u > v) : (r[i] / p > r[j] / p);
}

int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr), cin >> n >> q;
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  for (int i = 0; i < q; ++i)
    cin >> l[i] >> r[i], --l[i], --r[i];
  for (p = 1; p * p <= n; ++p)
    ;
  iota(h, h + q, 0), --p, sort(h, h + q, compare), ++g[c[0]];
  for (int i, u, v, w = 0, z = 0, j = 0, k = 1; j < q; ++j, ans[i] = k) {
    for (i = h[j], u = l[i], v = r[i]; z > v; --z)
      if (--g[c[z]] == 0)
        --k;
    for (; z < v; ++g[c[z]])
      if (g[c[++z]] == 0)
        ++k;
    for (; w < u; ++w)
      if (--g[c[w]] == 0)
        --k;
    for (; w > u; ++g[c[w]])
      if (g[c[--w]] == 0)
        ++k;
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int M = 5e5 + 1, N = 1e6 + 1;
using iset = unordered_set<int>;

struct bset : bitset<M> {
  int merged = 0;
  inline void merge(const iset &u) {
    for (auto c : u)
      if (not(*this)[c])
        set(c), ++merged;
  }
};

int n, q;
iset seg[N];

inline void update(int i, int c) {
  for (seg[i += n].insert(c); i > 1; seg[i].insert(c))
    i >>= 1;
}

inline int query(int l, int r) {
  bset s;
  for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
    if (l & 1)
      s.merge(seg[l++]);
    if (r & 1)
      s.merge(seg[--r]);
  }
  return s.merged;
}

int main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cin >> n >> q;
  for (int c, i = 0; i < n; ++i)
    cin >> c, update(i, c);
  for (int l, r; q--; cout << query(l - 1, r - 1) << '\\n')
    cin >> l >> r;
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 1;

int n, p, q, a[N], b[N], c[N], g[N], h[N], l[N], r[N], ans[N];

bool compare(int i, int j) {
  return a[i] != a[j] ? (a[i] > a[j]) : (b[i] > b[j]);
}

int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr), cin >> n >> q;
  for (p = 1; p * p <= n; ++p)
    ;
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  for (int m = p - 1, i = 0; i < q; ++i)
    cin >> l[i] >> r[i], a[i] = --l[i] / m, b[i] = --r[i] / m, h[i] = i;
  sort(h, h + q, compare), ++g[c[0]];
  for (int i, u, v, w = 0, z = 0, j = 0, k = 1; j < q; ++j, ans[i] = k) {
    for (i = h[j], u = l[i], v = r[i]; z > v; --z)
      if (--g[c[z]] == 0)
        --k;
    for (; z < v; ++g[c[z]])
      if (g[c[++z]] == 0)
        ++k;
    for (; w < u; ++w)
      if (--g[c[w]] == 0)
        --k;
    for (; w > u; ++g[c[w]])
      if (g[c[--w]] == 0)
        ++k;
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << '\\n';
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 1;

int n, p, q, a[N], b[N], c[N], g[N], h[N], l[N], r[N], ans[N];

bool compare(int i, int j) {
  return a[i] != a[j] ? (a[i] > a[j]) : (b[i] > b[j]);
}

int main() {
  ios::sync_with_stdio(false), cin.tie(nullptr), cin >> n >> q;
  for (int i = 0; i < n; ++i)
    cin >> c[i];
  for (int i = 0; i < q; ++i)
    cin >> l[i] >> r[i], --l[i], --r[i], h[i] = i;
  for (p = 1; p * p <= n; ++p)
    ;
  for (int m = p - 1, i = 0; i < n; ++i)
    a[i] = l[i] / m, b[i] = r[i] / m;
  sort(h, h + q, compare), ++g[c[0]];
  for (int i, u, v, w = 0, z = 0, j = 0, k = 1; j < q; ++j, ans[i] = k) {
    for (i = h[j], u = l[i], v = r[i]; z > v; --z)
      if (--g[c[z]] == 0)
        --k;
    for (; z < v; ++g[c[z]])
      if (g[c[++z]] == 0)
        ++k;
    for (; w < u; ++w)
      if (--g[c[w]] == 0)
        --k;
    for (; w > u; ++g[c[w]])
      if (g[c[--w]] == 0)
        ++k;
  }
  for (int i = 0; i < q; ++i)
    cout << ans[i] << '\\n';
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 7;
struct node {
  int l, r, sum;
} t[N * 40];
int n, cnt, a[N], rt[N];
map<int, int> pos;
void update(int l, int r, int &now, int pre, int k, int v) {
  t[++cnt] = t[pre], t[cnt].sum += v, now = cnt;
  if (l == r)
    return;
  int mid = l + r >> 1;
  if (k <= mid)
    update(l, mid, t[cnt].l, t[pre].l, k, v);
  else
    update(mid + 1, r, t[cnt].r, t[pre].r, k, v);
}
int query(int l, int r, int rt, int k) {
  if (l >= k)
    return t[rt].sum;
  int mid = l + r >> 1;
  if (k <= mid)
    return query(l, mid, t[rt].l, k) + t[t[rt].r].sum;
  return query(mid + 1, r, t[rt].r, k);
}
int main() {
  int q, tmp, l, r;
  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (!pos.count(a[i]))
      update(1, n, rt[i], rt[i - 1], i, 1);
    else
      update(1, n, tmp, rt[i - 1], pos[a[i]], -1),
          update(1, n, rt[i], tmp, i, 1);
    pos[a[i]] = i;
  }
  while (q--)
    scanf("%d%d", &l, &r), printf("%d\\n", query(1, n, rt[r], l));
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 7;
struct query {
  int l, r, id;
} q[N];
int n, m, B, now, c[N], ans[N], num[N];
bool cmp(query a, query b) {
  return (a.l - 1) / B == (b.l - 1) / B ? a.r < b.r
                                        : (a.l - 1) / B < (b.l - 1) / B;
}
void add(int x) {
  if (!num[x])
    ++now;
  ++num[x];
}
void del(int x) {
  --num[x];
  if (!num[x])
    --now;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &c[i]);
  for (int i = 1; i <= m; ++i)
    scanf("%d%d", &q[i].l, &q[i].r), q[i].id = i;
  B = sqrt(n);
  sort(q + 1, q + m + 1, cmp);
  int l = 1, r = 0;
  for (int i = 1; i <= m; ++i) {
    while (r < q[i].r)
      add(c[++r]);
    while (l > q[i].l)
      add(c[--l]);
    while (r > q[i].r)
      del(c[r--]);
    while (l < q[i].l)
      del(c[l++]);
    ans[q[i].id] = now;
  }
  for (int i = 1; i <= m; ++i)
    printf("%d\\n", ans[i]);
}
Problem p02599 (C++) — buggy submission, verdict: Time Limit Exceeded

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

template <typename T> class BIT {
  vector<T> array;
  int n;

public:
  BIT(int num) {
    n = num;
    array.resize(n + 1);
  }
  T sum(int i) {
    T s = 0;
    while (i > 0) {
      s += array[i];
      i -= i & -i;
    }
    return s;
  }
  T sum(int i, int j) { return sum(j) - sum(i - 1); }
  void add(int i, T x) {
    while (i <= n) {
      array[i] += x;
      i += i & -i;
    }
  }
};

int c[500010];
TU que[500010];

int main() {
  int n, q;
  cin >> n >> q;
  BIT<int> bit(n);
  for (int i = 1; i <= n; ++i)
    cin >> c[i];
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    que[i] = TU(r, l, i);
  }
  sort(que, que + q);
  rep(i, q) swap(get<0>(que[i]), get<1>(que[i]));
  vector<int> x(n + 1, -1), ans(q);
  int now = 0;
  rep(i, q) {
    for (int j = now + 1; j <= get<1>(que[i]); ++j) {
      if (x[c[j]] != -1) {
        bit.add(x[c[j]], -1);
      }
      x[c[j]] = j;
      bit.add(x[c[j]], 1);
    }
    ans[get<2>(que[i])] = bit.sum(get<0>(que[i]), get<1>(que[i]));
    now = get<1>(que[i]);
  }
  rep(i, q) cout << ans[i] << endl;
}

Error: TLE

Fixed submission:

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

template <typename T> class BIT {
  vector<T> array;
  int n;

public:
  BIT(int num) {
    n = num;
    array.resize(n + 1);
  }
  T sum(int i) {
    T s = 0;
    while (i > 0) {
      s += array[i];
      i -= i & -i;
    }
    return s;
  }
  T sum(int i, int j) { return sum(j) - sum(i - 1); }
  void add(int i, T x) {
    while (i <= n) {
      array[i] += x;
      i += i & -i;
    }
  }
};

int c[500010];
TU que[500010];

int main() {
  int n, q;
  cin >> n >> q;
  BIT<int> bit(n);
  for (int i = 1; i <= n; ++i)
    cin >> c[i];
  rep(i, q) {
    int l, r;
    cin >> l >> r;
    que[i] = TU(r, l, i);
  }
  sort(que, que + q);
  rep(i, q) swap(get<0>(que[i]), get<1>(que[i]));
  vector<int> x(n + 1, -1), ans(q);
  int now = 0;
  rep(i, q) {
    for (int j = now + 1; j <= get<1>(que[i]); ++j) {
      if (x[c[j]] != -1) {
        bit.add(x[c[j]], -1);
      }
      x[c[j]] = j;
      bit.add(x[c[j]], 1);
    }
    ans[get<2>(que[i])] = bit.sum(get<0>(que[i]), get<1>(que[i]));
  }
  rep(i, q) cout << ans[i] << endl;
}
Problem p02599 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#include <map>

using std::map;

const int MAXN = 500005, MAXLGN = 20;

int N, Q;
map<int, int> pos;
int tree[MAXN];
int cant;
struct node {
  int val, L, R, size;
} buff[2 * MAXN * MAXLGN];

int build(int lo, int hi) {
  if (lo > hi)
    return 0;

  int idx = ++cant;

  int mid = (lo + hi) / 2;
  buff[idx] = (node){mid, build(lo, mid - 1), build(mid + 1, hi), 0};
  return idx;
}

int update(int x, int val, int amount) {

  if (x == 0)
    return 0;

  int idx = ++cant;

  int L = buff[x].L;
  int R = buff[x].R;
  if (val < buff[x].val)
    L = update(L, val, amount);
  if (val > buff[x].val)
    R = update(R, val, amount);

  buff[idx] = (node){buff[x].val, L, R, buff[x].size + amount};

  return idx;
}

int query(int x, int val) {
  if (val < buff[x].val)
    return query(buff[x].L, val) + buff[x].size - buff[buff[x].L].size;

  if (val > buff[x].val)
    return query(buff[x].R, val);

  return buff[x].size - buff[buff[x].L].size;
}

int main() {

  scanf("%d", &N), scanf("%d", &Q);

  tree[0] = build(1, N);
  for (int i = 1; i <= N; i++) {
    int x, posx;
    scanf("%d", &x);
    posx = pos[x];
    if (posx != 0)
      tree[i] = update(update(tree[i - 1], posx, -1), i, +1);
    else
      tree[i] = update(tree[i - 1], i, +1);
    pos[x] = i;
  }

  while (Q--) {
    int lo, hi;
    scanf("%d %d", &lo, &hi);
    printf("%d\\n", query(tree[hi], lo));
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <map>

using std::map;

const int MAXN = 40000, MAXLGN = 16;

int N, Q;
map<int, int> pos;
int tree[MAXN];
int cant;
struct node {
  int val, L, R, size;
} buff[2 * MAXN * MAXLGN];

int build(int lo, int hi) {
  if (lo > hi)
    return 0;

  int idx = ++cant;

  int mid = (lo + hi) / 2;
  buff[idx] = (node){mid, build(lo, mid - 1), build(mid + 1, hi), 0};
  return idx;
}

int update(int x, int val, int amount) {

  if (x == 0)
    return 0;

  int idx = ++cant;

  int L = buff[x].L;
  int R = buff[x].R;
  if (val < buff[x].val)
    L = update(L, val, amount);
  if (val > buff[x].val)
    R = update(R, val, amount);

  buff[idx] = (node){buff[x].val, L, R, buff[x].size + amount};

  return idx;
}

int query(int x, int val) {
  if (val < buff[x].val)
    return query(buff[x].L, val) + buff[x].size - buff[buff[x].L].size;

  if (val > buff[x].val)
    return query(buff[x].R, val);

  return buff[x].size - buff[buff[x].L].size;
}

int main() {

  scanf("%d", &N), scanf("%d", &Q);

  tree[0] = build(1, N);
  for (int i = 1; i <= N; i++) {
    int x, posx;
    scanf("%d", &x);
    posx = pos[x];
    if (posx != 0)
      tree[i] = update(update(tree[i - 1], posx, -1), i, +1);
    else
      tree[i] = update(tree[i - 1], i, +1);
    pos[x] = i;
  }

  while (Q--) {
    int lo, hi;
    scanf("%d %d", &lo, &hi);
    printf("%d\\n", query(tree[hi], lo));
  }

  return 0;
}